Class: LLM::Paint

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/paint.rb,
lib/llm/paint/io.rb

Overview

The Paint class wraps a string in the ANSI escape sequences that a terminal understands as color. It returns the text it is given, unchanged, when color is disabled.

Examples:

LLM::Paint["hello", :blue]        # => "\e[34mhello\e[0m"
LLM::Paint["hello", :blue, :bold] # => "\e[34;1mhello\e[0m"
LLM::Paint["hello", :white, :on_red]

Defined Under Namespace

Classes: IO

Constant Summary collapse

FOREGROUND =

Returns The codes that set the foreground color.

Returns:

  • (Hash<Symbol, Integer>) —

    The codes that set the foreground color

{
  black: 30, red: 31, green: 32, yellow: 33,
  blue: 34, magenta: 35, cyan: 36, white: 37
}.freeze
BACKGROUND =

Returns The codes that set the background color.

Returns:

  • (Hash<Symbol, Integer>) —

    The codes that set the background color

{
  on_black: 40, on_red: 41, on_green: 42, on_yellow: 43,
  on_blue: 44, on_magenta: 45, on_cyan: 46, on_white: 47
}.freeze
EFFECTS =

Returns The codes that set an effect.

Returns:

  • (Hash<Symbol, Integer>) —

    The codes that set an effect

{
  bold: 1, faint: 2, italic: 3, underline: 4,
  blink: 5, inverse: 7, hidden: 8, strikethrough: 9
}.freeze
RESET =

Returns The escape sequence that resets every attribute.

Returns:

  • (String) —

    The escape sequence that resets every attribute

"\e[0m"

Class Method Summary collapse

Class Method Details

.[](text, *options) ⇒ String

Wraps a string in the escape sequences that represent the given colors and effects.

Parameters:

  • text (String, #to_s) —

    The text to colorize

  • options (Array<Symbol>) —

    A foreground color, a background color, and/or an effect

Returns:

  • (String)

Raises:

  • (ArgumentError) —

    When an option is neither a color nor an effect



80
81
82
83
# File 'lib/llm/paint.rb', line 80

def self.[](text, *options)
  return text.to_s if options.empty? || !enabled?
  "\e[#{codes(options)}m#{text}#{RESET}"
end

.enabled? ⇒ Boolean

Returns True when color is enabled.

Returns:

  • (Boolean) —

    True when color is enabled



88
89
90
# File 'lib/llm/paint.rb', line 88

def self.enabled?
  @enabled.nil? ? detect : @enabled
end

.enabled=(enabled)

This method returns an undefined value.

Enables or disables color. When it is given nil it goes back to detecting what the terminal supports.

Parameters:

  • enabled (Boolean, nil)


97
98
99
# File 'lib/llm/paint.rb', line 97

def self.enabled=(enabled)
  @enabled = enabled.nil? ? nil : !!enabled
end

.detect(io = $stdout) ⇒ Boolean

Detects whether an IO supports color. Color is disabled when $NO_COLOR is set, and when the IO is not a terminal.

Parameters:

  • io (IO) (defaults to: $stdout) —

    The IO to inspect

Returns:

  • (Boolean)

See Also:



108
109
110
111
# File 'lib/llm/paint.rb', line 108

def self.detect(io = $stdout)
  return false unless ENV["NO_COLOR"].to_s.strip.empty?
  io.respond_to?(:tty?) && io.tty?
end