Sha256: 9e4c7fd6d962a66f1b69f3529fc7cb8e0e7cc90ad4f593614721b78546aa2325
Contents?: true
Size: 1.64 KB
Versions: 20
Compression:
Stored size: 1.64 KB
Contents
# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. require 'io/console' module Console # Styled terminal output. module Terminal class Text def initialize(output) @output = output @styles = {reset: self.reset} end def [] key @styles[key] end def []= key, value @styles[key] = value end def colors? false end def style(foreground, background = nil, *attributes) end def reset end def write(*arguments, style: nil) if style and prefix = self[style] @output.write(prefix) @output.write(*arguments) @output.write(self.reset) else @output.write(*arguments) end end def puts(*arguments, style: nil) if style and prefix = self[style] @output.write(prefix) @output.puts(*arguments) @output.write(self.reset) else @output.puts(*arguments) end end # Print out the given arguments. # 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. def print(*arguments) arguments.each do |argument| case argument when Symbol @output.write(self[argument]) when Proc argument.call(self) else @output.write(argument) end end end # Print out the arguments as per {#print}, followed by the reset sequence and a newline. def print_line(*arguments) print(*arguments) @output.puts(self.reset) end end end end
Version data entries
20 entries across 20 versions & 1 rubygems