Class: SwarmCLI::V3::CommandCompleter
- Inherits:
-
Object
- Object
- SwarmCLI::V3::CommandCompleter
- Defined in:
- lib/swarm_cli/v3/command_completer.rb
Overview
Command completion logic for slash commands.
CommandCompleter provides methods to extract command targets from text buffers and find matching slash commands. It has no knowledge of the UI or display.
Constant Summary collapse
- COMMANDS =
Available slash commands
[ "/help", "/clear", "/memory", "/defrag", "/queue", "/refine", "/reboot", "/exit", "/quit", ].freeze
Class Method Summary collapse
-
.extract_command_word(buffer, cursor) ⇒ Array<String>?
Extract the command at cursor position.
-
.find_matches(target, max: 5, skills: []) ⇒ Array<String>
Find matching commands for a query (with / prefix).
Class Method Details
.extract_command_word(buffer, cursor) ⇒ Array<String>?
Extract the command at cursor position.
Searches backward from cursor to find a ‘/` at the start of the line, then extracts from that `/` to the cursor position.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/swarm_cli/v3/command_completer.rb', line 58 def extract_command_word(buffer, cursor) # Command must be at start of line return unless buffer.start_with?("/") # Find end of command after cursor (next space or end of buffer) target_end = cursor target_end += 1 while target_end < buffer.length && buffer[target_end] !~ /\s/ pre = "" target = buffer[0...cursor] post = buffer[cursor...target_end] || "" [pre, target, post] end |
.find_matches(target, max: 5, skills: []) ⇒ Array<String>
Find matching commands for a query (with / prefix).
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/swarm_cli/v3/command_completer.rb', line 96 def find_matches(target, max: 5, skills: []) return [] unless target.start_with?("/") query = target.downcase # Combine built-in commands with skill names skill_commands = skills.map { |name| "/#{name}" } all_commands = COMMANDS + skill_commands # Find commands that start with the query matches = all_commands.select { |cmd| cmd.downcase.start_with?(query) } # Sort by length (shorter = more specific) matches.sort_by(&:length).first(max) end |