Class: LLM::Paint::IO
- Inherits:
-
Object
- Object
- LLM::Paint::IO
- Defined in:
- lib/llm/paint/io.rb
Overview
The IO class wraps an IO so that the text written to it is wrapped in the escape sequences a terminal understands, but only when the wrapped IO is a terminal and LLM::Paint is enabled. Otherwise the text is written unchanged.
Instance Attribute Summary collapse
-
#io ⇒ IO
readonly
The IO that is wrapped.
Instance Method Summary collapse
- #initialize(io:, color: nil) ⇒ LLM::Paint::IO constructor
-
#color? ⇒ Boolean
True when this IO writes color.
-
#color=(color)
Enables or disables color, and when it is given nil it goes back to detecting what the IO supports.
-
#write(text, *options) ⇒ Integer
Writes text to the wrapped IO.
-
#puts(text, *options) ⇒ Integer
Write with a newline.
Constructor Details
#initialize(io:, color: nil) ⇒ LLM::Paint::IO
27 28 29 30 |
# File 'lib/llm/paint/io.rb', line 27 def initialize(io:, color: nil) @io = io @color = color.nil? ? LLM::Paint.detect(io) : !!color end |
Instance Attribute Details
#io ⇒ IO (readonly)
Returns The IO that is wrapped.
18 19 20 |
# File 'lib/llm/paint/io.rb', line 18 def io @io end |
Instance Method Details
#color? ⇒ Boolean
Returns True when this IO writes color.
35 36 37 |
# File 'lib/llm/paint/io.rb', line 35 def color? @color end |
#color=(color)
This method returns an undefined value.
Enables or disables color, and when it is given nil it goes back to detecting what the IO supports.
44 45 46 |
# File 'lib/llm/paint/io.rb', line 44 def color=(color) @color = color.nil? ? LLM::Paint.detect(@io) : !!color end |
#write(text, *options) ⇒ Integer
Writes text to the wrapped IO. The text is wrapped in the escape sequences that represent the given colors and effects when color is enabled, and written unchanged otherwise.
59 60 61 62 63 64 65 |
# File 'lib/llm/paint/io.rb', line 59 def write(text, *) if color? @io.write(LLM::Paint[text, *]) else @io.write(text.to_s) end end |
#puts(text, *options) ⇒ Integer
Write with a newline.
71 72 73 |
# File 'lib/llm/paint/io.rb', line 71 def puts(text, *) write("#{text}\n", *) end |