Explorar o código

fix(tui): help dialog sorting

adamdottv hai 8 meses
pai
achega
1d0bfc2b2a

+ 14 - 6
packages/tui/internal/commands/command.go

@@ -41,14 +41,12 @@ func (c Command) Keys() []string {
 
 type CommandRegistry map[CommandName]Command
 
-func (r CommandRegistry) Matches(msg tea.KeyPressMsg, leader bool) []Command {
-	var matched []Command
+func (r CommandRegistry) Sorted() []Command {
+	var commands []Command
 	for _, command := range r {
-		if command.Matches(msg, leader) {
-			matched = append(matched, command)
-		}
+		commands = append(commands, command)
 	}
-	slices.SortFunc(matched, func(a, b Command) int {
+	slices.SortFunc(commands, func(a, b Command) int {
 		if a.Name == AppExitCommand {
 			return 1
 		}
@@ -57,6 +55,16 @@ func (r CommandRegistry) Matches(msg tea.KeyPressMsg, leader bool) []Command {
 		}
 		return strings.Compare(string(a.Name), string(b.Name))
 	})
+	return commands
+}
+
+func (r CommandRegistry) Matches(msg tea.KeyPressMsg, leader bool) []Command {
+	var matched []Command
+	for _, command := range r.Sorted() {
+		if command.Matches(msg, leader) {
+			matched = append(matched, command)
+		}
+	}
 	return matched
 }
 

+ 2 - 2
packages/tui/internal/components/dialog/help.go

@@ -15,7 +15,7 @@ type helpDialog struct {
 	width    int
 	height   int
 	modal    *modal.Modal
-	commands commands.CommandRegistry
+	commands []commands.Command
 }
 
 func (h *helpDialog) Init() tea.Cmd {
@@ -80,7 +80,7 @@ type HelpDialog interface {
 	layout.Modal
 }
 
-func NewHelpDialog(commands commands.CommandRegistry) HelpDialog {
+func NewHelpDialog(commands []commands.Command) HelpDialog {
 	return &helpDialog{
 		commands: commands,
 		modal:    modal.New(),

+ 1 - 1
packages/tui/internal/tui/tui.go

@@ -284,7 +284,7 @@ func (a appModel) executeCommand(command commands.Command) (tea.Model, tea.Cmd)
 	}
 	switch command.Name {
 	case commands.AppHelpCommand:
-		helpDialog := dialog.NewHelpDialog(a.app.Commands)
+		helpDialog := dialog.NewHelpDialog(a.app.Commands.Sorted())
 		a.modal = helpDialog
 	case commands.EditorOpenCommand:
 		if a.app.IsBusy() {