Module: Ollama::Utils::Chooser

Includes:
SearchUI, Term::ANSIColor
Defined in:
lib/ollama/utils/chooser.rb

Class Method Summary collapse

Class Method Details

.choose(entries, prompt: 'Search? %s', return_immediately: false) ⇒ Object

The choose method presents a list of entries and prompts the user for input, allowing them to select one entry based on their input.

Examples:

choose(['entry1', 'entry2'], prompt: 'Choose an option:')


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ollama/utils/chooser.rb', line 22

def choose(entries, prompt: 'Search? %s', return_immediately: false)
  if return_immediately && entries.size <= 1
    return entries.first
  end
  entry = Search.new(
    prompt:,
    match: -> answer {
      matcher = Amatch::PairDistance.new(answer.downcase)
      matches = entries.map { |n| [ n, -matcher.similar(n.to_s.downcase) ] }.
        select { |_, s| s < 0 }.sort_by(&:last).map(&:first)
      matches.empty? and matches = entries
      matches.first(Tins::Terminal.lines - 1)
    },
    query: -> _answer, matches, selector {
      matches.each_with_index.map { |m, i|
        i == selector ? "#{blue{?⮕}} #{on_blue{m}}" : "  #{m.to_s}"
      } * ?\n
    },
    found: -> _answer, matches, selector {
      matches[selector]
    },
    output: STDOUT
  ).start
  if entry
    entry
  else
    print clear_screen, move_home
    nil
  end
end