@@ -3,7 +3,6 @@ package cmd
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "log"
7
6
"strings"
8
7
9
8
tea "github.com/charmbracelet/bubbletea"
@@ -18,14 +17,40 @@ type model struct {
18
17
choices []string
19
18
currentIdx int
20
19
errorMsg string
20
+ isLoading bool
21
+ messages []string
22
+ }
23
+
24
+ type generateMessages struct {
25
+ messages []string
26
+ errorMsg string
21
27
}
22
28
23
29
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
+ }
25
41
}
26
42
27
43
func (m model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
28
44
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
29
54
case tea.KeyMsg :
30
55
switch msg .Type {
31
56
case tea .KeyUp :
@@ -38,7 +63,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
38
63
}
39
64
case tea .KeyEnter :
40
65
if err := util .ExecCommitMessage (m .choices [m .currentIdx ]); err != nil {
41
- m .errorMsg = "コミットエラーが発生"
66
+ m .errorMsg = "コミットに失敗: " + err . Error ()
42
67
return m , tea .Quit
43
68
}
44
69
return m , tea .Quit
@@ -50,13 +75,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
50
75
}
51
76
52
77
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
+ }
53
85
var b strings.Builder
54
86
if m .errorMsg != "" {
55
87
red := color .New (color .FgRed ).SprintFunc ()
56
88
b .WriteString (red (m .errorMsg ) + "\n \n " )
57
89
}
58
90
white := color .New (color .FgWhite ).SprintFunc ()
59
- b .WriteString (white ("Please select an option:" ))
91
+ b .WriteString (white ("🍕 Please select an option:" ))
60
92
b .WriteString (white ("\n Use arrow ↑↓ to navigate and press Enter to select.\n \n " ))
61
93
62
94
for i , choice := range m .choices {
@@ -76,19 +108,7 @@ var suggestCmd = &cobra.Command{
76
108
Short : "Suggestion of commit message for staging repository" ,
77
109
Aliases : []string {"s" , "suggest" },
78
110
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 }
92
112
p := tea .NewProgram (m )
93
113
p .Run ()
94
114
},
0 commit comments