Class: Hammock::StringPatches::InstanceMethods::Colorizer
- Inherits:
-
Object
- Object
- Hammock::StringPatches::InstanceMethods::Colorizer
- Defined in:
- lib/hammock/monkey_patches/string.rb
Constant Summary collapse
- HomeOffset =
29
- LightOffset =
60
- BGOffset =
10
- LightRegex =
/^light_/
- ColorRegex =
/^(light_)?none|gr[ae]y|red|green|yellow|blue|pink|cyan|white$/
- CtrlRegex =
/^bold|underlined?|blink(ing)?|reversed?$/
- ColorOffsets =
relative to HomeOffset
{ # relative to HomeOffset 'none' => 0, 'gray' => 61, 'grey' => 61, 'red' => 2, 'green' => 3, 'yellow' => 4, 'blue' => 5, 'pink' => 6, 'cyan' => 7, 'white' => 8 }
- CtrlOffsets =
{ 'bold' => 1, 'underline' => 4, 'underlined' => 4, 'blink' => 5, 'blinking' => 5, 'reverse' => 7, 'reversed' => 7 }
Class Method Summary collapse
- .bg_for(name) ⇒ Object
- .colorize(text, description) ⇒ Object
- .ctrl_for(name) ⇒ Object
- .fg_for(name) ⇒ Object
Class Method Details
.bg_for(name) ⇒ Object
218 219 220 221 222 |
# File 'lib/hammock/monkey_patches/string.rb', line 218 def bg_for name # There's a hole in the table on bg=none, so we use BGOffset to the left offset = fg_for((name || '').sub(/^on_/, '')) offset + BGOffset unless offset == HomeOffset end |
.colorize(text, description) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/hammock/monkey_patches/string.rb', line 204 def colorize text, description terms = " #{description} ".gsub(' light ', ' light_').gsub(' on ', ' on_').strip.split(/\s+/) bg = terms.detect {|i| /on_#{ColorRegex}/ =~ i } fg = terms.detect {|i| ColorRegex =~ i } ctrl = terms.detect {|i| CtrlRegex =~ i } "\e[#{"0;#{fg_for(fg)};#{bg_for(bg) || ctrl_for(ctrl)}"}m#{text}\e[0m" end |
.ctrl_for(name) ⇒ Object
224 225 226 |
# File 'lib/hammock/monkey_patches/string.rb', line 224 def ctrl_for name CtrlOffsets[name] || HomeOffset end |
.fg_for(name) ⇒ Object
213 214 215 216 |
# File 'lib/hammock/monkey_patches/string.rb', line 213 def fg_for name light = name.gsub!(LightRegex, '') unless name.nil? (ColorOffsets[name] || 0) + HomeOffset + (light ? LightOffset : 0) end |