Module: GitlabInternalEventsCli::Helpers::CliInputs

Included in:
GitlabInternalEventsCli::Helpers
Defined in:
lib/gitlab_internal_events_cli/helpers/cli_inputs.rb

Instance Method Summary collapse

Instance Method Details

#disableable_option(value:, disabled:, name: nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 126

def disableable_option(value:, disabled:, name: nil)
  should_disable = yield
  name ||= value

  {
    value: value,
    name: (should_disable ? format_help(name) : name),
    disabled: (disabled if should_disable)
  }
end

#disabled_format_callbackObject

For use when menu options are disabled by being grayed out



110
111
112
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 110

def disabled_format_callback
  proc { |menu| menu.symbols(cross: format_help('')) }
end

#filter_opts(header_size: nil) ⇒ Object

Accepts a number of lines occupied by text, so remaining screen real estate can be filled with select options



88
89
90
91
92
93
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 88

def filter_opts(header_size: nil)
  {
    filter: true,
    per_page: header_size ? [(window_height - header_size), 10].max : 30
  }
end

#format_disabled_options_as_dividers(select_menu) ⇒ Object

Styling all disabled options in a menu without indication of being a selectable option

Parameters:

  • select_menu (TTY::Prompt)


105
106
107
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 105

def format_disabled_options_as_dividers(select_menu)
  select_menu.symbols(cross: '')
end

#input_optional_text(value) ⇒ Object

Help text to use with optional, multiline cli#ask prompts. Otherwise, prefer #prompt_for_text.



122
123
124
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 122

def input_optional_text(value)
  format_help("(enter to #{value ? 'submit' : 'skip'})")
end

#input_optsObject



57
58
59
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 57

def input_opts
  { prefix: format_prompt('Input text: ') }
end

#input_required_textObject

Help text to use with required, multiline cli#ask prompts. Otherwise, prefer #prompt_for_text.



116
117
118
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 116

def input_required_text
  format_help('(leave blank for help)')
end

#multiselect_optsObject

Provide to cli#multiselect as kwargs for consistent style/ux



77
78
79
80
81
82
83
84
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 77

def multiselect_opts
  {
    **select_opts,
    prefix: format_prompt('Select multiple: '),
    min: 1,
    help: '(Space to select, Enter to submit, ↑/↓/←/→ to move, Ctrl+A|R to select all|none, letters to filter)'
  }
end

#prompt_for_array_selection(message, choices, default = nil, **opts, &formatter) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 7

def prompt_for_array_selection(message, choices, default = nil, **opts, &formatter)
  formatter ||= ->(choice) { choice.sort.join(', ') }

  choices = choices.map do |choice|
    { name: formatter.call(choice), value: choice }
  end

  cli.select(message, choices, **select_opts, **opts) do |menu|
    menu.enum '.'
    menu.default formatter.call(default) if default
  end
end

#prompt_for_text(message, value = nil, multiline: false, **opts) {|TTY::Prompt::Question| ... } ⇒ String?

Prompts the user to input text. Prefer this over calling cli#ask directly (so styling is consistent).

Parameters:

  • message (String)

    a single line prompt/question or last line of a prompt

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

    prepopulated as the answer which user can accept/modify

  • multiline (Hash) (defaults to: false)

    a customizable set of options

Options Hash (multiline:):

  • indicates (Boolean)

    that any help text or prompt prefix will be printed on another line before calling #prompt_for_text –> ex) see MetricDefiner#prompt_for_description

Yields:

  • (TTY::Prompt::Question)

Returns:

  • (String, nil)

    user-provided text

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 30

def prompt_for_text(message, value = nil, multiline: false, **opts)
  prompt = message.dup # mutable for concat in #ask callback

  options = { **input_opts, **opts }
  value ||= options.delete(:value)
  options.delete(:prefix) if multiline

  cli.ask(prompt, **options) do |q|
    q.value(value) if value

    yield q if block_given?

    if multiline
      # wrap error messages so they render nicely with prompt
      q.messages.each do |key, error|
        closing_text = "\n#{format_error('<<|')}" if error.lines.length > 1

        q.messages[key] = [error, closing_text, "\n\n\n"].join
      end
    else
      # append help text only if this line includes the formatted 'prompt' prefix,
      # otherwise depend on the caller to print the help text if needed
      prompt.concat(" #{q.required ? input_required_text : input_optional_text(value)}")
    end
  end
end

#select_option_divider(text) ⇒ Object

Creates divider to be passed to a select or multiselect as a menu item. Use with #format_disabled_options_as_dividers for best formatting.



98
99
100
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 98

def select_option_divider(text)
  { name: "-- #{text} --", value: nil, disabled: '' }
end

#select_optsObject

Provide to cli#select as kwargs for consistent style/ux



66
67
68
69
70
71
72
73
74
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 66

def select_opts
  {
    prefix: format_prompt('Select one: '),
    cycle: true,
    show_help: :always,
    # Strip colors so #format_selection is applied uniformly
    active_color: ->(choice) { format_selection(clear_format(choice)) }
  }
end

#yes_no_optsObject



61
62
63
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 61

def yes_no_opts
  { prefix: format_prompt('Yes/No: ') }
end