Class: TTY::Terminal::Color
- Inherits:
-
Object
- Object
- TTY::Terminal::Color
- Defined in:
- lib/tty/terminal/color.rb
Overview
A class responsible for coloring strings.
Constant Summary collapse
- CLEAR =
Embed in a String to clear all previous ANSI sequences.
"\e[0m"
- BOLD =
The start of an ANSI bold sequence.
"\e[1m"
- UNDERLINE =
The start of an ANSI underlined sequence.
"\e[4m"
- STYLES =
%w[ BOLD CLEAR UNDERLINE ].freeze
- BLACK =
Escape codes for text color.
"\e[30m"
- RED =
"\e[31m"
- GREEN =
"\e[32m"
- YELLOW =
"\e[33m"
- BLUE =
"\e[34m"
- MAGENTA =
"\e[35m"
- CYAN =
"\e[36m"
- WHITE =
"\e[37m"
- LIGHT_BLACK =
"\e[90m"
- LIGHT_RED =
"\e[91m"
- LIGHT_GREEN =
"\e[92m"
- LIGHT_YELLOW =
"\e[93m"
- LIGHT_BLUE =
"\e[94m"
- LIGHT_MAGENTA =
"\e[95m"
- LIGHT_CYAN =
"\e[96m"
- TEXT_COLORS =
(constants.grep(/^[^ON_]*/) - STYLES).freeze
- ON_BLACK =
Escape codes for background color.
"\e[40m"
- ON_RED =
"\e[41m"
- ON_GREEN =
"\e[42m"
- ON_YELLOW =
"\e[43m"
- ON_BLUE =
"\e[44m"
- ON_MAGENTA =
"\e[45m"
- ON_CYAN =
"\e[46m"
- ON_WHITE =
"\e[47m"
- ON_LIGHT_BLACK =
"\e[100m"
- ON_LIGHT_RED =
"\e[101m"
- ON_LIGHT_GREEN =
"\e[102m"
- ON_LIGHT_YELLOW =
"\e[103m"
- ON_LIGHT_BLUE =
"\e[104m"
- ON_LIGHT_MAGENTA =
"\e[105m"
- ON_LIGHT_CYAN =
"\e[106m"
- BACKGROUND_COLORS =
constants.grep(/^ON_*/).freeze
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
Class Method Summary collapse
-
.set(string, *colors) ⇒ undefined
Same as instance method.
Instance Method Summary collapse
-
#code(*colors) ⇒ Array[String]
Return raw color code without embeding it into a string.
-
#disable! ⇒ Object
Disable coloring of this terminal session.
-
#enabled? ⇒ Boolean
Check if coloring is on.
-
#initialize(enabled = false) ⇒ Color
constructor
Initialize a Terminal Color.
-
#names ⇒ Array[String]
All ANSI color names as strings.
-
#remove(string) ⇒ String
Remove color codes from a string.
-
#set(string, *colors) ⇒ String
Apply ANSI color to the given string.
Constructor Details
#initialize(enabled = false) ⇒ Color
Initialize a Terminal Color
63 64 65 |
# File 'lib/tty/terminal/color.rb', line 63 def initialize(enabled=false) @enabled = enabled end |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
58 59 60 |
# File 'lib/tty/terminal/color.rb', line 58 def enabled @enabled end |
Class Method Details
.set(string, *colors) ⇒ undefined
Same as instance method.
105 106 107 |
# File 'lib/tty/terminal/color.rb', line 105 def self.set(string, *colors) new.set(string, *colors) end |
Instance Method Details
#code(*colors) ⇒ Array[String]
Return raw color code without embeding it into a string.
126 127 128 129 |
# File 'lib/tty/terminal/color.rb', line 126 def code(*colors) validate(*colors) colors.map { |color| lookup(color) } end |
#disable! ⇒ Object
Disable coloring of this terminal session
70 71 72 |
# File 'lib/tty/terminal/color.rb', line 70 def disable! @enabled = false end |
#enabled? ⇒ Boolean
Check if coloring is on
77 78 79 |
# File 'lib/tty/terminal/color.rb', line 77 def enabled? @enabled end |
#names ⇒ Array[String]
All ANSI color names as strings.
136 137 138 |
# File 'lib/tty/terminal/color.rb', line 136 def names (STYLES + BACKGROUND_COLORS + TEXT_COLORS).map { |color| color.to_s.downcase } end |
#remove(string) ⇒ String
Remove color codes from a string.
116 117 118 |
# File 'lib/tty/terminal/color.rb', line 116 def remove(string) string.to_s.gsub(/(\[)?\033(\[)?[;?\d]*[\dA-Za-z](\])?/, '') end |
#set(string, *colors) ⇒ String
Apply ANSI color to the given string.
94 95 96 97 98 |
# File 'lib/tty/terminal/color.rb', line 94 def set(string, *colors) validate(*colors) ansi_colors = colors.map { |color| lookup(color) } "#{ansi_colors.join}#{string}#{CLEAR}" end |