Skip to content

Commit 91478c9

Browse files
authored
Merge pull request #49 from cocoide/feature/loading
メッセージ生成中のLoadingUIの実装
2 parents 2076c67 + 7b2a9eb commit 91478c9

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

cmd/suggest.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,31 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/charmbracelet/bubbles/spinner"
910
"github.com/charmbracelet/bubbles/textinput"
1011
tea "github.com/charmbracelet/bubbletea"
12+
"github.com/charmbracelet/lipgloss"
1113
"github.com/cocoide/commitify/internal/entity"
1214
"github.com/cocoide/commitify/internal/gateway"
1315
"github.com/cocoide/commitify/util"
1416
"github.com/fatih/color"
1517
"github.com/spf13/cobra"
1618
)
1719

20+
var (
21+
textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("252")).Render
22+
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
23+
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
24+
)
25+
1826
type model struct {
1927
choices []string
2028
currentIdx int
2129
errorMsg string
2230
isLoading bool
23-
isEditing bool
24-
textInput textinput.Model
31+
isEditing bool
32+
spinner spinner.Model
33+
textInput textinput.Model
2534
}
2635

2736
func (m *model) Init() tea.Cmd {
@@ -38,13 +47,16 @@ func (m *model) Init() tea.Cmd {
3847
gi = gateway.NewGrpcServeGateway()
3948
}
4049

41-
messages, err := gi.FetchCommitMessages()
42-
if err != nil {
43-
log.Fatal("コミットメッセージの生成に失敗: ", err)
44-
os.Exit(-1)
45-
}
46-
m.choices = messages
47-
m.isLoading = false
50+
go func() {
51+
messages, err := gi.FetchCommitMessages()
52+
if err != nil {
53+
log.Fatal("コミットメッセージの生成に失敗: ", err)
54+
os.Exit(-1)
55+
}
56+
m.choices = messages
57+
m.isLoading = false
58+
}()
59+
4860
return textinput.Blink
4961
}
5062

@@ -78,29 +90,39 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7890
case tea.KeyCtrlC, tea.KeyEsc:
7991
return m, tea.Quit
8092
}
93+
case spinner.TickMsg:
94+
var cmd tea.Cmd
95+
m.spinner, cmd = m.spinner.Update(msg)
96+
return m, cmd
8197
}
82-
return m, nil
98+
return m, m.spinner.Tick
99+
}
100+
101+
func (m *model) resetSpinner() {
102+
m.spinner = spinner.New()
103+
m.spinner.Style = spinnerStyle
104+
m.spinner.Spinner = spinner.Globe
83105
}
84106

85107
func (m *model) View() string {
86108
if m.errorMsg != "" {
87109
return color.RedString(m.errorMsg)
88110
}
89111
if m.isLoading {
90-
return "🌎 Generating commit messages ..."
112+
s := fmt.Sprintf("\n %s %s\n\n", m.spinner.View(), textStyle("Generating commit messages..."))
113+
return s
91114
}
92115
var b strings.Builder
93116
if m.errorMsg != "" {
94117
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
95118
}
96-
if m.isEditing{
119+
if m.isEditing {
97120
return m.textInput.View()
98121
}
99122

100123
b.WriteString(color.WhiteString("🍕Please select an option:"))
101124
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
102125

103-
104126
for i, choice := range m.choices {
105127
if i == m.currentIdx {
106128
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
@@ -116,22 +138,22 @@ func initialModel() model {
116138
ti.Focus()
117139

118140
return model{
119-
choices :[]string{""},
120-
currentIdx :0,
121-
errorMsg :"",
141+
choices: []string{""},
142+
currentIdx: 0,
143+
errorMsg: "",
122144
isLoading: true,
123-
isEditing: false,
124-
textInput: ti,
145+
isEditing: false,
146+
textInput: ti,
125147
}
126148
}
127149

128-
129150
var suggestCmd = &cobra.Command{
130151
Use: "suggest",
131152
Short: "Suggestion of commit message for staging repository",
132153
Aliases: []string{"s", "suggest"},
133154
Run: func(cmd *cobra.Command, args []string) {
134155
m := initialModel()
156+
m.resetSpinner()
135157
p := tea.NewProgram(&m)
136158
p.Run()
137159
},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/charmbracelet/bubbles v0.16.1
77
github.com/charmbracelet/bubbletea v0.24.2
8+
github.com/charmbracelet/lipgloss v0.7.1
89
github.com/fatih/color v1.15.0
910
github.com/golang/mock v1.4.4
1011
github.com/sashabaranov/go-openai v1.15.1

0 commit comments

Comments
 (0)