1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2025, by Samuel Williams.
# Copyright, 2025, by Patrik Wenger.
require "io/console"
module Console
# Styled terminal output.
module Terminal
# A simple text-based terminal output.
class Text
# Create a new text terminal output.
#
# @parameter stream [IO] The stream to write to.
def initialize(stream)
@stream = stream
@styles = {reset: self.reset}
end
# @attribute [IO] The stream to write to.
attr :stream
# Get the style associated with the given key.
#
# @parameter key [Symbol] The key to look up.
# @returns [String] The style associated with the key.
def [] key
@styles[key]
end
# Set the style associated with the given key.
#
# @parameter key [Symbol] The key to associate the style with.
# @parameter value [String] The style to associate with the key.
def []= key, value
@styles[key] = value
end
# @returns [Boolean] Whether the terminal supports colors.
def colors?
false
end
# @returns [Tuple(Integer, Integer)] The size of the terminal, or a default value of [24, 80].
def size
[24, 80]
end
# @returns [Integer] The width of the terminal.
def width
self.size.last
end
# Generate a style string for the given foreground, background, and attributes.
#
# @returns [String | Nil] The style string if colors are supported, otherwise nil.
def style(foreground, background = nil, *attributes)
end
# Generate a reset sequence.
#
# @returns [String | Nil] The reset sequence if colors are supported, otherwise nil.
def reset
end
# Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.
#
# @parameter arguments [Array] The arguments to write.
# @parameter style [Symbol] The style to apply.
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
# Write the given arguments to the output stream using the given style. The reset sequence is automatically
# appended at the end of each line.
#
# @parameter arguments [Array] The arguments to write, each on a new line.
# @parameter style [Symbol] The style to apply.
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
# 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.
#
# @parameter arguments [Array] The arguments to print.
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
# Print rich text to the output stream, followed by the reset sequence and a newline.
#
# @parameter arguments [Array] The arguments to print.
def print_line(*arguments)
print(*arguments)
@stream.puts(self.reset)
end
end
end
end
|