Sha256: 68419381ed3bf1616078a110536b5bfe515d4afea13cbdab1d81777f76dfbe01

Contents?: true

Size: 969 Bytes

Versions: 8

Compression:

Stored size: 969 Bytes

Contents

require 'amatch'
require 'search_ui'
require 'term/ansicolor'

module Ollama::Utils::Chooser
  include SearchUI
  include Term::ANSIColor

  module_function

  def choose(entries, prompt: 'Search? %s')
    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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ollama-ruby-0.11.0 lib/ollama/utils/chooser.rb
ollama-ruby-0.10.0 lib/ollama/utils/chooser.rb
ollama-ruby-0.9.3 lib/ollama/utils/chooser.rb
ollama-ruby-0.9.2 lib/ollama/utils/chooser.rb
ollama-ruby-0.9.1 lib/ollama/utils/chooser.rb
ollama-ruby-0.9.0 lib/ollama/utils/chooser.rb
ollama-ruby-0.8.0 lib/ollama/utils/chooser.rb
ollama-ruby-0.7.0 lib/ollama/utils/chooser.rb