class Console::Terminal::Text
A simple text-based terminal output.
def [] key
@parameter key [Symbol] The key to look up.
Get the style associated with the given key.
def [] key @styles[key] end
def []= key, value
@parameter key [Symbol] The key to associate the style with.
Set the style associated with the given key.
def []= key, value @styles[key] = value end
def colors?
def colors? false end
def initialize(stream)
Create a new text terminal output.
def initialize(stream) @stream = stream @styles = {reset: self.reset} end
def print(*arguments)
- When the argument is anything else, write it directly to the output.
- When the argument is a proc/lambda, call it with self as the argument.
- When the argument is a symbol, look up the style and inject it into the output stream.
Print rich text to the output stream.
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.
def print_line(*arguments) print(*arguments) @stream.puts(self.reset) end
def puts(*arguments, style: nil)
@parameter arguments [Array] The arguments to write, each on a new line.
appended at the end of each line.
Write the given arguments to the output stream using the given style. The reset sequence is automatically
def puts(*arguments, style: nil) if style and prefix = self[style] arguments.each do |argument| argument.to_s.lines.each do |line| @stream.write(prefix, line.chomp) @stream.puts(self.reset) end end else @stream.puts(*arguments) end end
def reset
Generate a reset sequence.
def reset end
def size
def size [24, 80] end
def style(foreground, background = nil, *attributes)
Generate a style string for the given foreground, background, and attributes.
def style(foreground, background = nil, *attributes) end
def width
def width self.size.last end
def write(*arguments, style: nil)
@parameter arguments [Array] The arguments to write.
Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.
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