Class: Legion::TTY::Components::CommandPalette
- Inherits:
-
Object
- Object
- Legion::TTY::Components::CommandPalette
- Includes:
- Logging::Helper
- 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
- #entries ⇒ Object
-
#initialize(session_store: nil) ⇒ CommandPalette
constructor
A new instance of CommandPalette.
- #search(query) ⇒ Object
- #select_with_prompt(output: $stdout) ⇒ Object
Constructor Details
#initialize(session_store: nil) ⇒ CommandPalette
Returns a new instance of CommandPalette.
17 18 19 |
# File 'lib/legion/tty/components/command_palette.rb', line 17 def initialize(session_store: nil) @session_store = session_store end |
Instance Method Details
#entries ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/legion/tty/components/command_palette.rb', line 21 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
30 31 32 33 34 35 |
# File 'lib/legion/tty/components/command_palette.rb', line 30 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/legion/tty/components/command_palette.rb', line 37 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 => e log.debug { "command palette cancelled: #{e.}" } nil end |