Module: CLI::Mastermind::UserInterface
- Included in:
- CLI::Mastermind
- Defined in:
- lib/cli/mastermind/user_interface.rb
Overview
Wraps methods from CLI::UI in a slightly nicer DSL
Defined Under Namespace
Classes: AsyncSpinners
Instance Method Summary collapse
-
#ask(question, default: nil) ⇒ Object
Ask the user for some text.
-
#capture_command_output(*command, **kwargs, &block) ⇒ Object
Capture the output of the given command and print them in a cli-ui friendly way.
-
#concurrently {|group| ... } ⇒ Object
Performs a set of actions concurrently Yields an
AsyncSpinners
objects which inherits fromCLI::UI::SpinGroup
. -
#confirm(question) ⇒ Object
Ask the user a yes/no
question
. -
#enable_ui ⇒ Object
Enables cli-ui’s STDOUT Router for fancy UIs.
-
#frame(*args) ⇒ Object
Opens a CLI::UI frame with the given
args
. -
#select(question, options:, default: options.first, **opts) ⇒ Object
Display an interactive list of options for the user to select.
-
#spinner(title, &block) ⇒ Object
(also: #await)
Display a spinner with a
title
while data is being loaded. -
#stylize(string) ⇒ Object
Uses
CLI::UI.fmt
to format a string. -
#titleize(string) ⇒ Object
Titleize the given
string
. -
#ui_enabled? ⇒ Boolean
:private:.
Instance Method Details
#ask(question, default: nil) ⇒ Object
Ask the user for some text.
58 59 60 |
# File 'lib/cli/mastermind/user_interface.rb', line 58 def ask(question, default: nil) CLI::UI.ask(question, default: default) end |
#capture_command_output(*command, **kwargs, &block) ⇒ Object
Capture the output of the given command and print them in a cli-ui friendly way. This command is an ease of use wrapper around a common capture construct.
The command given can be a single string, an array of strings, or individual arguments. The command and any kwargs given are passed to IO.popen to capture output.
Optionally, a block may be passed to modify the output of the line prior to printing.
138 139 140 141 142 |
# File 'lib/cli/mastermind/user_interface.rb', line 138 def capture_command_output(*command, **kwargs, &block) # Default block returns what's passed in block ||= -> line { line } IO.popen(command.flatten, **kwargs) { |io| io.each_line { |line| print block.call(line) } } end |
#concurrently {|group| ... } ⇒ Object
Performs a set of actions concurrently Yields an AsyncSpinners
objects which inherits from CLI::UI::SpinGroup
. The only difference between the two is that AsyncSpinners
provides a mechanism for exfiltrating results by using await
instead of the usual add
.
33 34 35 36 37 38 39 40 41 |
# File 'lib/cli/mastermind/user_interface.rb', line 33 def concurrently group = AsyncSpinners.new yield group group.wait group.results end |
#confirm(question) ⇒ Object
Ask the user a yes/no question
64 65 66 |
# File 'lib/cli/mastermind/user_interface.rb', line 64 def confirm(question) CLI::UI.confirm(question) end |
#enable_ui ⇒ Object
Enables cli-ui’s STDOUT Router for fancy UIs
5 6 7 |
# File 'lib/cli/mastermind/user_interface.rb', line 5 def enable_ui CLI::UI::StdoutRouter.enable end |
#frame(*args) ⇒ Object
Opens a CLI::UI frame with the given args
51 52 53 54 |
# File 'lib/cli/mastermind/user_interface.rb', line 51 def frame(*args) return yield unless ui_enabled? CLI::UI::Frame.open(*args) { yield } end |
#select(question, options:, default: options.first, **opts) ⇒ Object
Display an interactive list of options for the user to select. If less than 2 options would be displayed, the default value is automatically returned.
Any other keyword arguments given are passed down into CLI::UI::Prompt.ask
.
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 |
# File 'lib/cli/mastermind/user_interface.rb', line 81 def select(question, options:, default: .first, **opts) default_value = nil = case when Array default_text = default o = - [default] o.zip(o).to_h when Hash # Handle the "default" default. Otherwise, we expect the default # is the default value if default.is_a? Array default_text, default = default else default_text = .invert[default] end # dup so that we don't change whatever was passed in .dup.tap { |o| o.delete(default_text) } end return default unless .count > 0 CLI::UI::Prompt.ask(question, **opts) do |handler| handler.option(default_text.to_s) { default } .each do |(text, value)| handler.option(text) { value } end end end |
#spinner(title, &block) ⇒ Object Also known as: await
Display a spinner with a title
while data is being loaded
17 18 19 20 21 22 23 24 25 |
# File 'lib/cli/mastermind/user_interface.rb', line 17 def spinner(title, &block) return yield unless ui_enabled? results = concurrently do |actions| actions.await(title, &block) end results[title] end |
#stylize(string) ⇒ Object
Uses CLI::UI.fmt
to format a string
45 46 47 |
# File 'lib/cli/mastermind/user_interface.rb', line 45 def stylize(string) CLI::UI.fmt string end |
#titleize(string) ⇒ Object
Titleize the given string
. Replaces any dashes (-) or underscores (_) in the string
with spaces and then capitalizes each word.
Examples:
titleize('foo') => 'Foo'
titleize('foo bar') => 'Foo Bar'
titleize('foo-bar') => 'Foo Bar'
titleize('foo_bar') => 'Foo Bar'
122 123 124 |
# File 'lib/cli/mastermind/user_interface.rb', line 122 def titleize(string) string.gsub(/[-_-]/, ' ').split(' ').map(&:capitalize).join(' ') end |
#ui_enabled? ⇒ Boolean
:private:
10 11 12 |
# File 'lib/cli/mastermind/user_interface.rb', line 10 def ui_enabled? CLI::UI::StdoutRouter.enabled? end |