mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
feat: added commit message window to tui
This commit is contained in:
@@ -23,6 +23,8 @@ var rootCmd = &cobra.Command{
|
||||
//TODO: add bubble tea interface to this
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var message string
|
||||
|
||||
// check if the print flag is set
|
||||
pflag, _ := cmd.Flags().GetBool("print")
|
||||
// run execute commands again as root run will not call this part
|
||||
@@ -32,11 +34,9 @@ var rootCmd = &cobra.Command{
|
||||
case 0:
|
||||
// launch the tui
|
||||
args = append(args, tui.Entry_CM())
|
||||
fmt.Println(args[0])
|
||||
//sel_auth := tui.Entry()
|
||||
// for _, a := range sel_auth {
|
||||
// fmt.Println(a)
|
||||
// }
|
||||
sel_auth := tui.Entry()
|
||||
message = utils.Commit(args[0], sel_auth)
|
||||
goto tui
|
||||
case 1:
|
||||
if len(args) == 1 {
|
||||
utils.GitWrapper(args[0])
|
||||
@@ -54,7 +54,9 @@ var rootCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// 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
|
||||
if pflag {
|
||||
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"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user