class Text
Represents a plain text output handler without color support.
Definitions
def initialize(io)
Initialize a new Text output handler.
Signature
-
parameter
ioIO The IO object to write to.
Implementation
def initialize(io)
@io = io
@styles = {reset: self.reset}
@indent = String.new
@styles[:indent] = @indent
end
attr :styles
Signature
-
attribute
Hash The style definitions.
def buffered
Create a buffered output handler.
Signature
-
returns
Buffered A new Buffered instance.
Implementation
def buffered
Buffered.new(self)
end
def append(buffer)
Append and replay chunks from a buffer.
Signature
-
parameter
bufferBuffered The buffer to append from.
Implementation
def append(buffer)
buffer.each do |operation|
self.public_send(*operation)
end
end
attr :io
Signature
-
attribute
IO The IO object to write to.
INDENTATION = "\t"
The indentation string.
def indent
Increase indentation level.
Implementation
def indent
@indent << INDENTATION
end
def outdent
Decrease indentation level.
Implementation
def outdent
@indent.slice!(INDENTATION)
end
def indented
Execute a block with increased indentation.
Signature
-
yields
{...} The block to execute.
Implementation
def indented
self.indent
yield
ensure
self.outdent
end
def interactive?
Signature
-
returns
Boolean Whether the IO is interactive (a TTY).
Implementation
def interactive?
@io.tty?
end
def [](key)
Get a style by key.
Signature
-
parameter
keySymbol The style key.
-
returns
String The style value.
Implementation
def [] key
@styles[key]
end
def []=(key, value)
Set a style by key.
Signature
-
parameter
keySymbol The style key.
-
parameter
valueString The style value.
Implementation
def []= key, value
@styles[key] = value
end
def size
Signature
-
returns
Array(Integer) The terminal size [height, width] (defaults to [24, 80]).
Implementation
def size
[24, 80]
end
def width
Signature
-
returns
Integer The terminal width (defaults to 80).
Implementation
def width
size.last
end
def colors?
Signature
-
returns
Boolean Always returns false, as Text output doesn't support colors.
Implementation
def colors?
false
end
def style(foreground, background = nil, *attributes)
Create a style string (no-op for Text output).
Signature
-
parameter
foregroundSymbol, nil The foreground color.
-
parameter
backgroundSymbol, nil The background color.
-
parameter
attributesArray Additional style attributes.
-
returns
String An empty string.
Implementation
def style(foreground, background = nil, *attributes)
end
def reset
Signature
-
returns
String An empty string (no reset needed for plain text).
Implementation
def reset
end
def write(*arguments)
Print out the given arguments. When the argument is a symbol, look up the style and inject it into the io stream. When the argument is a proc/lambda, call it with self as the argument. When the argument is anything else, write it directly to the io.
Signature
-
parameter
argumentsArray The arguments to write.
Implementation
def write(*arguments)
arguments.each do |argument|
case argument
when Symbol
@io.write(self[argument])
when Proc
argument.call(self)
else
if argument.respond_to?(:print)
argument.print(self)
else
@io.write(argument)
end
end
end
end
def puts(*arguments)
Print out the arguments as per #write, followed by the reset sequence and a newline.
Signature
-
parameter
argumentsArray The arguments to write.
Implementation
def puts(*arguments)
write(*arguments)
@io.puts(self.reset)
end
def variable(value, limit: Variable::TRUNCATION_LIMIT)
Write a value in the variable style: a compact, truncated representation (handling large values, recursion and styling internally).
Signature
-
parameter
valueObject The value to represent.
-
parameter
limitInteger The maximum length of the representation.
Implementation
def variable(value, limit: Variable::TRUNCATION_LIMIT)
Variable.format(self, value, limit: limit)
end