fix: improve submit

Carlos Alexandro Becker created

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

internal/tui/components/dialogs/commands/arguments.go |  2 
internal/tui/components/dialogs/commands/loader.go    | 29 +++++++++---
internal/tui/tui.go                                   | 14 -----
3 files changed, 23 insertions(+), 22 deletions(-)

Detailed changes

internal/tui/components/dialogs/commands/arguments.go 🔗

@@ -21,8 +21,8 @@ const (
 type ShowArgumentsDialogMsg struct {
 	CommandID   string
 	Description string
-	Content     string
 	ArgNames    []string
+	OnSubmit    func(args map[string]string) tea.Cmd
 }
 
 // CloseArgumentsDialogMsg is a message that is sent when the arguments dialog is closed.

internal/tui/components/dialogs/commands/loader.go 🔗

@@ -160,18 +160,31 @@ func createCommandHandler(id, desc, content string) func(Command) tea.Cmd {
 	return func(cmd Command) tea.Cmd {
 		args := extractArgNames(content)
 
-		if len(args) > 0 {
-			return util.CmdHandler(ShowArgumentsDialogMsg{
-				CommandID:   id,
-				Description: desc,
-				Content:     content,
-				ArgNames:    args,
+		if len(args) == 0 {
+			return util.CmdHandler(CommandRunCustomMsg{
+				Content: content,
 			})
 		}
+		return util.CmdHandler(ShowArgumentsDialogMsg{
+			CommandID:   id,
+			Description: desc,
+			ArgNames:    args,
+			OnSubmit: func(args map[string]string) tea.Cmd {
+				return execUserPrompt(content, args)
+			},
+		})
+	}
+}
 
-		return util.CmdHandler(CommandRunCustomMsg{
+func execUserPrompt(content string, args map[string]string) tea.Cmd {
+	return func() tea.Msg {
+		for name, value := range args {
+			placeholder := "$" + name
+			content = strings.ReplaceAll(content, placeholder, value)
+		}
+		return CommandRunCustomMsg{
 			Content: content,
-		})
+		}
 	}
 }
 

internal/tui/tui.go 🔗

@@ -156,19 +156,7 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 					msg.CommandID,
 					msg.Description,
 					args,
-					func(args map[string]string) tea.Cmd {
-						return func() tea.Msg {
-							content := msg.Content
-							for _, name := range msg.ArgNames {
-								value := args[name]
-								placeholder := "$" + name
-								content = strings.ReplaceAll(content, placeholder, value)
-							}
-							return commands.CommandRunCustomMsg{
-								Content: content,
-							}
-						}
-					},
+					msg.OnSubmit,
 				),
 			},
 		)