Module: Snibbets::Menu

Defined in:
lib/snibbets/menu.rb

Overview

Menu functions

Class Method Summary collapse

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 console_menu(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.options[: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 console_menu(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 find_query_in_options(filename, res, query)
  options = res.map { |m| "#{filename} #{m['title']}" }
  words = query.split(/ +/)
  words.delete_if { |word| options.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 fzf_menu(executable, res, title, query, filename)
  orig_options = res.dup
  unless query.nil? || query.empty?
    res = remove_items_without_query(filename, res, query)
    return res[0] if res.count == 1
  end

  res = orig_options if res.count.zero?

  options = res.map { |m| "#{filename}: #{m['title']}" }
  q = query.nil? ? '' : find_query_in_options(filename, res, query)
  args = [
    "--height=#{options.count + 3}",
    %(--prompt="#{title} > "),
    '-1',
    %(--header="#{filename}"),
    # '--header-first',
    '--reverse',
    '--no-info',
    %(--query="#{q}"),
    '--tac'
  ]
  selection = `echo #{Shellwords.escape(options.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 gum_menu(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.options[:interactive]
    Process.exit 1
  end

  options = res.map { |m| m['title'] }

  puts title
  args = ["--height=#{options.count}"]
  selection = `echo #{Shellwords.escape(options.join("\n"))} | #{executable} filter #{args.join(' ')}`.strip
  Process.exit 1 if selection.empty?

  res.select { |m| m['title'] =~ /#{Regexp.escape(selection)}/ }[0]
end


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 menu(res, filename: nil, title: 'Select one', query: nil)
  menu_opt = Snibbets.options[:menus]
  query&.remove_spotlight_tags!

  fzf = TTY::Which.which('fzf')
  gum = TTY::Which.which('gum')

  case menu_opt
  when /fzf/
    return fzf_menu(fzf, res, title, query, filename) unless fzf.empty?
  when /gum/
    return gum_menu(gum, res, title, query, filename) unless gum.empty?
  when /console/
    return console_menu(res, title, filename, query: query)
  end

  return fzf_menu(fzf, res, title, query, filename) unless fzf.empty?

  return gum_menu(gum, res, title, query, filename) unless gum.empty?

  console_menu(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 = find_query_in_options(filename, res, query).split(/ /)
  res.delete_if do |opt|
    q.none? { |word| "#{filename} #{opt['title']}" =~ /#{word}/i }
  end
  res
end