Class: Babushka::ANSI
Constant Summary collapse
- BG_OFFSET =
10
- COLOR_REGEX =
/black|gr[ae]y|red|green|yellow|blue|pink|cyan|white/
- FG_REGEX =
/\b#{COLOR_REGEX}\b/
- BG_REGEX =
/\bon_#{COLOR_REGEX}\b/
- CTRL_REGEX =
/\bbold|underlined?|blink(ing)?|reversed?\b/
- COLOR_OFFSETS =
{ # This is actually "bright black", i.e. black (30) plus brightness (60), # which almost all terminals render as grey. 'gray' => 90, 'grey' => 90, 'black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'pink' => 35, 'cyan' => 36, 'white' => 37 }
- CTRL_OFFSETS =
{ 'bold' => 1, 'underline' => 4, 'underlined' => 4, 'blink' => 5, 'blinking' => 5, 'reverse' => 7, 'reversed' => 7 }
Class Method Summary collapse
- .bg_for(desc) ⇒ Object
- .ctrl_for(desc) ⇒ Object
- .escape_for(description) ⇒ Object
- .fg_for(desc) ⇒ Object
- .linux_pty? ⇒ Boolean
- .using_colour? ⇒ Boolean
-
.wrap(text, description) ⇒ Object
Wraps
text
with ANSI escape codes to render it as described indescription
.
Class Method Details
.bg_for(desc) ⇒ Object
58 59 60 61 |
# File 'lib/babushka/ansi.rb', line 58 def self.bg_for desc offset = fg_for((desc[BG_REGEX] || '').sub(/^on_/, '')) offset + BG_OFFSET unless offset.nil? end |
.ctrl_for(desc) ⇒ Object
63 64 65 |
# File 'lib/babushka/ansi.rb', line 63 def self.ctrl_for desc CTRL_OFFSETS[desc[CTRL_REGEX]] end |
.escape_for(description) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/babushka/ansi.rb', line 43 def self.escape_for description # Make "on_grey" etc single words, so the foreground regex doesn't match. desc = description.strip.gsub(/\bon /, 'on_') # If we're on a linux pty, substitute 'bold black' for 'bright black'. desc = desc.gsub(/\bgrey\b/, 'bold black') if linux_pty? codes = [fg_for(desc), bg_for(desc), ctrl_for(desc)] "\e[#{codes.compact.join(';')}m" end |
.fg_for(desc) ⇒ Object
54 55 56 |
# File 'lib/babushka/ansi.rb', line 54 def self.fg_for desc COLOR_OFFSETS[desc[FG_REGEX]] end |
.linux_pty? ⇒ Boolean
67 68 69 |
# File 'lib/babushka/ansi.rb', line 67 def self.linux_pty? STDOUT.tty? && (ENV['TERM'] == 'linux') end |
.using_colour? ⇒ Boolean
71 72 73 74 |
# File 'lib/babushka/ansi.rb', line 71 def self.using_colour? # This means "colour", not "no colour". The "no_" is the flippable bit. Babushka::Base.cmdline.opts[:"[no_]color"] end |
.wrap(text, description) ⇒ Object
Wraps text
with ANSI escape codes to render it as described in description
. Some examples:
Babushka::ANSI.wrap('babushka', 'green') #=> "\e[32mbabushka\e[m"
Babushka::ANSI.wrap('babushka', 'on grey') #=> "\e[100mbabushka\e[m"
Babushka::ANSI.wrap('babushka', 'underlined blue') #=> "\e[34;4mbabushka\e[m"
Babushka::ANSI.wrap('babushka', 'reverse') #=> "\e[7mbabushka\e[m"
35 36 37 38 39 40 41 |
# File 'lib/babushka/ansi.rb', line 35 def self.wrap text, description if !using_colour? text else "#{escape_for(description)}#{text}\e[m" end end |