Module: Toys::StandardMixins::Terminal

Includes:
Mixin
Defined in:
core-docs/toys/standard_mixins/terminal.rb

Overview

Defined in the toys-core gem

A mixin that provides a simple terminal. It includes a set of methods that produce styled output, get user input, and otherwise interact with the user's terminal. This mixin is not as richly featured as other mixins such as Highline, but it has no gem dependencies so is ideal for basic cases.

You may make these methods available to your tool by including the following directive in your tool configuration:

include :terminal

A Terminal object will then be available by calling the #terminal method. For information on using this object, see the documentation for Utils::Terminal. Some of the most useful methods are also mixed into the tool and can be called directly.

You can configure the Terminal object by passing options to the include directive. For example:

include :terminal, styled: true

The arguments will be passed on to Utils::Terminal#initialize.

Constant Summary collapse

KEY =

Context key for the terminal object.

Returns:

  • (Object)
::Object.new.freeze

Instance Method Summary collapse

Methods included from Mixin

create

Instance Method Details

#ask(prompt, *styles, default: nil, trailing_text: :default) ⇒ String

Ask a question and get a response.

Parameters:

  • prompt (String)

    Required prompt string.

  • styles (Symbol, String, Array<Integer>...)

    Styles to apply to the prompt.

  • default (String, nil) (defaults to: nil)

    Default value, or nil for no default. Uses nil if not specified.

  • trailing_text (:default, String, nil) (defaults to: :default)

    Trailing text appended to the prompt, nil for none, or :default to show the default.

Returns:

  • (String)

See Also:



89
90
91
# File 'core-docs/toys/standard_mixins/terminal.rb', line 89

def ask(prompt, *styles, default: nil, trailing_text: :default)
  # Source available in the toys-core gem
end

#confirm(prompt = "Proceed?", *styles, default: nil) ⇒ Boolean

Confirm with the user.

Parameters:

  • prompt (String) (defaults to: "Proceed?")

    Prompt string. Defaults to "Proceed?".

  • styles (Symbol, String, Array<Integer>...)

    Styles to apply to the prompt.

  • default (Boolean, nil) (defaults to: nil)

    Default value, or nil for no default. Uses nil if not specified.

Returns:

  • (Boolean)

See Also:



105
106
107
# File 'core-docs/toys/standard_mixins/terminal.rb', line 105

def confirm(prompt = "Proceed?", *styles, default: nil)
  # 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.

Parameters:

  • str (String) (defaults to: "")

    The line to write

  • styles (Symbol, String, Array<Integer>...)

    Styles to apply to the entire line.

Returns:

  • (self)

See Also:



56
57
58
# File 'core-docs/toys/standard_mixins/terminal.rb', line 56

def puts(str = "", *styles)
  # Source available in the toys-core gem
end

#spinner(leading_text: "", final_text: "", frame_length: nil, frames: nil, style: nil, &block) ⇒ 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.

Parameters:

  • leading_text (String) (defaults to: "")

    Optional leading string to display to the left of the spinner. Default is the empty string.

  • frame_length (Float) (defaults to: nil)

    Length of a single frame, in seconds. Defaults to Utils::Terminal::DEFAULT_SPINNER_FRAME_LENGTH.

  • frames (Array<String>) (defaults to: nil)

    An array of frames. Defaults to Utils::Terminal::DEFAULT_SPINNER_FRAMES.

  • style (Symbol, Array<Symbol>) (defaults to: nil)

    A terminal style or array of styles to apply to all frames in the spinner. Defaults to empty,

  • final_text (String) (defaults to: "")

    Optional final string to display when the spinner is complete. Default is the empty string. A common practice is to set this to newline.

Returns:

  • (Object)

    The return value of the block.

See Also:



129
130
131
132
# File 'core-docs/toys/standard_mixins/terminal.rb', line 129

def spinner(leading_text: "", final_text: "",
            frame_length: nil, frames: nil, style: nil, &block)
  # Source available in the toys-core gem
end

#terminalToys::Utils::Terminal

A tool-wide terminal instance



42
43
44
# File 'core-docs/toys/standard_mixins/terminal.rb', line 42

def terminal
  # Source available in the toys-core gem
end

#write(str = "", *styles) ⇒ self

Write a partial line without appending a newline.

Parameters:

  • str (String) (defaults to: "")

    The line to write

  • styles (Symbol, String, Array<Integer>...)

    Styles to apply to the partial line.

Returns:

  • (self)

See Also:



71
72
73
# File 'core-docs/toys/standard_mixins/terminal.rb', line 71

def write(str = "", *styles)
  # Source available in the toys-core gem
end