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) ⇒ String

Ask the user for some text.

Parameters:

  • question (String)

    the question to ask the user

  • default (String) (defaults to: nil)

    the default answer

Returns:

  • (String)

    the user’s answer

See Also:



74
75
76
# File 'lib/cli/mastermind/user_interface.rb', line 74

def ask(question, default: nil)
  CLI::UI.ask(question, default: default)
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


162
163
164
165
166
# File 'lib/cli/mastermind/user_interface.rb', line 162

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:



83
84
85
# File 'lib/cli/mastermind/user_interface.rb', line 83

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.

Parameters:

  • question (String)

    The question to ask the user

  • options (Array<String>, Hash)

    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.

See Also:



98
99
100
101
102
103
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
# File 'lib/cli/mastermind/user_interface.rb', line 98

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

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

  return default unless options.count > 0

  CLI::UI::Prompt.ask(question, **opts) do |handler|
    handler.option(default_text.to_s) { 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.



141
142
143
# File 'lib/cli/mastermind/user_interface.rb', line 141

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