Class: Toys::Utils::Terminal
- Inherits:
-
Object
- Object
- Toys::Utils::Terminal
- Defined in:
- core-docs/toys/utils/terminal.rb
Overview
Defined in the toys-core gem
A simple terminal class.
Styles
This class supports ANSI styled output where supported.
Styles may be specified in any of the following forms:
- A symbol indicating the name of a well-known style, or the name of a defined style.
- An rgb string in hex "rgb" or "rrggbb" form.
- An ANSI code string in
\e[XXm
form. - An array of ANSI codes as integers.
Defined Under Namespace
Classes: TerminalError
Constant Summary collapse
- CLEAR_CODE =
ANSI style code to clear styles
"\e[0m"
- BUILTIN_STYLE_NAMES =
Standard ANSI style codes by name.
{ clear: [0], reset: [0], bold: [1], faint: [2], italic: [3], underline: [4], blink: [5], reverse: [7], black: [30], red: [31], green: [32], yellow: [33], blue: [34], magenta: [35], cyan: [36], white: [37], on_black: [30], on_red: [31], on_green: [32], on_yellow: [33], on_blue: [34], on_magenta: [35], on_cyan: [36], on_white: [37], bright_black: [90], bright_red: [91], bright_green: [92], bright_yellow: [93], bright_blue: [94], bright_magenta: [95], bright_cyan: [96], bright_white: [97], on_bright_black: [100], on_bright_red: [101], on_bright_green: [102], on_bright_yellow: [103], on_bright_blue: [104], on_bright_magenta: [105], on_bright_cyan: [106], on_bright_white: [107], }.freeze
- DEFAULT_SPINNER_FRAME_LENGTH =
Default length of a single spinner frame, in seconds.
0.1
- DEFAULT_SPINNER_FRAMES =
Default set of spinner frames.
["-", "\\", "|", "/"].freeze
Instance Attribute Summary collapse
-
#input ⇒ IO?
readonly
Input stream.
-
#output ⇒ IO, ...
readonly
Output stream or logger.
-
#styled ⇒ Boolean
readonly
Whether output is styled.
Class Method Summary collapse
-
.remove_style_escapes(str) ⇒ String
Returns a copy of the given string with all ANSI style codes removed.
Instance Method Summary collapse
-
#<<(str) ⇒ self
Write a line, appending a newline if one is not already present.
-
#apply_styles(str, *styles) ⇒ String
Apply the given styles to the given string, returning the styled string.
-
#ask(prompt, *styles, default: nil, trailing_text: :default) ⇒ String
Ask a question and get a response.
-
#close ⇒ Object
This method is defined so that
::Logger
will recognize a terminal as a log device target, but it does not actually close anything. -
#confirm(prompt = "Proceed? ", *styles, default: nil) ⇒ Boolean
Confirm with the user.
-
#define_style(name, *styles) ⇒ self
Define a named style.
-
#height ⇒ Integer
Return the terminal height.
-
#initialize(input: $stdin, output: $stdout, styled: nil) ⇒ Terminal
constructor
Create a terminal.
-
#newline ⇒ self
Write a newline and flush the current line.
-
#puts(str = "", *styles) ⇒ self
(also: #say)
Write a line, appending a newline if one is not already present.
-
#readline ⇒ String?
Read a line, blocking until one is available.
-
#size ⇒ Array(Integer,Integer)
Return the terminal size as an array of width, height.
-
#spinner(leading_text: "", final_text: "", frame_length: nil, frames: nil, style: nil) ⇒ Object
Display a spinner during a task.
-
#width ⇒ Integer
Return the terminal width.
-
#write(str = "", *styles) ⇒ self
Write a partial line without appending a newline.
Constructor Details
#initialize(input: $stdin, output: $stdout, styled: nil) ⇒ Terminal
Create a terminal.
117 118 119 |
# File 'core-docs/toys/utils/terminal.rb', line 117 def initialize(input: $stdin, output: $stdout, styled: nil) # Source available in the toys-core gem end |
Instance Attribute Details
#input ⇒ IO? (readonly)
Input stream
131 132 133 |
# File 'core-docs/toys/utils/terminal.rb', line 131 def input @input end |
#output ⇒ IO, ... (readonly)
Output stream or logger
125 126 127 |
# File 'core-docs/toys/utils/terminal.rb', line 125 def output @output end |
#styled ⇒ Boolean (readonly)
Whether output is styled
137 138 139 |
# File 'core-docs/toys/utils/terminal.rb', line 137 def styled @styled end |
Class Method Details
.remove_style_escapes(str) ⇒ String
Returns a copy of the given string with all ANSI style codes removed.
105 106 107 |
# File 'core-docs/toys/utils/terminal.rb', line 105 def self.remove_style_escapes(str) # Source available in the toys-core gem end |
Instance Method Details
#<<(str) ⇒ self
Write a line, appending a newline if one is not already present.
188 189 190 |
# File 'core-docs/toys/utils/terminal.rb', line 188 def <<(str) # Source available in the toys-core gem end |
#apply_styles(str, *styles) ⇒ String
Apply the given styles to the given string, returning the styled string. Honors the styled setting; if styling is disabled, does not add any ANSI style codes and in fact removes any existing codes. If styles were added, ensures that the string ends with a clear code.
305 306 307 |
# File 'core-docs/toys/utils/terminal.rb', line 305 def apply_styles(str, *styles) # Source available in the toys-core gem end |
#ask(prompt, *styles, default: nil, trailing_text: :default) ⇒ String
Ask a question and get a response.
212 213 214 |
# File 'core-docs/toys/utils/terminal.rb', line 212 def ask(prompt, *styles, default: nil, trailing_text: :default) # Source available in the toys-core gem end |
#close ⇒ Object
This method is defined so that ::Logger
will recognize a terminal as
a log device target, but it does not actually close anything.
165 166 167 |
# File 'core-docs/toys/utils/terminal.rb', line 165 def close # Source available in the toys-core gem end |
#confirm(prompt = "Proceed? ", *styles, default: nil) ⇒ Boolean
Confirm with the user.
226 227 228 |
# File 'core-docs/toys/utils/terminal.rb', line 226 def confirm(prompt = "Proceed? ", *styles, default: nil) # Source available in the toys-core gem end |
#define_style(name, *styles) ⇒ self
Define a named style.
Style names must be symbols. The definition of a style may include any valid style specification, including the symbol names of existing defined styles.
291 292 293 |
# File 'core-docs/toys/utils/terminal.rb', line 291 def define_style(name, *styles) # Source available in the toys-core gem end |
#height ⇒ Integer
Return the terminal height
276 277 278 |
# File 'core-docs/toys/utils/terminal.rb', line 276 def height # Source available in the toys-core gem end |
#newline ⇒ self
Write a newline and flush the current line.
196 197 198 |
# File 'core-docs/toys/utils/terminal.rb', line 196 def newline # Source available in the toys-core gem end |
#puts(str = "", *styles) ⇒ self Also known as: say
Write a line, appending a newline if one is not already present.
177 178 179 |
# File 'core-docs/toys/utils/terminal.rb', line 177 def puts(str = "", *styles) # Source available in the toys-core gem end |
#readline ⇒ String?
Read a line, blocking until one is available.
157 158 159 |
# File 'core-docs/toys/utils/terminal.rb', line 157 def readline # Source available in the toys-core gem end |
#size ⇒ Array(Integer,Integer)
Return the terminal size as an array of width, height.
258 259 260 |
# File 'core-docs/toys/utils/terminal.rb', line 258 def size # Source available in the toys-core gem end |
#spinner(leading_text: "", final_text: "", frame_length: nil, frames: nil, style: nil) ⇒ Object
Display a spinner during a task. You should provide a block that performs the long-running task. While the block is executing, a spinner will be displayed.
248 249 250 251 |
# File 'core-docs/toys/utils/terminal.rb', line 248 def spinner(leading_text: "", final_text: "", frame_length: nil, frames: nil, style: nil) # Source available in the toys-core gem end |
#width ⇒ Integer
Return the terminal width
267 268 269 |
# File 'core-docs/toys/utils/terminal.rb', line 267 def width # Source available in the toys-core gem end |
#write(str = "", *styles) ⇒ self
Write a partial line without appending a newline.
147 148 149 |
# File 'core-docs/toys/utils/terminal.rb', line 147 def write(str = "", *styles) # Source available in the toys-core gem end |