Skip to content

Commit

Permalink
feat: implement commit message editing with bubbletea UI (#187)
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy authored Jul 7, 2024
1 parent 15f4908 commit 6f5ce01
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/appleboy/CodeGPT/prompt"
"github.com/appleboy/CodeGPT/util"

tea "github.com/charmbracelet/bubbletea"
"github.com/erikgeiser/promptkit/confirmation"
"github.com/fatih/color"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -264,10 +265,11 @@ var commitCmd = &cobra.Command{

// unescape html entities in commit message
commitMessage = html.UnescapeString(commitMessage)
commitMessage = strings.TrimSpace(commitMessage)

// Output commit summary data from AI
color.Yellow("================Commit Summary====================")
color.Yellow("\n" + strings.TrimSpace(commitMessage) + "\n\n")
color.Yellow("\n" + commitMessage + "\n\n")
color.Yellow("==================================================")

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

if preview {
input := confirmation.New("Commit preview summary?", confirmation.Undecided)
input := confirmation.New("Commit preview summary?", confirmation.Yes)
ready, err := input.RunPrompt()
if err != nil {
return err
Expand All @@ -296,6 +298,20 @@ var commitCmd = &cobra.Command{
}
}

input := confirmation.New("Do you want to change the commit message?", confirmation.No)
change, err := input.RunPrompt()
if err != nil {
return err
}

if change {
p := tea.NewProgram(initialPrompt(commitMessage))
if _, err := p.Run(); err != nil {
return err
}
p.Wait()
}

// git commit automatically
color.Cyan("Git record changes to the repository")
output, err := g.Commit(commitMessage)
Expand Down
72 changes: 72 additions & 0 deletions cmd/textarea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
)

type errMsg error

type model struct {
textarea textarea.Model
err error
}

func initialPrompt(value string) model {
ti := textarea.New()
ti.InsertString(value)
ti.SetWidth(80)
ti.SetHeight(len(strings.Split(value, "\n")))
ti.Focus()

return model{
textarea: ti,
err: nil,
}
}

func (m model) Init() tea.Cmd {
return textarea.Blink
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type { //nolint:exhaustive
case tea.KeyEsc:
if m.textarea.Focused() {
m.textarea.Blur()
}
case tea.KeyCtrlC:
return m, tea.Quit
default:
if !m.textarea.Focused() {
cmd = m.textarea.Focus()
cmds = append(cmds, cmd)
}
}

// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}

m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}

func (m model) View() string {
return fmt.Sprintf(
"Please confirm the following commit message.\n\n%s\n\n%s",
m.textarea.View(),
"(ctrl+c to continue.)",
) + "\n\n"
}

0 comments on commit 6f5ce01

Please sign in to comment.