-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_registry.go
More file actions
301 lines (250 loc) · 7.9 KB
/
command_registry.go
File metadata and controls
301 lines (250 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"sort"
"strings"
"unicode"
)
// CommandInfo holds metadata about a command
type CommandInfo struct {
Name string // Primary command name
Aliases []string // Alternative names for the command
Description string // Short description of what the command does
Usage string // Usage syntax (e.g., "open <context|folder>")
Category string // Category for grouping (e.g., "Navigation", "Data Management")
Examples []string // Usage examples
}
// CommandRegistry manages commands with their metadata
type CommandRegistry[T any] struct {
commands map[string]T // Command name -> function
info map[string]CommandInfo // Command name -> metadata
aliases map[string]string // Alias -> primary command name
}
// NewCommandRegistry creates a new command registry
func NewCommandRegistry[T any]() *CommandRegistry[T] {
return &CommandRegistry[T]{
commands: make(map[string]T),
info: make(map[string]CommandInfo),
aliases: make(map[string]string),
}
}
// Register adds a command with its metadata to the registry
func (r *CommandRegistry[T]) Register(name string, fn T, info CommandInfo) {
// Set the primary name if not already set
if info.Name == "" {
info.Name = name
}
// Store the command function and info
r.commands[name] = fn
r.info[name] = info
// Register aliases
for _, alias := range info.Aliases {
r.commands[alias] = fn
r.aliases[alias] = name // Map alias back to primary name
}
}
// GetFunctionMap returns the command map in the format expected by existing code
func (r *CommandRegistry[T]) GetFunctionMap() map[string]T {
return r.commands
}
// GetCommandInfo returns the metadata for a command (by name or alias)
func (r *CommandRegistry[T]) GetCommandInfo(name string) (CommandInfo, bool) {
// Check if it's an alias first
if primaryName, isAlias := r.aliases[name]; isAlias {
name = primaryName
}
info, exists := r.info[name]
return info, exists
}
// GetAllCommands returns all commands organized by category
func (r *CommandRegistry[T]) GetAllCommands() map[string][]CommandInfo {
categories := make(map[string][]CommandInfo)
// Group commands by category, avoiding duplicates from aliases
for _, info := range r.info {
categories[info.Category] = append(categories[info.Category], info)
}
// Sort commands within each category
for category := range categories {
sort.Slice(categories[category], func(i, j int) bool {
return categories[category][i].Name < categories[category][j].Name
})
}
return categories
}
// GetCommandNames returns all primary command names (no aliases)
func (r *CommandRegistry[T]) GetCommandNames() []string {
var names []string
for name := range r.info {
names = append(names, name)
}
sort.Strings(names)
return names
}
// SuggestCommand returns command suggestions for a typo using simple fuzzy matching
func (r *CommandRegistry[T]) SuggestCommand(input string) []string {
input = strings.ToLower(input)
var suggestions []string
// First pass: exact prefix matches
for name := range r.info {
if strings.HasPrefix(strings.ToLower(name), input) {
suggestions = append(suggestions, name)
}
}
// Second pass: contains matches if no prefix matches found
if len(suggestions) == 0 {
for name := range r.info {
if strings.Contains(strings.ToLower(name), input) {
suggestions = append(suggestions, name)
}
}
}
// Third pass: similar commands (edit distance or similar)
if len(suggestions) == 0 {
for name := range r.info {
if levenshteinDistance(strings.ToLower(input), strings.ToLower(name)) <= 2 {
suggestions = append(suggestions, name)
}
}
}
sort.Strings(suggestions)
return suggestions
}
// FormatCommandHelp returns formatted help text for a specific command
func (r *CommandRegistry[T]) FormatCommandHelp(name string) string {
info, exists := r.GetCommandInfo(name)
if !exists {
return fmt.Sprintf("Command '%s' not found", name)
}
var help strings.Builder
help.WriteString(fmt.Sprintf("Command: %s\n", info.Name))
if len(info.Aliases) > 0 {
help.WriteString(fmt.Sprintf("Aliases: %s\n", strings.Join(info.Aliases, ", ")))
}
if info.Usage != "" {
help.WriteString(fmt.Sprintf("Usage: %s\n", info.Usage))
}
if info.Description != "" {
help.WriteString(fmt.Sprintf("Description: %s\n", info.Description))
}
if len(info.Examples) > 0 {
help.WriteString("Examples:\n")
for _, example := range info.Examples {
help.WriteString(fmt.Sprintf(" %s\n", example))
}
}
return help.String()
}
// FormatCategoryHelp returns formatted help for all commands in a category
func (r *CommandRegistry[T]) FormatCategoryHelp(category string) string {
allCommands := r.GetAllCommands()
commands, exists := allCommands[category]
if !exists {
return fmt.Sprintf("Category '%s' not found", category)
}
var help strings.Builder
help.WriteString(fmt.Sprintf("%s Commands:\n\n", category))
for _, cmd := range commands {
help.WriteString(fmt.Sprintf(" %-15s %s\n", cmd.Name, cmd.Description))
if len(cmd.Aliases) > 0 {
help.WriteString(fmt.Sprintf(" %-15s (aliases: %s)\n", "", strings.Join(cmd.Aliases, ", ")))
}
}
return help.String()
}
// FormatAllHelp returns formatted help for all commands, organized by category
func (r *CommandRegistry[T]) FormatAllHelp() string {
var help strings.Builder
help.WriteString("# Available Commands:\n\n")
categories := r.GetAllCommands()
// Sort categories for consistent output
var categoryNames []string
for category := range categories {
categoryNames = append(categoryNames, category)
}
sort.Strings(categoryNames)
for _, category := range categoryNames {
commands := categories[category]
help.WriteString(fmt.Sprintf("## %s:\n", category))
for _, cmd := range commands {
aliases := ""
if len(cmd.Aliases) > 0 {
aliases = fmt.Sprintf(" (%s)", strings.Join(cmd.Aliases, ", "))
}
examples := ""
if len(cmd.Examples) > 0 {
examples = fmt.Sprintf(" *%s*", strings.Join(cmd.Examples, ", "))
}
help.WriteString(fmt.Sprintf("- `%-14s`%s - %s %s\n", cmd.Name, aliases, cmd.Description, examples))
}
help.WriteString("\n")
}
help.WriteString("Use ':help <command>' for detailed help on a specific command.\n")
help.WriteString("Use ':help <category>' for commands in a specific category.\n")
return help.String()
}
// Simple Levenshtein distance calculation for command suggestions
func levenshteinDistance(s1, s2 string) int {
if len(s1) == 0 {
return len(s2)
}
if len(s2) == 0 {
return len(s1)
}
matrix := make([][]int, len(s1)+1)
for i := range matrix {
matrix[i] = make([]int, len(s2)+1)
matrix[i][0] = i
}
for j := 0; j <= len(s2); j++ {
matrix[0][j] = j
}
for i := 1; i <= len(s1); i++ {
for j := 1; j <= len(s2); j++ {
cost := 0
if s1[i-1] != s2[j-1] {
cost = 1
}
matrix[i][j] = min(
matrix[i-1][j]+1, // deletion
matrix[i][j-1]+1, // insertion
matrix[i-1][j-1]+cost, // substitution
)
}
}
return matrix[len(s1)][len(s2)]
}
func min(a, b, c int) int {
if a < b && a < c {
return a
}
if b < c {
return b
}
return c
}
// keyToDisplayName converts key sequences to human-readable format for help display
func keyToDisplayName(key string) string {
if strings.HasPrefix(key, leader) {
return "<leader>" + key[len(leader):]
}
var representation []string
for _, b := range []byte(key) {
representation = append(representation, representByte(b))
}
return strings.Join(representation, " ")
}
func representByte(b byte) string {
// Check if it's a standard control code (Ctrl-A to Ctrl-Z)
if b >= 1 && b <= 26 {
char := byte('A' - 1 + b)
return fmt.Sprintf("Ctrl-%c", char)
}
// Check if it's a printable character using the unicode package.
// This is more robust than checking ASCII ranges.
if unicode.IsPrint(rune(b)) {
return string(b)
}
// For any other non-printable character (like Null, Tab, etc.),
// return a generic but clear hex representation.
return fmt.Sprintf("<0x%02X>", b)
}