Method: UnicodePlot::StyledPrinter#print_styled

Defined in:
lib/unicode_plot/styled_printer.rb


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/unicode_plot/styled_printer.rb', line 60

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