@@ -2,6 +2,7 @@ package ui
22
33import (
44 "context"
5+ "fmt"
56 "strings"
67
78 tea "github.com/charmbracelet/bubbletea"
@@ -64,19 +65,21 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6465 }
6566 if a .pendingInput != nil {
6667 if msg .Type == tea .KeyEnter {
67- // ENTER selects the first option (default)
68- selectedKey := ""
69- if len (a .pendingInput .Options ) > 0 {
70- selectedKey = a .pendingInput .Options [0 ].Key
68+ for _ , opt := range a .pendingInput .Options {
69+ if opt .Key == "enter" {
70+ a .lines = appendLine (a .lines , formatResolvedInput (* a .pendingInput , "enter" ))
71+ responseCmd := sendInputResponseCmd (a .pendingInput .ResponseCh , output.InputResponse {SelectedKey : "enter" })
72+ a .pendingInput = nil
73+ a .inputPrompt = a .inputPrompt .Hide ()
74+ return a , responseCmd
75+ }
7176 }
72- responseCmd := sendInputResponseCmd (a .pendingInput .ResponseCh , output.InputResponse {SelectedKey : selectedKey })
73- a .pendingInput = nil
74- a .inputPrompt = a .inputPrompt .Hide ()
75- return a , responseCmd
77+ return a , nil
7678 }
7779 // A single character key press selects the matching option
7880 for _ , opt := range a .pendingInput .Options {
7981 if msg .String () == opt .Key {
82+ a .lines = appendLine (a .lines , formatResolvedInput (* a .pendingInput , opt .Key ))
8083 responseCmd := sendInputResponseCmd (a .pendingInput .ResponseCh , output.InputResponse {SelectedKey : opt .Key })
8184 a .pendingInput = nil
8285 a .inputPrompt = a .inputPrompt .Hide ()
@@ -122,6 +125,32 @@ func appendLine(lines []string, line string) []string {
122125 return lines
123126}
124127
128+ func formatResolvedInput (req output.UserInputRequestEvent , selectedKey string ) string {
129+ firstLine := strings .Split (req .Prompt , "\n " )[0 ]
130+ labels := make ([]string , 0 , len (req .Options ))
131+ selected := selectedKey
132+
133+ for _ , opt := range req .Options {
134+ if opt .Label != "" {
135+ labels = append (labels , opt .Label )
136+ }
137+ if opt .Key == selectedKey && opt .Label != "" {
138+ selected = opt .Label
139+ }
140+ }
141+
142+ switch len (labels ) {
143+ case 1 :
144+ firstLine = fmt .Sprintf ("%s (%s)" , firstLine , labels [0 ])
145+ default :
146+ if len (labels ) > 1 {
147+ firstLine = fmt .Sprintf ("%s [%s]" , firstLine , strings .Join (labels , "/" ))
148+ }
149+ }
150+
151+ return fmt .Sprintf ("%s %s" , firstLine , selected )
152+ }
153+
125154func (a App ) View () string {
126155 var sb strings.Builder
127156 sb .WriteString (a .header .View ())
@@ -133,7 +162,7 @@ func (a App) View() string {
133162 }
134163 if promptView := a .inputPrompt .View (); promptView != "" {
135164 sb .WriteString (" " )
136- sb .WriteString (promptView )
165+ sb .WriteString (strings . ReplaceAll ( promptView , " \n " , " \n " ) )
137166 sb .WriteString ("\n " )
138167 }
139168 return sb .String ()
0 commit comments