Class: Bundler::Thor::Shell::Color
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: 
          self,
           Basic | |
| Instance Chain: 
          self,
           Basic | |
| Inherits: | Bundler::Thor::Shell::Basic 
 | 
| Defined in: | lib/bundler/vendor/thor/lib/thor/shell/color.rb | 
Overview
Constant Summary
- 
    BLACK =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 15Set the terminal’s foreground ANSI color to black. "\e[30m"
- 
    BLUE =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 23Set the terminal’s foreground ANSI color to blue. "\e[34m"
- 
    BOLD =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 12The start of an ANSI bold sequence. "\e[1m"
- 
    CLEAR =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 10Embed in a String to clear all previous ANSI sequences. "\e[0m"
- 
    CYAN =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 27Set the terminal’s foreground ANSI color to cyan. "\e[36m"
- 
    GREEN =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 19Set the terminal’s foreground ANSI color to green. "\e[32m"
- 
    MAGENTA =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 25Set the terminal’s foreground ANSI color to magenta. "\e[35m"
- 
    ON_BLACK =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 32Set the terminal’s background ANSI color to black. "\e[40m"
- 
    ON_BLUE =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 40Set the terminal’s background ANSI color to blue. "\e[44m"
- 
    ON_CYAN =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 44Set the terminal’s background ANSI color to cyan. "\e[46m"
- 
    ON_GREEN =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 36Set the terminal’s background ANSI color to green. "\e[42m"
- 
    ON_MAGENTA =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 42Set the terminal’s background ANSI color to magenta. "\e[45m"
- 
    ON_RED =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 34Set the terminal’s background ANSI color to red. "\e[41m"
- 
    ON_WHITE =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 46Set the terminal’s background ANSI color to white. "\e[47m"
- 
    ON_YELLOW =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 38Set the terminal’s background ANSI color to yellow. "\e[43m"
- 
    RED =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 17Set the terminal’s foreground ANSI color to red. "\e[31m"
- 
    WHITE =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 29Set the terminal’s foreground ANSI color to white. "\e[37m"
- 
    YELLOW =
    # File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 21Set the terminal’s foreground ANSI color to yellow. "\e[33m"
Class Method Summary
Instance Attribute Summary
- #can_display_colors? ⇒ Boolean readonly protected
- 
    
      #diff_lcs_loaded?  ⇒ Boolean 
    
    readonly
    protected
    Internal use only
    Check if Diff::LCSis loaded.
Basic - Inherited
Instance Method Summary
- 
    
      #set_color(string, *colors)  
    
    Set color by using a string or one of the defined constants. 
- #output_diff_line(diff) protected Internal use only
- 
    
      #show_diff(destination, content)  
    
    protected
    Internal use only
    Overwrite show_diff to show diff with colors if Diff::LCSis available.
Basic - Inherited
| #ask | Asks something to the user and receives a response. | 
| #error | Called if something goes wrong during the execution. | 
| #file_collision | Deals with file collision and returns true if the file should be overwritten and false otherwise. | 
| #indent | Sets the output padding while executing a block and resets it. | 
| #no? | Make a question the to user and returns true if the user replies “n” or “no”. | 
| #print_in_columns | Prints values in columns. | 
| #print_table | Prints a table. | 
| #print_wrapped | Prints a long string, word-wrapping the text to the current width of the terminal display. | 
| #say | Say (print) something to the user. | 
| #say_status | Say a status with the given color and appends the message. | 
| #terminal_width | This code was copied from Rake, available under MIT-LICENSE Copyright © 2003, 2004 Jim Weirich. | 
| #yes? | Make a question the to user and returns true if the user replies “y” or “yes”. | 
| #as_unicode | See additional method definition at line 394. | 
| #ask_filtered, #ask_simply, | |
| #dynamic_width | Calculate the dynamic width of the terminal. | 
| #dynamic_width_stty, #dynamic_width_tput, #lookup_color, #prepare_message, #stderr, #stdout, #truncate, | |
| #set_color | Apply color to the given string with optional bold. | 
| #file_collision_help, #is?, #show_diff | |
Constructor Details
This class inherits a constructor from Bundler::Thor::Shell::Basic
Instance Attribute Details
    #can_display_colors?  ⇒ Boolean  (readonly, protected)
  
  [ GitHub ]
# File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 99
def can_display_colors? stdout.tty? end
    #diff_lcs_loaded?  ⇒ Boolean  (readonly, protected)
  
  Check if Diff::LCS is loaded. If it is, use it to create pretty output for diff.
# File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 136
def diff_lcs_loaded? #:nodoc: return true if defined?(Diff::LCS) return @diff_lcs_loaded unless @diff_lcs_loaded.nil? @diff_lcs_loaded = begin require "diff/lcs" true rescue LoadError false end end
Instance Method Details
#output_diff_line(diff) (protected)
# File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 119
def output_diff_line(diff) #:nodoc: case diff.action when "-" say "- #{diff.old_element.chomp}", :red, true when "+" say "+ #{diff.new_element.chomp}", :green, true when "!" say "- #{diff.old_element.chomp}", :red, true say "+ #{diff.new_element.chomp}", :green, true else say " #{diff.old_element.chomp}", nil, true end end
#set_color(string, *colors)
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, :boldThe 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/bundler/vendor/thor/lib/thor/shell/color.rb', line 79
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
#show_diff(destination, content) (protected)
Overwrite show_diff to show diff with colors if Diff::LCS is available.
# File 'lib/bundler/vendor/thor/lib/thor/shell/color.rb', line 106
def show_diff(destination, content) #:nodoc: if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil? actual = File.binread(destination).to_s.split("\n") content = content.to_s.split("\n") Diff::LCS.sdiff(actual, content).each do |diff| output_diff_line(diff) end else super end end