Module: Doing::PromptChoose
- Included in:
- Prompt
- Defined in:
- lib/doing/prompt/choose.rb
Overview
Methods for creating interactive menus of options and items
Instance Method Summary collapse
-
#choose_from(options, prompt: 'Make a selection: ', multiple: false, sorted: true, fzf_args: []) ⇒ String
Generate a menu of options and allow user selection.
-
#choose_from_items(items, **opt) ⇒ Object
Create an interactive menu to select from a set of Items.
Instance Method Details
#choose_from(options, prompt: 'Make a selection: ', multiple: false, sorted: true, fzf_args: []) ⇒ String
Generate a menu of options and allow user selection
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/doing/prompt/choose.rb', line 17 def choose_from(, prompt: 'Make a selection: ', multiple: false, sorted: true, fzf_args: []) return nil unless $stdout.isatty # fzf_args << '-1' # User is expecting a menu, and even if only one it serves as confirmation default_args = [] default_args << %(--prompt="#{prompt}") default_args << "--height=#{.count + 2}" default_args << '--info=inline' default_args << '--multi' if multiple header = "esc: cancel,#{multiple ? ' tab: multi-select, ctrl-a: select all,' : ''} return: confirm" default_args << %(--header="#{header}") default_args.concat(fzf_args) .sort! if sorted res = `echo #{Shellwords.escape(.join("\n"))}|#{fzf} #{default_args.join(' ')}` return false if res.strip.size.zero? res end |
#choose_from_items(items, **opt) ⇒ Object
Create an interactive menu to select from a set of Items
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/doing/prompt/choose.rb', line 53 def choose_from_items(items, **opt) return items unless $stdout.isatty return nil unless items.count.positive? case_sensitive = opt.fetch(:case, :smart).normalize_case header = opt.fetch(:header, 'Arrows: navigate, tab: mark for selection, ctrl-a: select all, enter: commit') prompt = opt.fetch(:prompt, 'Select entries to act on > ') query = opt.fetch(:query) { opt.fetch(:search, '') } include_section = opt.fetch(:include_section, false) pad = items.length.to_s.length = items.map.with_index do |item, i| out = [ format("%#{pad}d", i), ') ', format('%16s', item.date.strftime('%Y-%m-%d %H:%M')), ' | ', item.title ] if include_section out.concat([ ' (', item.section, ') ' ]) end out.join('') end fzf_args = [ %(--header="#{header}"), %(--prompt="#{prompt.sub(/ *$/, ' ')}"), opt.fetch(:multiple) ? '--multi' : '--no-multi', '-0', '--bind ctrl-a:select-all', %(-q "#{query}"), '--info=inline' ] fzf_args.push('-1') unless opt.fetch(:show_if_single, false) fzf_args << case case_sensitive when :sensitive '+i' when :ignore '-i' end fzf_args << '-e' if opt.fetch(:exact, false) unless opt.fetch(:menu) raise InvalidArgument, "Can't skip menu when no query is provided" unless query && !query.empty? fzf_args.concat([%(--filter="#{query}"), opt.fetch(:sort) ? '' : '--no-sort']) end res = `echo #{Shellwords.escape(.join("\n"))}|#{fzf} #{fzf_args.join(' ')}` selected = [] res.split(/\n/).each do |item| idx = item.match(/^ *(\d+)\)/)[1].to_i selected.push(items[idx]) end opt.fetch(:multiple) ? selected : selected[0] end |