6
6
"os"
7
7
"strings"
8
8
9
+ "github.com/charmbracelet/bubbles/textinput"
9
10
tea "github.com/charmbracelet/bubbletea"
10
11
"github.com/cocoide/commitify/internal/entity"
11
12
"github.com/cocoide/commitify/internal/gateway"
@@ -19,6 +20,8 @@ type model struct {
19
20
currentIdx int
20
21
errorMsg string
21
22
isLoading bool
23
+ isEditing bool
24
+ textInput textinput.Model
22
25
}
23
26
24
27
func (m * model ) Init () tea.Cmd {
@@ -42,14 +45,22 @@ func (m *model) Init() tea.Cmd {
42
45
}
43
46
m .choices = messages
44
47
m .isLoading = false
45
-
46
- return nil
48
+ return textinput .Blink
47
49
}
48
50
49
51
func (m * model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
52
+ var cmd tea.Cmd
53
+ m .textInput , cmd = m .textInput .Update (msg )
50
54
switch msg := msg .(type ) {
51
55
case tea.KeyMsg :
52
56
switch msg .Type {
57
+ case tea .KeyTab :
58
+ m .isEditing = true
59
+ m .textInput .Focus ()
60
+ m .textInput .SetValue (m .choices [m .currentIdx ])
61
+ m .textInput .CharLimit = 100
62
+ m .textInput .Width = 100
63
+ return m , cmd
53
64
case tea .KeyUp :
54
65
if m .currentIdx > 0 {
55
66
m .currentIdx --
@@ -82,9 +93,14 @@ func (m *model) View() string {
82
93
if m .errorMsg != "" {
83
94
b .WriteString (color .RedString (m .errorMsg ) + "\n \n " )
84
95
}
96
+ if m .isEditing {
97
+ return m .textInput .View ()
98
+ }
99
+
85
100
b .WriteString (color .WhiteString ("🍕Please select an option:" ))
86
101
b .WriteString (color .WhiteString ("\n Use arrow ↑↓ to navigate and press Enter to select.\n \n " ))
87
102
103
+
88
104
for i , choice := range m .choices {
89
105
if i == m .currentIdx {
90
106
b .WriteString (fmt .Sprintf (color .HiCyanString ("➡️ %s\n " ), choice ))
@@ -95,12 +111,27 @@ func (m *model) View() string {
95
111
return b .String ()
96
112
}
97
113
114
+ func initialModel () model {
115
+ ti := textinput .New ()
116
+ ti .Focus ()
117
+
118
+ return model {
119
+ choices :[]string {"" },
120
+ currentIdx :0 ,
121
+ errorMsg :"" ,
122
+ isLoading : true ,
123
+ isEditing : false ,
124
+ textInput : ti ,
125
+ }
126
+ }
127
+
128
+
98
129
var suggestCmd = & cobra.Command {
99
130
Use : "suggest" ,
100
131
Short : "Suggestion of commit message for staging repository" ,
101
132
Aliases : []string {"s" , "suggest" },
102
133
Run : func (cmd * cobra.Command , args []string ) {
103
- m := model { isLoading : true }
134
+ m := initialModel ()
104
135
p := tea .NewProgram (& m )
105
136
p .Run ()
106
137
},
0 commit comments