Class: Tco::Colouring
- Inherits:
-
Object
- Object
- Tco::Colouring
- Defined in:
- lib/tco/colouring.rb
Constant Summary collapse
- ANSI_FG_BASE =
30
- ANSI_BG_BASE =
40
Instance Attribute Summary collapse
-
#palette ⇒ Object
readonly
Returns the value of attribute palette.
Instance Method Summary collapse
-
#decorate(string, fg, bg, bright, underline) ⇒ Object
Decorate a string according to the style passed.
- #get_best_font_colour(background) ⇒ Object
- #get_colour_instance(value) ⇒ Object
- #get_style(name) ⇒ Object
-
#initialize(configuration) ⇒ Colouring
constructor
A new instance of Colouring.
- #set_output(output_type) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Colouring
Returns a new instance of Colouring.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tco/colouring.rb', line 34 def initialize(configuration) @palette = Palette.new configuration.["palette"] @output_type = configuration.["output"] @disabled = configuration.["disabled"] configuration.colour_values.each do |id, value| @palette.set_colour_value(parse_colour_id(id), parse_rgb_value(value)) end @names = {} configuration.names.each do |name, colour_def| @names[name] = resolve_colour_def colour_def end @styles = {} configuration.styles.each do |name, style| @styles[name] = Style.new(resolve_colour_def(style[:fg]), resolve_colour_def(style[:bg]), style[:bright], style[:underline]) end end |
Instance Attribute Details
#palette ⇒ Object (readonly)
Returns the value of attribute palette.
32 33 34 |
# File 'lib/tco/colouring.rb', line 32 def palette @palette end |
Instance Method Details
#decorate(string, fg, bg, bright, underline) ⇒ Object
Decorate a string according to the style passed. The input string is processed line-by-line (the escape sequences are added to each line). This is due to some problems I’ve been having with some terminal emulators not handling multi-line coloured sequences well.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tco/colouring.rb', line 60 def decorate(string, (fg, bg, bright, underline)) if (!STDOUT.isatty) || @output_type == :raw || @disabled return string end fg = get_colour_instance fg bg = get_colour_instance bg output = [] lines = string.lines.map(&:chomp) lines.each do |line| unless line.length <= 0 line = case @palette.type when "ansi" then colour_ansi line, fg, bg when "extended" then colour_extended line, fg, bg else raise "Unknown palette '#{@palette.type}'." end if bright line = e(1) + line end if underline line = e(4) + line end if (bright or underline) and fg == nil and bg == nil line << e(0) end end output.push line end output << "" if string =~ /\n$/ output.join "\n" end |
#get_best_font_colour(background) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/tco/colouring.rb', line 111 def get_best_font_colour(background) black = Tco::Colour.new([0,0,0]) white = Tco::Colour.new([255,255,255]) if background.is_a?(Colour) && (background - black).abs < (background - white).abs return white end black end |
#get_colour_instance(value) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/tco/colouring.rb', line 123 def get_colour_instance(value) case when value.is_a?(String) resolve_colour_def value when value.is_a?(Array) Colour.new value when value.is_a?(Colour) || value.is_a?(Unknown) value when value == nil nil else raise "Colour value type '#{value.class}' not supported." end end |
#get_style(name) ⇒ Object
98 99 100 101 102 |
# File 'lib/tco/colouring.rb', line 98 def get_style(name) raise "Style '#{name}' not found." unless @styles.has_key? name @styles[name] end |
#set_output(output_type) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/tco/colouring.rb', line 104 def set_output(output_type) unless [:term, :raw].include? output_type raise "Output '#{output_type}' not supported." end @output_type = output_type end |