class Text
A simple text-based terminal output.
Definitions
def initialize(stream)
Create a new text terminal output.
Signature
-
parameter
stream
IO
The stream to write to.
Implementation
def initialize(stream)
@stream = stream
@styles = {reset: self.reset}
end
attr :stream
Signature
-
attribute
IO
The stream to write to.
def [] key
Get the style associated with the given key.
Signature
-
parameter
key
Symbol
The key to look up.
-
returns
String
The style associated with the key.
Implementation
def [] key
@styles[key]
end
def []= key, value
Set the style associated with the given key.
Signature
-
parameter
key
Symbol
The key to associate the style with.
-
parameter
value
String
The style to associate with the key.
Implementation
def []= key, value
@styles[key] = value
end
def colors?
Signature
-
returns
Boolean
Whether the terminal supports colors.
Implementation
def colors?
false
end
def size
Signature
-
returns
Tuple(Integer, Integer)
The size of the terminal, or a default value of [24, 80].
Implementation
def size
[24, 80]
end
def width
Signature
-
returns
Integer
The width of the terminal.
Implementation
def width
self.size.last
end
def style(foreground, background = nil, *attributes)
Generate a style string for the given foreground, background, and attributes.
Signature
-
returns
String | Nil
The style string if colors are supported, otherwise nil.
Implementation
def style(foreground, background = nil, *attributes)
end
def reset
Generate a reset sequence.
Signature
-
returns
String | Nil
The reset sequence if colors are supported, otherwise nil.
Implementation
def reset
end
def write(*arguments, style: nil)
Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.
Signature
-
parameter
arguments
Array
The arguments to write.
-
parameter
style
Symbol
The style to apply.
Implementation
def write(*arguments, style: nil)
if style and prefix = self[style]
@stream.write(prefix)
@stream.write(*arguments)
@stream.write(self.reset)
else
@stream.write(*arguments)
end
end
def puts(*arguments, style: nil)
Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.
Signature
-
parameter
arguments
Array
The arguments to write, each on a new line.
-
parameter
style
Symbol
The style to apply.
Implementation
def puts(*arguments, style: nil)
if style and prefix = self[style]
@stream.write(prefix)
@stream.puts(*arguments)
@stream.write(self.reset)
else
@stream.puts(*arguments)
end
end
def print(*arguments)
Print rich text to the output stream.
- When the argument is a symbol, look up the style and inject it into the output 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 output.
Signature
-
parameter
arguments
Array
The arguments to print.
Implementation
def print(*arguments)
arguments.each do |argument|
case argument
when Symbol
@stream.write(self[argument])
when Proc
argument.call(self)
else
@stream.write(argument)
end
end
end
def print_line(*arguments)
Print rich text to the output stream, followed by the reset sequence and a newline.
Signature
-
parameter
arguments
Array
The arguments to print.
Implementation
def print_line(*arguments)
print(*arguments)
@stream.puts(self.reset)
end