66 "os"
77 "strings"
88
9+ "github.com/charmbracelet/bubbles/textinput"
910 tea "github.com/charmbracelet/bubbletea"
1011 "github.com/cocoide/commitify/internal/entity"
1112 "github.com/cocoide/commitify/internal/gateway"
@@ -19,6 +20,8 @@ type model struct {
1920 currentIdx int
2021 errorMsg string
2122 isLoading bool
23+ isEditing bool
24+ textInput textinput.Model
2225}
2326
2427func (m * model ) Init () tea.Cmd {
@@ -42,14 +45,22 @@ func (m *model) Init() tea.Cmd {
4245 }
4346 m .choices = messages
4447 m .isLoading = false
45-
46- return nil
48+ return textinput .Blink
4749}
4850
4951func (m * model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
52+ var cmd tea.Cmd
53+ m .textInput , cmd = m .textInput .Update (msg )
5054 switch msg := msg .(type ) {
5155 case tea.KeyMsg :
5256 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
5364 case tea .KeyUp :
5465 if m .currentIdx > 0 {
5566 m .currentIdx --
@@ -84,6 +95,10 @@ func (m *model) View() string {
8495 red := color .New (color .FgRed ).SprintFunc ()
8596 b .WriteString (red (m .errorMsg ) + "\n \n " )
8697 }
98+ if m .isEditing {
99+ return m .textInput .View ()
100+ }
101+
87102 white := color .New (color .FgWhite ).SprintFunc ()
88103 b .WriteString (white ("🍕Please select an option:" ))
89104 b .WriteString (white ("\n Use arrow ↑↓ to navigate and press Enter to select.\n \n " ))
@@ -100,12 +115,27 @@ func (m *model) View() string {
100115 return b .String ()
101116}
102117
118+ func initialModel () model {
119+ ti := textinput .New ()
120+ ti .Focus ()
121+
122+ return model {
123+ choices :[]string {"" },
124+ currentIdx :0 ,
125+ errorMsg :"" ,
126+ isLoading : true ,
127+ isEditing : false ,
128+ textInput : ti ,
129+ }
130+ }
131+
132+
103133var suggestCmd = & cobra.Command {
104134 Use : "suggest" ,
105135 Short : "Suggestion of commit message for staging repository" ,
106136 Aliases : []string {"s" , "suggest" },
107137 Run : func (cmd * cobra.Command , args []string ) {
108- m := model { isLoading : true }
138+ m := initialModel ()
109139 p := tea .NewProgram (& m )
110140 p .Run ()
111141 },
0 commit comments