module MiniHistogram::MiniUnicodePlot::StyledPrinter
Constants
- COLOR_DECODE
- COLOR_ENCODE
- DISABLE_TEXT_STYLE
- TEXT_COLORS
Public Instance Methods
color?(out)
click to toggle source
# File lib/mini_histogram/plot.rb, line 359 def color?(out) (out && out.tty?) || false end
print_color(out, color, *args)
click to toggle source
# File lib/mini_histogram/plot.rb, line 354 def print_color(out, color, *args) color = COLOR_DECODE[color] print_styled(out, *args, color: color) end
print_styled(out, *args, bold: false, color: :normal)
click to toggle source
# File lib/mini_histogram/plot.rb, line 332 def print_styled(out, *args, bold: false, color: :normal) return out.print(*args) unless color?(out) str = StringIO.open {|sio| sio.print(*args); sio.close; sio.string } color = :nothing if bold && color == :bold enable_ansi = TEXT_COLORS.fetch(color, TEXT_COLORS[:default]) + (bold ? TEXT_COLORS[:bold] : "") disable_ansi = (bold ? DISABLE_TEXT_STYLE[:bold] : "") + DISABLE_TEXT_STYLE.fetch(color, TEXT_COLORS[:default]) first = true StringIO.open do |sio| str.each_line do |line| sio.puts unless first first = false continue if line.empty? sio.print(enable_ansi, line, disable_ansi) end sio.close out.print(sio.string) end end