|
5 | 5 | "os" |
6 | 6 | "os/exec" |
7 | 7 | "path/filepath" |
| 8 | + "sort" |
8 | 9 | "strings" |
9 | 10 | ) |
10 | 11 |
|
@@ -79,6 +80,54 @@ func registerDisplayNavCommands(r *Registry) { |
79 | 80 | } |
80 | 81 |
|
81 | 82 | func cmdHelp(ctx Context) Result { |
| 83 | + // /help <command> — show description for a specific command. |
| 84 | + if ctx.HasArg(1) && ctx.Registry != nil { |
| 85 | + name := ctx.Arg(1) |
| 86 | + if !strings.HasPrefix(name, "/") { |
| 87 | + name = "/" + name |
| 88 | + } |
| 89 | + cmd, ok := ctx.Registry.Lookup(name) |
| 90 | + if !ok { |
| 91 | + fmt.Printf("Unknown command: %s\n", name) |
| 92 | + return Result{Handled: true} |
| 93 | + } |
| 94 | + fmt.Printf("%s%s%s — %s\n", ColorBold, cmd.Name, ColorReset, cmd.Description) |
| 95 | + if len(cmd.Aliases) > 0 { |
| 96 | + fmt.Printf(" aliases: %s\n", strings.Join(cmd.Aliases, ", ")) |
| 97 | + } |
| 98 | + fmt.Println() |
| 99 | + return Result{Handled: true} |
| 100 | + } |
| 101 | + |
| 102 | + // Dynamic help generated from the registry when available. |
| 103 | + if ctx.Registry != nil { |
| 104 | + byCategory := ctx.Registry.ByCategory() |
| 105 | + // Sort categories for stable output. |
| 106 | + cats := make([]string, 0, len(byCategory)) |
| 107 | + for c := range byCategory { |
| 108 | + cats = append(cats, c) |
| 109 | + } |
| 110 | + sort.Strings(cats) |
| 111 | + |
| 112 | + fmt.Printf("\n%sAvailable commands%s\n\n", ColorBold, ColorReset) |
| 113 | + for _, cat := range cats { |
| 114 | + cmds := byCategory[cat] |
| 115 | + sort.Slice(cmds, func(i, j int) bool { return cmds[i].Name < cmds[j].Name }) |
| 116 | + fmt.Printf("%s%s%s\n", ColorDim, strings.ToUpper(cat), ColorReset) |
| 117 | + for _, cmd := range cmds { |
| 118 | + alias := "" |
| 119 | + if len(cmd.Aliases) > 0 { |
| 120 | + alias = fmt.Sprintf(" %s(%s)%s", ColorDim, strings.Join(cmd.Aliases, ", "), ColorReset) |
| 121 | + } |
| 122 | + fmt.Printf(" %-22s %s%s\n", cmd.Name, cmd.Description, alias) |
| 123 | + } |
| 124 | + fmt.Println() |
| 125 | + } |
| 126 | + fmt.Printf("Tip: %s/help <command>%s for details on any command.\n\n", ColorDim, ColorReset) |
| 127 | + return Result{Handled: true} |
| 128 | + } |
| 129 | + |
| 130 | + // Fallback static help when registry is unavailable (e.g., in tests). |
82 | 131 | fmt.Print(` |
83 | 132 | Available commands: |
84 | 133 | /help — show this help |
|
0 commit comments