Method: HighLine#color

Defined in:
lib/highline.rb

#color(string, *colors) ⇒ String

This method provides easy access to ANSI color sequences, without the user needing to remember to CLEAR at the end of each sequence. Just pass the string to color, followed by a list of colors you would like it to be affected by. The colors can be HighLine class constants, or symbols (:blue for BLUE, for example). A CLEAR will automatically be embedded to the end of the returned String.

This method returns the original string unchanged if use_color? is false.

Examples:

cli = HighLine.new
cli.color("Sustainable", :green, :bold)
# => "\e[32m\e[1mSustainable\e[0m"

# As class method (delegating to HighLine.default_instance)
HighLine.color("Sustainable", :green, :bold)

Parameters:

  • string (String)

    string to be colored

  • colors (Array<Symbol>)

    array of colors like [:red, :blue]

Returns:

  • (String)

    (ANSI escaped) colored string



328
329
330
331
# File 'lib/highline.rb', line 328

def color(string, *colors)
  return string unless use_color?
  HighLine.Style(*colors).color(string)
end