Module: GitlabInternalEventsCli::Helpers::CliInputs
- Included in:
- GitlabInternalEventsCli::Helpers
- Defined in:
- lib/gitlab_internal_events_cli/helpers/cli_inputs.rb
Instance Method Summary collapse
- #disableable_option(value:, disabled:, name: nil) ⇒ Object
-
#disabled_format_callback ⇒ Object
For use when menu options are disabled by being grayed out.
-
#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.
-
#format_disabled_options_as_dividers(select_menu) ⇒ Object
Styling all disabled options in a menu without indication of being a selectable option.
-
#input_optional_text(value) ⇒ Object
Help text to use with optional, multiline cli#ask prompts.
- #input_opts ⇒ Object
-
#input_required_text ⇒ Object
Help text to use with required, multiline cli#ask prompts.
-
#multiselect_opts ⇒ Object
Provide to cli#multiselect as kwargs for consistent style/ux.
- #prompt_for_array_selection(message, choices, default = nil, **opts, &formatter) ⇒ Object
-
#prompt_for_text(message, value = nil, multiline: false, **opts) {|TTY::Prompt::Question| ... } ⇒ String?
Prompts the user to input text.
-
#select_option_divider(text) ⇒ Object
Creates divider to be passed to a select or multiselect as a menu item.
-
#select_opts ⇒ Object
Provide to cli#select as kwargs for consistent style/ux.
- #yes_no_opts ⇒ Object
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_callback ⇒ Object
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 { || .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
105 106 107 |
# File 'lib/gitlab_internal_events_cli/helpers/cli_inputs.rb', line 105 def () .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_opts ⇒ Object
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_text ⇒ Object
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_opts ⇒ Object
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(, choices, default = nil, **opts, &formatter) formatter ||= ->(choice) { choice.sort.join(', ') } choices = choices.map do |choice| { name: formatter.call(choice), value: choice } end cli.select(, choices, **select_opts, **opts) do || .enum '.' .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).
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(, value = nil, multiline: false, **opts) prompt = .dup # mutable for concat in #ask callback = { **input_opts, **opts } value ||= .delete(:value) .delete(:prefix) if multiline cli.ask(prompt, **) do |q| q.value(value) if value yield q if block_given? if multiline # wrap error messages so they render nicely with prompt q..each do |key, error| closing_text = "\n#{format_error('<<|')}" if error.lines.length > 1 q.[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_opts ⇒ Object
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_opts ⇒ Object
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 |