Module: SimpleColor::Helpers
- Included in:
- SimpleColor
- Defined in:
- lib/simplecolor.rb
Instance Method Summary collapse
- #attributes_from_colors(s) ⇒ Object
-
#color_entities(l) ⇒ Object
split the line into characters and ANSII color sequences.
-
#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).
- #mix_in(klass) ⇒ Object
- #mix_in_string ⇒ Object
Instance Method Details
#attributes_from_colors(s) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/simplecolor.rb', line 157 def attributes_from_colors(s) s.scan(/#{ANSICOLOR_REGEXP}/).flat_map do |a| next :reset if a=="\e[m" a[/\e\[(.*)m/,1].split(';').map {|c| COLORS.key(c.to_i)} end end |
#color_entities(l) ⇒ Object
split the line into characters and ANSII color sequences
176 177 178 |
# File 'lib/simplecolor.rb', line 176 def color_entities(l) l.split(/(#{COLOR_REGEXP})/).flat_map {|c| color?(c) ? [c] : c.split('') } end |
#copy_colors(s, t) ⇒ Object
copy the colors from s to t
171 172 173 174 |
# File 'lib/simplecolor.rb', line 171 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)
164 165 166 167 168 169 |
# File 'lib/simplecolor.rb', line 164 def current_colors(s) m=s.match(/^(#{COLORMATCH_REGEXP})(.*?)(#{COLORMATCH_REGEXP})$/) [m[1],m[3],m[2]] rescue ArgumentError #rescue from "invalid byte sequence in UTF-8" ["","",s] end |
#mix_in(klass) ⇒ Object
150 151 152 |
# File 'lib/simplecolor.rb', line 150 def mix_in(klass) klass.send :include, SimpleColor end |
#mix_in_string ⇒ Object
153 154 155 |
# File 'lib/simplecolor.rb', line 153 def mix_in_string mix_in(String) end |