feat: added commit message window to tui

This commit is contained in:
Slug-Boi
2024-10-20 22:52:37 +02:00
parent 87a6de6b6f
commit 7f5a3bcca6
2 changed files with 119 additions and 8 deletions
+9 -7
View File
@@ -23,20 +23,20 @@ var rootCmd = &cobra.Command{
//TODO: add bubble tea interface to this //TODO: add bubble tea interface to this
Args: cobra.MinimumNArgs(0), Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var message string
// check if the print flag is set // check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print") pflag, _ := cmd.Flags().GetBool("print")
// run execute commands again as root run will not call this part // run execute commands again as root run will not call this part
// redundant check for now but will be useful later when we add tui // redundant check for now but will be useful later when we add tui
wrap_around: wrap_around:
switch len(args) { switch len(args) {
case 0: case 0:
// launch the tui // launch the tui
args = append(args, tui.Entry_CM()) args = append(args, tui.Entry_CM())
fmt.Println(args[0]) sel_auth := tui.Entry()
//sel_auth := tui.Entry() message = utils.Commit(args[0], sel_auth)
// for _, a := range sel_auth { goto tui
// fmt.Println(a)
// }
case 1: case 1:
if len(args) == 1 { if len(args) == 1 {
utils.GitWrapper(args[0]) utils.GitWrapper(args[0])
@@ -54,7 +54,9 @@ var rootCmd = &cobra.Command{
} }
// builds the commit message with the selected authors // builds the commit message with the selected authors
message := utils.Commit(args[0], args[1:]) message = utils.Commit(args[0], args[1:])
tui:
// prints the commit message to the console if the print flag is set // prints the commit message to the console if the print flag is set
if pflag { if pflag {
fmt.Println(message) fmt.Println(message)
@@ -0,0 +1,109 @@
package tui
// A simple program demonstrating the textarea component from the Bubbles
// component library.
//TODO: maybe add a submit button below the textarea
import (
"fmt"
"log"
"os"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
)
type KeyMap struct {
EndWithMes key.Binding
}
func newKeyMap() *KeyMap {
return &KeyMap{
EndWithMes: key.NewBinding(
key.WithKeys("alt+enter"),
),
}
}
func Entry_CM() string {
newKeyMap()
p := tea.NewProgram(initialModel_cm())
m, err := p.Run()
if err != nil {
log.Fatal(err)
}
if m.(model_cm).textarea.Value() == "" {
fmt.Println("No commit message provided. Exiting...")
os.Exit(1)
}
return m.(model_cm).textarea.Value() + "\n"
}
type errMsg error
type model_cm struct {
textarea textarea.Model
keys *KeyMap
err error
}
func initialModel_cm() model_cm {
ti := textarea.New()
ti.Placeholder = "Write your commit message here..."
ti.Focus()
return model_cm{
textarea: ti,
keys: newKeyMap(),
err: nil,
}
}
func (m model_cm) Init() tea.Cmd {
return textarea.Blink
}
func (m model_cm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.EndWithMes):
return m, tea.Quit
}
switch msg.Type {
case tea.KeyCtrlC:
m.textarea.SetValue("")
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_cm) View() string {
return fmt.Sprintf(
"Tell me a story.\n\n%s\n\n%s",
m.textarea.View(),
"alt+enter|Submit\nctrl+c|Cancel",
) + "\n\n"
}