Class: LLM::Paint
- Inherits:
-
Object
- Object
- LLM::Paint
- 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.
Defined Under Namespace
Classes: IO
Constant Summary collapse
- FOREGROUND =
Returns 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.
{ 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.
{ 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.
"\e[0m"
Class Method Summary collapse
-
.[](text, *options) ⇒ String
Wraps a string in the escape sequences that represent the given colors and effects.
-
.enabled? ⇒ Boolean
True when color is enabled.
-
.enabled=(enabled)
Enables or disables color.
-
.detect(io = $stdout) ⇒ Boolean
Detects whether an IO supports color.
Class Method Details
.[](text, *options) ⇒ String
Wraps a string in the escape sequences that represent the given colors and effects.
80 81 82 83 |
# File 'lib/llm/paint.rb', line 80 def self.[](text, *) return text.to_s if .empty? || !enabled? "\e[#{codes()}m#{text}#{RESET}" end |
.enabled? ⇒ Boolean
Returns 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.
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.
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 |