Skip to content

Commit 6f5ce01

Browse files
authored
feat: implement commit message editing with bubbletea UI (#187)
- Add `tea` import from `github.com/charmbracelet/bubbletea` - Trim commit message before displaying it - Change confirmation prompt default to `Yes` - Add prompt to change commit message with default `No` - Add logic to handle commit message change using `bubbletea` program - Create new file `textarea.go` for handling commit message input - Implement `model` struct and functions for `textarea` interaction - Add `View` function to display commit message confirmation prompt Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 15f4908 commit 6f5ce01

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

cmd/commit.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/appleboy/CodeGPT/prompt"
1414
"github.com/appleboy/CodeGPT/util"
1515

16+
tea "github.com/charmbracelet/bubbletea"
1617
"github.com/erikgeiser/promptkit/confirmation"
1718
"github.com/fatih/color"
1819
"github.com/joho/godotenv"
@@ -264,10 +265,11 @@ var commitCmd = &cobra.Command{
264265

265266
// unescape html entities in commit message
266267
commitMessage = html.UnescapeString(commitMessage)
268+
commitMessage = strings.TrimSpace(commitMessage)
267269

268270
// Output commit summary data from AI
269271
color.Yellow("================Commit Summary====================")
270-
color.Yellow("\n" + strings.TrimSpace(commitMessage) + "\n\n")
272+
color.Yellow("\n" + commitMessage + "\n\n")
271273
color.Yellow("==================================================")
272274

273275
outputFile := viper.GetString("output.file")
@@ -286,7 +288,7 @@ var commitCmd = &cobra.Command{
286288
}
287289

288290
if preview {
289-
input := confirmation.New("Commit preview summary?", confirmation.Undecided)
291+
input := confirmation.New("Commit preview summary?", confirmation.Yes)
290292
ready, err := input.RunPrompt()
291293
if err != nil {
292294
return err
@@ -296,6 +298,20 @@ var commitCmd = &cobra.Command{
296298
}
297299
}
298300

301+
input := confirmation.New("Do you want to change the commit message?", confirmation.No)
302+
change, err := input.RunPrompt()
303+
if err != nil {
304+
return err
305+
}
306+
307+
if change {
308+
p := tea.NewProgram(initialPrompt(commitMessage))
309+
if _, err := p.Run(); err != nil {
310+
return err
311+
}
312+
p.Wait()
313+
}
314+
299315
// git commit automatically
300316
color.Cyan("Git record changes to the repository")
301317
output, err := g.Commit(commitMessage)

cmd/textarea.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/charmbracelet/bubbles/textarea"
8+
tea "github.com/charmbracelet/bubbletea"
9+
)
10+
11+
type errMsg error
12+
13+
type model struct {
14+
textarea textarea.Model
15+
err error
16+
}
17+
18+
func initialPrompt(value string) model {
19+
ti := textarea.New()
20+
ti.InsertString(value)
21+
ti.SetWidth(80)
22+
ti.SetHeight(len(strings.Split(value, "\n")))
23+
ti.Focus()
24+
25+
return model{
26+
textarea: ti,
27+
err: nil,
28+
}
29+
}
30+
31+
func (m model) Init() tea.Cmd {
32+
return textarea.Blink
33+
}
34+
35+
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
36+
var cmds []tea.Cmd
37+
var cmd tea.Cmd
38+
39+
switch msg := msg.(type) {
40+
case tea.KeyMsg:
41+
switch msg.Type { //nolint:exhaustive
42+
case tea.KeyEsc:
43+
if m.textarea.Focused() {
44+
m.textarea.Blur()
45+
}
46+
case tea.KeyCtrlC:
47+
return m, tea.Quit
48+
default:
49+
if !m.textarea.Focused() {
50+
cmd = m.textarea.Focus()
51+
cmds = append(cmds, cmd)
52+
}
53+
}
54+
55+
// We handle errors just like any other message
56+
case errMsg:
57+
m.err = msg
58+
return m, nil
59+
}
60+
61+
m.textarea, cmd = m.textarea.Update(msg)
62+
cmds = append(cmds, cmd)
63+
return m, tea.Batch(cmds...)
64+
}
65+
66+
func (m model) View() string {
67+
return fmt.Sprintf(
68+
"Please confirm the following commit message.\n\n%s\n\n%s",
69+
m.textarea.View(),
70+
"(ctrl+c to continue.)",
71+
) + "\n\n"
72+
}

0 commit comments

Comments
 (0)