Module: SimpleColor::Utilities
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#attributes_from_colors(s) ⇒ Object
scan s from left to right and for each ansi sequences found split it into elementary components and output the symbolic meaning eg: SimpleColor.attributes_from_colors(SimpleColor.color("foo", :red)) => [:red, :reset].
- #clear ⇒ Object
-
#color_entities(l, color_regexp: Colorer.regexp(:color)) ⇒ Object
split the line into characters and ANSII color sequences.
-
#color_strings(l, color_regexp: Colorer.regexp(:color)) ⇒ Object
same as above but split into strings.
-
#copy_colors(s, t) ⇒ Object
copy the colors from s to t.
-
#current_colors(s) ⇒ Object
get the ansi sequences on s (assume the whole line is colored) returns left_ansi, right_ansi, string.
- #fill(s, columns: ENV['COLUMNS']&.to_i || 80) ⇒ Object
Instance Method Details
#[](*args) ⇒ Object
145 146 147 |
# File 'lib/simplecolor.rb', line 145 def [](*args) color(*args) end |
#attributes_from_colors(s) ⇒ Object
scan s from left to right and for each ansi sequences found split it into elementary components and output the symbolic meaning eg: SimpleColor.attributes_from_colors(SimpleColor.color("foo", :red)) => [:red, :reset]
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/simplecolor.rb', line 75 def attributes_from_colors(s) s.scan(Colorer.regexp(:ansi)).flat_map do |a| next :reset if a=="\e[m" #alternative for reset stack=[] # a[/\e\[(.*)m/,1].split(';').map {|c| Colorer.colors.key(c.to_i)} codes=a[/\e\[(.*)m/,1].split(';') i=0; while i<codes.length do c=codes[i] if c=="38" or c=="48" if codes[i+1]=="5" #256 colors v=codes[i+2] stack << RGB.new(v.to_i, mode: 256, background: c=="48") i+=3 elsif codes[i+1]=="2" #true colors r=codes[i+2].to_i; g=codes[i+3].to_i; b=codes[i+4].to_i stack << RGB.new([r,g,b], background: c=="48") i+=5 end else stack << Colorer.colors.key(c.to_i) i+=1 end end stack end end |
#clear ⇒ Object
141 142 143 |
# File 'lib/simplecolor.rb', line 141 def clear color(:clear) end |
#color_entities(l, color_regexp: Colorer.regexp(:color)) ⇒ Object
split the line into characters and ANSII color sequences
117 118 119 |
# File 'lib/simplecolor.rb', line 117 def color_entities(l, color_regexp: Colorer.regexp(:color)) l.split(/(#{color_regexp})/).flat_map {|c| color?(c) ? [c] : c.split('') } end |
#color_strings(l, color_regexp: Colorer.regexp(:color)) ⇒ Object
same as above but split into strings
121 122 123 124 125 126 127 |
# File 'lib/simplecolor.rb', line 121 def color_strings(l, color_regexp: Colorer.regexp(:color)) u=l.split(/(#{color_regexp})/) # if we start with an ANSI sequence, u is ["", ...], so we need to # get rid of that "" u.shift if u.first == "" u end |
#copy_colors(s, t) ⇒ Object
copy the colors from s to t
111 112 113 114 |
# File 'lib/simplecolor.rb', line 111 def copy_colors(s,t) b,e=current_colors(s) b+t+e end |
#current_colors(s) ⇒ Object
get the ansi sequences on s (assume the whole line is colored) returns left_ansi, right_ansi, string
104 105 106 107 108 109 |
# File 'lib/simplecolor.rb', line 104 def current_colors(s) m=s.match(/^(#{Colorer.regexp(:match)})(.*?)(#{Colorer.regexp(:match)})$/) [m[1],m[3],m[2]] rescue ArgumentError #rescue from "invalid byte sequence in UTF-8" ["","",s] end |
#fill(s, columns: ENV['COLUMNS']&.to_i || 80) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/simplecolor.rb', line 129 def fill(s, columns:ENV['COLUMNS']&.to_i || 80) r=s.each_line.map do |l| l=l.chomp length=uncolor(l).length to_fill=columns - (length % columns) to_fill = 0 if to_fill == columns l+" "*to_fill end.join("\n") r+="\n" if s.end_with?("\n") r end |