Skip to content

Commit e674174

Browse files
author
{cocoide}
committed
4. Update Update() function to handle generateMessages message and update model accordingly
1 parent 7d380f8 commit e674174

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

cmd/suggest.go

+37-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"strings"
87

98
tea "github.com/charmbracelet/bubbletea"
@@ -18,14 +17,40 @@ type model struct {
1817
choices []string
1918
currentIdx int
2019
errorMsg string
20+
isLoading bool
21+
messages []string
22+
}
23+
24+
type generateMessages struct {
25+
messages []string
26+
errorMsg string
2127
}
2228

2329
func (m model) Init() tea.Cmd {
24-
return nil
30+
return func() tea.Msg {
31+
util.LoadEnv()
32+
ctx := context.Background()
33+
og := gateway.NewOpenAIGateway(ctx)
34+
ms := service.NewMessageService(og)
35+
messages, err := ms.AsyncGenerateCommitMessage()
36+
if err != nil {
37+
return generateMessages{errorMsg: "メッセージの生成に失敗: " + err.Error()}
38+
}
39+
return generateMessages{messages: messages}
40+
}
2541
}
2642

2743
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2844
switch msg := msg.(type) {
45+
case generateMessages:
46+
if msg.errorMsg != "" {
47+
m.errorMsg = msg.errorMsg
48+
m.isLoading = false
49+
return m, nil
50+
}
51+
m.choices = msg.messages
52+
m.isLoading = false
53+
return m, nil
2954
case tea.KeyMsg:
3055
switch msg.Type {
3156
case tea.KeyUp:
@@ -38,7 +63,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
3863
}
3964
case tea.KeyEnter:
4065
if err := util.ExecCommitMessage(m.choices[m.currentIdx]); err != nil {
41-
m.errorMsg = "コミットエラーが発生"
66+
m.errorMsg = "コミットに失敗: " + err.Error()
4267
return m, tea.Quit
4368
}
4469
return m, tea.Quit
@@ -50,13 +75,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5075
}
5176

5277
func (m model) View() string {
78+
if m.errorMsg != "" {
79+
red := color.New(color.FgRed).SprintFunc()
80+
return fmt.Sprintf(red(m.errorMsg))
81+
}
82+
if m.isLoading {
83+
return "🌎 Generating commit messages ..."
84+
}
5385
var b strings.Builder
5486
if m.errorMsg != "" {
5587
red := color.New(color.FgRed).SprintFunc()
5688
b.WriteString(red(m.errorMsg) + "\n\n")
5789
}
5890
white := color.New(color.FgWhite).SprintFunc()
59-
b.WriteString(white("Please select an option:"))
91+
b.WriteString(white("🍕Please select an option:"))
6092
b.WriteString(white("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
6193

6294
for i, choice := range m.choices {
@@ -76,19 +108,7 @@ var suggestCmd = &cobra.Command{
76108
Short: "Suggestion of commit message for staging repository",
77109
Aliases: []string{"s", "suggest"},
78110
Run: func(cmd *cobra.Command, args []string) {
79-
util.LoadEnv()
80-
ctx := context.Background()
81-
og := gateway.NewOpenAIGateway(ctx)
82-
ms := service.NewMessageService(og)
83-
messages, err := ms.AsyncGenerateCommitMessage()
84-
if err != nil {
85-
log.Fatal(err.Error())
86-
}
87-
var choices []string
88-
for _, v := range messages {
89-
choices = append(choices, v)
90-
}
91-
m := model{choices: choices}
111+
m := model{isLoading: true}
92112
p := tea.NewProgram(m)
93113
p.Run()
94114
},

internal/service/message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
const (
12-
CommitMessagePrompt = "Generate commit messages for [%s]. Each message should be separated by only space"
12+
CommitMessagePrompt = "Generate up to 5 commit messages for [%s]. Each message should be separated by only space"
1313
FormatNotice = ", format commit as:\n- feat: [feature description]\n- bugfix: [bugfix description]"
1414
)
1515

0 commit comments

Comments
 (0)