Module: Snibbets::Menu
- Defined in:
- lib/snibbets/menu.rb
Overview
Menu functions
Class Method Summary collapse
-
.console_menu(res, title, filename, query: nil) ⇒ Object
Generate a numbered menu, items passed must have a title property.
- .find_query_in_options(filename, res, query) ⇒ Object
- .fzf_menu(executable, res, title, query, filename) ⇒ Object
- .gum_menu(executable, res, title, query, filename) ⇒ Object
- .menu(res, filename: nil, title: 'Select one', query: nil) ⇒ Object
- .remove_items_without_query(filename, res, query) ⇒ Object
Class Method Details
.console_menu(res, title, filename, query: nil) ⇒ Object
Generate a numbered menu, items passed must have a title property
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 117 118 |
# File 'lib/snibbets/menu.rb', line 74 def (res, title, filename, query: nil) unless query.nil? || query.empty? res = remove_items_without_query(filename, res, query) return res[0] if res.count == 1 end if res.count.zero? warn 'No matches found' if Snibbets.[:interactive] Process.exit 1 end stty_save = `stty -g`.chomp trap('INT') do system('stty', stty_save) Process.exit 1 end # Generate a numbered menu, items passed must have a title property('INT') { system('stty', stty_save); exit } counter = 1 $stderr.puts res.each do |m| $stderr.printf("%<counter>2d) %<title>s\n", counter: counter, title: m['title']) counter += 1 end $stderr.puts begin $stderr.printf(title.sub(/:?$/, ': '), res.length) while (line = Readline.readline('', true)) unless line =~ /^[0-9]/ system('stty', stty_save) # Restore exit end line = line.to_i return res[line - 1] if line.positive? && line <= res.length warn 'Out of range' return (res, title, filename, query: query) end rescue Interrupt system('stty', stty_save) exit end end |
.find_query_in_options(filename, res, query) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/snibbets/menu.rb', line 7 def (filename, res, query) = res.map { |m| "#{filename} #{m['title']}" } words = query.split(/ +/) words.delete_if { |word| .none? { |o| o =~ /#{word}/i } } words.map(&:downcase).join(' ') end |
.fzf_menu(executable, res, title, query, filename) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/snibbets/menu.rb', line 43 def (executable, res, title, query, filename) = res.dup unless query.nil? || query.empty? res = remove_items_without_query(filename, res, query) return res[0] if res.count == 1 end res = if res.count.zero? = res.map { |m| "#{filename}: #{m['title']}" } q = query.nil? ? '' : (filename, res, query) args = [ "--height=#{.count + 3}", %(--prompt="#{title} > "), '-1', %(--header="#{filename}"), # '--header-first', '--reverse', '--no-info', %(--query="#{q}"), '--tac' ] selection = `echo #{Shellwords.escape(.join("\n"))} | #{executable} #{args.join(' ')}`.strip Process.exit 1 if selection.empty? result = res.select { |m| m['title'] == selection.sub(/^.*?: /, '') } result[0] end |
.gum_menu(executable, res, title, query, filename) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/snibbets/menu.rb', line 22 def (executable, res, title, query, filename) unless query.nil? || query.empty? res = remove_items_without_query(filename, res, query) return res[0] if res.count == 1 end if res.count.zero? warn 'No matches found' if Snibbets.[:interactive] Process.exit 1 end = res.map { |m| m['title'] } puts title args = ["--height=#{.count}"] selection = `echo #{Shellwords.escape(.join("\n"))} | #{executable} filter #{args.join(' ')}`.strip Process.exit 1 if selection.empty? res.select { |m| m['title'] =~ /#{Regexp.escape(selection)}/ }[0] end |
.menu(res, filename: nil, title: 'Select one', query: nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/snibbets/menu.rb', line 120 def (res, filename: nil, title: 'Select one', query: nil) = Snibbets.[:menus] query&. fzf = TTY::Which.which('fzf') gum = TTY::Which.which('gum') case when /fzf/ return (fzf, res, title, query, filename) unless fzf.empty? when /gum/ return (gum, res, title, query, filename) unless gum.empty? when /console/ return (res, title, filename, query: query) end return (fzf, res, title, query, filename) unless fzf.empty? return (gum, res, title, query, filename) unless gum.empty? (res, title, filename, query: query) end |
.remove_items_without_query(filename, res, query) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/snibbets/menu.rb', line 14 def remove_items_without_query(filename, res, query) q = (filename, res, query).split(/ /) res.delete_if do |opt| q.none? { |word| "#{filename} #{opt['title']}" =~ /#{word}/i } end res end |