class Thor::Shell::Color
Inherit from Thor::Shell::Basic
and add set_color
behavior. Check Thor::Shell::Basic
to see all available methods.
Constants
- BLACK
-
Set the terminal’s foreground ANSI color to black.
- BLUE
-
Set the terminal’s foreground ANSI color to blue.
- BOLD
-
The start of an ANSI bold sequence.
- CLEAR
-
Embed in a String to clear all previous ANSI sequences.
- CYAN
-
Set the terminal’s foreground ANSI color to cyan.
- GREEN
-
Set the terminal’s foreground ANSI color to green.
- MAGENTA
-
Set the terminal’s foreground ANSI color to magenta.
- ON_BLACK
-
Set the terminal’s background ANSI color to black.
- ON_BLUE
-
Set the terminal’s background ANSI color to blue.
- ON_CYAN
-
Set the terminal’s background ANSI color to cyan.
- ON_GREEN
-
Set the terminal’s background ANSI color to green.
- ON_MAGENTA
-
Set the terminal’s background ANSI color to magenta.
- ON_RED
-
Set the terminal’s background ANSI color to red.
- ON_WHITE
-
Set the terminal’s background ANSI color to white.
- ON_YELLOW
-
Set the terminal’s background ANSI color to yellow.
- RED
-
Set the terminal’s foreground ANSI color to red.
- WHITE
-
Set the terminal’s foreground ANSI color to white.
- YELLOW
-
Set the terminal’s foreground ANSI color to yellow.
Public Instance Methods
Set color by using a string or one of the defined constants. If a third option is set to true, it also adds bold to the string. This is based on Highline implementation and it automatically appends CLEAR
to the end of the returned String.
Pass foreground, background and bold options to this method as symbols.
Example:
set_color "Hi!", :red, :on_white, :bold
The available colors are:
:bold :black :red :green :yellow :blue :magenta :cyan :white :on_black :on_red :on_green :on_yellow :on_blue :on_magenta :on_cyan :on_white
# File lib/thor/shell/color.rb, line 82 def set_color(string, *colors) if colors.compact.empty? || !can_display_colors? string elsif colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) } ansi_colors = colors.map { |color| lookup_color(color) } "#{ansi_colors.join}#{string}#{CLEAR}" else # The old API was `set_color(color, bold=boolean)`. We # continue to support the old API because you should never # break old APIs unnecessarily :P foreground, bold = colors foreground = self.class.const_get(foreground.to_s.upcase) if foreground.is_a?(Symbol) bold = bold ? BOLD : "" "#{bold}#{foreground}#{string}#{CLEAR}" end end
Protected Instance Methods
# File lib/thor/shell/color.rb, line 110 def are_colors_disabled? !ENV["NO_COLOR"].nil? && !ENV["NO_COLOR"].empty? end
# File lib/thor/shell/color.rb, line 106 def are_colors_supported? stdout.tty? && ENV["TERM"] != "dumb" end
# File lib/thor/shell/color.rb, line 102 def can_display_colors? are_colors_supported? && !are_colors_disabled? end