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"
- TEXT_COLORS =
%w[ BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE ].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"
- BACKGROUND_COLORS =
%w[ ON_BLACK ON_RED ON_GREEN ON_YELLOW ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE ].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
47 48 49 |
# File 'lib/tty/terminal/color.rb', line 47 def initialize(enabled=false) @enabled = enabled end |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
42 43 44 |
# File 'lib/tty/terminal/color.rb', line 42 def enabled @enabled end |
Class Method Details
.set(string, *colors) ⇒ undefined
Same as instance method.
89 90 91 |
# File 'lib/tty/terminal/color.rb', line 89 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.
110 111 112 113 |
# File 'lib/tty/terminal/color.rb', line 110 def code(*colors) validate *colors colors.map { |color| lookup(color) } end |
#disable! ⇒ Object
Disable coloring of this terminal session
54 55 56 |
# File 'lib/tty/terminal/color.rb', line 54 def disable! @enabled = false end |
#enabled? ⇒ Boolean
Check if coloring is on
61 62 63 |
# File 'lib/tty/terminal/color.rb', line 61 def enabled? @enabled end |
#names ⇒ Array[String]
All ANSI color names as strings.
120 121 122 |
# File 'lib/tty/terminal/color.rb', line 120 def names (STYLES + BACKGROUND_COLORS + TEXT_COLORS).map { |color| color.to_s.downcase } end |
#remove(string) ⇒ String
Remove color codes from a string.
100 101 102 |
# File 'lib/tty/terminal/color.rb', line 100 def remove(string) string.to_s.gsub(/(\[)?\033(\[)?[;?\d]*[\dA-Za-z](\])?/, '') end |
#set(string, *colors) ⇒ String
Apply ANSI color to the given string.
78 79 80 81 82 |
# File 'lib/tty/terminal/color.rb', line 78 def set(string, *colors) validate *colors ansi_colors = colors.map { |color| lookup(color) } "#{ansi_colors.join}#{string}#{CLEAR}" end |