diff --git a/src_code/go_src/cmd/root.go b/src_code/go_src/cmd/root.go index 4846f27..206bdc1 100644 --- a/src_code/go_src/cmd/root.go +++ b/src_code/go_src/cmd/root.go @@ -23,20 +23,20 @@ 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 // redundant check for now but will be useful later when we add tui - wrap_around: + wrap_around: switch len(args) { 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]) @@ -46,7 +46,7 @@ var rootCmd = &cobra.Command{ os.Exit(0) } } - + // check if user included -m tag and remove. Wrap around for safety's sake if args[0] == "-m" { args = args[1:] @@ -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) diff --git a/src_code/go_src/cmd/tui/tui_commit_message.go b/src_code/go_src/cmd/tui/tui_commit_message.go new file mode 100644 index 0000000..f94c136 --- /dev/null +++ b/src_code/go_src/cmd/tui/tui_commit_message.go @@ -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" +} +