Class: Legion::TTY::Components::CommandPalette

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/command_palette.rb

Constant Summary collapse

COMMANDS =
%w[/help /quit /clear /model /session /cost /export /tools
/dashboard /hotkeys /save /load /sessions /system /delete /plan
/palette /extensions /config].freeze
SCREENS =
%w[chat dashboard extensions config].freeze

Instance Method Summary collapse

Constructor Details

#initialize(session_store: nil) ⇒ CommandPalette

Returns a new instance of CommandPalette.



13
14
15
# File 'lib/legion/tty/components/command_palette.rb', line 13

def initialize(session_store: nil)
  @session_store = session_store
end

Instance Method Details

#entriesObject



17
18
19
20
21
22
23
24
# File 'lib/legion/tty/components/command_palette.rb', line 17

def entries
  all = COMMANDS.map { |cmd| { label: cmd, category: 'Commands' } }
  SCREENS.each { |s| all << { label: s, category: 'Screens' } }
  @session_store&.list&.each do |s|
    all << { label: "/load #{s[:name]}", category: 'Sessions' }
  end
  all
end

#search(query) ⇒ Object



26
27
28
29
30
31
# File 'lib/legion/tty/components/command_palette.rb', line 26

def search(query)
  return entries if query.nil? || query.empty?

  q = query.downcase
  entries.select { |e| e[:label].downcase.include?(q) }
end

#select_with_prompt(output: $stdout) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/legion/tty/components/command_palette.rb', line 33

def select_with_prompt(output: $stdout)
  require 'tty-prompt'
  prompt = ::TTY::Prompt.new(output: output)
  choices = entries.map { |e| { name: "#{e[:label]} (#{e[:category]})", value: e[:label] } }
  prompt.select('Command:', choices, filter: true, per_page: 15)
rescue ::TTY::Reader::InputInterrupt, Interrupt
  nil
end