Skip to content

Commit 2076c67

Browse files
authored
Merge pull request #47 from cocoide/feature/edit-commit-message
コミットメッセージの編集機能を実装
2 parents c6d35f8 + 287fd3b commit 2076c67

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

cmd/suggest.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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

2427
func (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

4951
func (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--
@@ -82,9 +93,14 @@ func (m *model) View() string {
8293
if m.errorMsg != "" {
8394
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
8495
}
96+
if m.isEditing{
97+
return m.textInput.View()
98+
}
99+
85100
b.WriteString(color.WhiteString("🍕Please select an option:"))
86101
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
87102

103+
88104
for i, choice := range m.choices {
89105
if i == m.currentIdx {
90106
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
@@ -95,12 +111,27 @@ func (m *model) View() string {
95111
return b.String()
96112
}
97113

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+
98129
var suggestCmd = &cobra.Command{
99130
Use: "suggest",
100131
Short: "Suggestion of commit message for staging repository",
101132
Aliases: []string{"s", "suggest"},
102133
Run: func(cmd *cobra.Command, args []string) {
103-
m := model{isLoading: true}
134+
m := initialModel()
104135
p := tea.NewProgram(&m)
105136
p.Run()
106137
},

0 commit comments

Comments
 (0)