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

Instance Method Details

#ask(question, default: nil, **opts) ⇒ String

Ask the user for some text.

Parameters:

  • question (String)

    the question to ask the user

  • default (String) (defaults to: nil)

    the default answer

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :is_file (Boolean) — default: nil

    Use file autocompletion (tab completion)

  • :allow_empty (Boolean) — default: true

    Allow the answer to be empty

Returns:

  • (String)

    the user’s answer

See Also:



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.

Parameters:

  • command (Array<String>)

    the command to execute

  • kwargs (Hash)

    additional arguments to be passed into IO.popen

Yield Parameters:

  • line (String)

    a line of output to be processed

See Also:

  • IO.popen
  • Open3.popen


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.

Yield Parameters:

See Also:



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

Parameters:

  • question (String)

    the question to ask the user

Returns:

  • (Boolean)

    how the user answered

See Also:



86
87
88
# File 'lib/cli/mastermind/user_interface.rb', line 86

def confirm(question)
  CLI::UI.confirm(question)
end

#enable_uiObject

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.

Parameters:

  • question (String)

    The question to ask the user

  • options (Array<#to_s>, Hash<#to_s>)

    the options to display

  • default (String) (defaults to: options.first)

    The default value for this question. Assumed to exist within the given options.

  • opts (Hash)

    additional options passed into CLI::UI::Prompt.ask.

Options Hash (**opts):

  • :multiple (Boolean) — default: false

    Whether multiple selections should be made.

  • :filter_ui (Boolean) — default: true

    Enable option filtering

  • :select_ui (Boolean) — default: true

    Enable long-form option selection

See Also:



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: options.first, **opts)
  default_value = nil
  options = case options
            when Array
              default_text = default

              o = options - [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 = options.invert[default]
              end

              default_text = default_text

              # dup so that we don't change whatever was passed in
              options.dup.tap { |o| o.delete(default_text) }
            end

  # Ensure all keys are strings for CLI::UI
  default_text = default_text.to_s
  options.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] + options.keys

    # Put default back into the options so it can be pulled out
    options[default_text] = default

    response = Array(CLI::UI::Prompt.ask(question, options: keys, **opts))
    return response.map { |resp| options[resp] }
  end

  return default unless options.count > 0

  CLI::UI::Prompt.ask(question, **opts) do |handler|
    handler.option(default_text) { default }

    options.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

Parameters:

  • title (String)

    the title to display

  • block (#call)

    passed to the underlying spinner implementation.

Returns:

  • the result of calling the given block

See Also:



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

Parameters:

  • string (String)

    the string to format

Returns:

  • (String)

    the formatted string

See Also:



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.

Examples:

titleize(‘foo’) => ‘Foo’


titleize(‘foo bar’) => ‘Foo Bar’


titleize(‘foo-bar’) => ‘Foo Bar’


titleize(‘foo_bar’) => ‘Foo Bar’


Parameters:

  • string (String)

    the string to titleize.



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.

Returns:

  • (Boolean)

    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