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, **opts) ⇒ String
Ask the user for some text.
-
#capture_command_output(*command, **kwargs) {|line| ... } ⇒ 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) ⇒ Boolean
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) ⇒ String
Uses
CLI::UI.fmt
to format a string. -
#titleize(string) ⇒ Object
Titleize the given
string
. -
#ui_enabled? ⇒ Boolean
If the StdoutRouter is enabled.
Instance Method Details
#ask(question, default: nil, **opts) ⇒ String
Ask the user for some text.
77 78 79 |
# File 'lib/cli/mastermind/user_interface.rb', line 77 def ask(question, default: nil, **opts) CLI::UI.ask(question, default: default, **opts) end |
#capture_command_output(*command, **kwargs) {|line| ... } ⇒ 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.
186 187 188 189 190 |
# File 'lib/cli/mastermind/user_interface.rb', line 186 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
.
42 43 44 45 46 47 48 49 50 |
# File 'lib/cli/mastermind/user_interface.rb', line 42 def concurrently group = AsyncSpinners.new yield group group.wait group.results end |
#confirm(question) ⇒ Boolean
Ask the user a yes/no question
86 87 88 |
# File 'lib/cli/mastermind/user_interface.rb', line 86 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
63 64 65 66 |
# File 'lib/cli/mastermind/user_interface.rb', line 63 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 unless multiple:
is true
.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cli/mastermind/user_interface.rb', line 104 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 default_text = default_text # dup so that we don't change whatever was passed in .dup.tap { |o| o.delete(default_text) } end # Ensure all keys are strings for CLI::UI default_text = default_text.to_s .transform_keys!(&:to_s) # work around a bug in CLI::UI with multi-select and block invocation if opts[:multiple] # Put default at the beginning so it shows in the expected order keys = [default_text] + .keys # Put default back into the options so it can be pulled out [default_text] = default response = Array(CLI::UI::Prompt.ask(question, options: keys, **opts)) return response.map { |resp| [resp] } end return default unless .count > 0 CLI::UI::Prompt.ask(question, **opts) do |handler| handler.option(default_text) { 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
22 23 24 25 26 27 28 29 30 |
# File 'lib/cli/mastermind/user_interface.rb', line 22 def spinner(title, &block) return yield unless ui_enabled? results = concurrently do |actions| actions.await(title, &block) end results[title] end |
#stylize(string) ⇒ String
Uses CLI::UI.fmt
to format a string
57 58 59 |
# File 'lib/cli/mastermind/user_interface.rb', line 57 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.
165 166 167 |
# File 'lib/cli/mastermind/user_interface.rb', line 165 def titleize(string) string.gsub(/[-_-]/, ' ').split(' ').map(&:capitalize).join(' ') end |
#ui_enabled? ⇒ Boolean
Returns if the StdoutRouter is enabled.
11 12 13 |
# File 'lib/cli/mastermind/user_interface.rb', line 11 def ui_enabled? CLI::UI::StdoutRouter.enabled? end |