Module: Graphina::Panel::Chooser

Extended by:
SearchUI, Term::ANSIColor
Defined in:
lib/graphina/panel/chooser.rb

Overview

A module that provides functionality for choosing panels from a configuration

The Panel::Chooser module implements a search-based interface for selecting from available panel configurations. It integrates with terminal-based user interfaces to allow interactive selection of panels with fuzzy matching and visual feedback during the selection process

Class Method Summary collapse

Class Method Details

.choose(panels) ⇒ Graphina::Panel?

The choose method presents an interactive selection interface for available panels

This method implements a search-based user interface that allows users to select from a collection of predefined panel configurations. It utilizes fuzzy matching to find relevant panel names and provides visual feedback during the selection process with a colored highlight for the currently selected option

Parameters:

  • panels (Hash)

    a hash (convertible) containing panel configurations indexed by name

Returns:

  • (Graphina::Panel, nil)

    returns a new Panel instance configured with the selected panel’s settings, or nil if no panel was chosen



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphina/panel/chooser.rb', line 29

def choose(panels)
  panel_names = panels.to_h.keys
  panel = Search.new(
    prompt: 'Choose a panel? ',
    match: -> answer {
      matcher = Amatch::PairDistance.new(answer.downcase)
      matches = panel_names.map { |n| [ n, -matcher.similar(n.to_s.downcase) ] }.
        select { |_, s| s < 0 }.sort_by(&:last).map(&:first)
      matches.empty? and matches = panel_names
      matches.first(Tins::Terminal.lines - 1)
    },
    query: -> _answer, matches, selector {
      matches.each_with_index.
      map { |m, i| i == selector ? on_blue { m } : m } * ?\n
    },
    found: -> _answer, matches, selector {
      matches[selector]
    },
    output: STDOUT
  ).start
  if panel
    Graphina::Panel.new(panels[panel])
  else
    puts
  end
end