From 7662069886eb14a0b10952780ff345815865cb2c Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Thu, 10 Apr 2025 22:31:16 +0200 Subject: [PATCH 1/5] feat: add gh add tui component --- src/cmd/tui/tui_github.go | 223 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 src/cmd/tui/tui_github.go diff --git a/src/cmd/tui/tui_github.go b/src/cmd/tui/tui_github.go new file mode 100644 index 0000000..bd9b61f --- /dev/null +++ b/src/cmd/tui/tui_github.go @@ -0,0 +1,223 @@ +package tui + +import ( + "fmt" + "strings" + + "github.com/Slug-Boi/cocommit/src/cmd/utils" + "github.com/charmbracelet/bubbles/textinput" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +// Styles +var ( + errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) + toggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("99")) + activeToggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) +) + +type GitHubUserModel struct { + inputs []textinput.Model + focusIndex int + submitted bool + showError bool + errorMsg string + tempAuthShow bool + tempAuth bool +} + +func NewGitHubUserForm(old_m *model) GitHubUserModel { + parent_m = old_m + + m := GitHubUserModel{ + inputs: make([]textinput.Model, 2), + tempAuthShow: func() bool { + return old_m != nil + }(), + + } + + // GitHub Username (required) + username := textinput.New() + username.Placeholder = "GitHub username *" + username.PromptStyle = focusedStyle + username.TextStyle = focusedStyle + username.Focus() + username.CharLimit = 39 // GitHub username max length + m.inputs[0] = username + + // Email (optional) + email := textinput.New() + email.Placeholder = "Email" + email.PromptStyle = blurredStyle + email.TextStyle = blurredStyle + m.inputs[1] = email + + return m +} + +func (m GitHubUserModel) Init() tea.Cmd { + return textinput.Blink +} + +func (m GitHubUserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "esc": + return m, tea.Quit + case "ctrl+t": // Toggle temp mode + if m.tempAuthShow { + m.tempAuth = !m.tempAuth + return m, nil + } + case "tab", "shift+tab", "enter", "up", "down": + s := msg.String() + + // Submit on enter when button is focused + if s == "enter" && m.focusIndex == len(m.inputs)+1 && m.tempAuthShow || s == "enter" && m.focusIndex == len(m.inputs) && !m.tempAuthShow { + if m.inputs[0].Value() == "" { + m.showError = true + m.errorMsg = "GitHub username is required" + return m, nil + } + m.submitted = true + user := utils.FetchGithubProfile(m.inputs[0].Value()) + if m.inputs[1].Value() != "" { + user.Email = m.inputs[1].Value() + } + if m.tempAuth { + return createGHTempAuthorModel(parent_m,user), nil + } + return createGHAuthorModel(parent_m,user), nil + + } else if s == "enter" && m.focusIndex == len(m.inputs) && m.tempAuthShow { + //toggle temp mode + m.tempAuth = !m.tempAuth + return m, nil + } + + + // Cycle through inputs + if s == "up" || s == "shift+tab" { + m.focusIndex-- + } else { + m.focusIndex++ + } + + inpNum := len(m.inputs) + if m.tempAuthShow { + inpNum++ + } + + if m.focusIndex > inpNum { + m.focusIndex = 0 + } else if m.focusIndex < 0 { + m.focusIndex = inpNum + } + + cmds := make([]tea.Cmd, len(m.inputs)) + for i := 0; i < len(m.inputs); i++ { + if i == m.focusIndex { + cmds[i] = m.inputs[i].Focus() + m.inputs[i].PromptStyle = focusedStyle + m.inputs[i].TextStyle = focusedStyle + continue + } + m.inputs[i].Blur() + m.inputs[i].PromptStyle = blurredStyle + if m.inputs[i].Value() == "" { + m.inputs[i].TextStyle = blurredStyle + } else { + m.inputs[i].TextStyle = noStyle + } + } + + m.showError = false // Clear error when navigating + return m, tea.Batch(cmds...) + } + } + + // Handle text input + cmd := m.updateInputs(msg) + return m, cmd +} + +func (m *GitHubUserModel) updateInputs(msg tea.Msg) tea.Cmd { + cmds := make([]tea.Cmd, len(m.inputs)) + for i := range m.inputs { + m.inputs[i], cmds[i] = m.inputs[i].Update(msg) + } + return tea.Batch(cmds...) +} + +func (m GitHubUserModel) View() string { + if m.submitted { + return "" + } + + var b strings.Builder + + // Title + b.WriteString("Enter GitHub User Details\n\n") + + // Input fields + for i := range m.inputs { + b.WriteString(m.inputs[i].View()) + if i < len(m.inputs)-1 { + b.WriteRune('\n') + } + } + + if m.tempAuthShow { + toggleText := "[ ]" + if m.tempAuth { + toggleText = "[X]" + } + + toggleBtn := fmt.Sprintf("[ TempAuthor ] %s ", toggleText) + + if m.focusIndex == len(m.inputs) { // When toggle is focused + b.WriteString("\n" + focusedStyle.Render(toggleBtn)) + } else { + b.WriteString("\n" + blurredStyle.Render(toggleBtn)) + } + } + + // Submit button + button := blurredButton + if m.focusIndex == len(m.inputs)+1 && m.tempAuthShow || m.focusIndex == len(m.inputs) && !m.tempAuthShow { + button = focusedButton + } + b.WriteString("\n\n" + button + "\n") + + // Error message + if m.showError { + b.WriteString("\n" + errorStyle.Render(m.errorMsg) + "\n") + } + + // Help text + b.WriteString("\n" + blurredStyle.Render("tab to navigate • enter to submit")) + + return b.String() +} + +// RunForm starts the TUI and returns the entered values +func RunForm() (string, string, error) { + model := NewGitHubUserForm(nil) + p := tea.NewProgram(model) + + m, err := p.Run() + if err != nil { + return "", "", err + } + + if fm, ok := m.(GitHubUserModel); ok { + if fm.submitted { + return fm.inputs[0].Value(), fm.inputs[1].Value(), nil + } + } + + return "", "", nil +} \ No newline at end of file From 5c41cd4038cbec108b5cb8ceefad56319b67494b Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Thu, 10 Apr 2025 22:31:34 +0200 Subject: [PATCH 2/5] refactor: edit existing tui elements to add new gh add tui component --- src/cmd/tui/tui_author.go | 27 +++++++++++++++++++++++++++ src/cmd/tui/tui_list.go | 9 +++++++++ 2 files changed, 36 insertions(+) diff --git a/src/cmd/tui/tui_author.go b/src/cmd/tui/tui_author.go index 1d16075..0848e07 100644 --- a/src/cmd/tui/tui_author.go +++ b/src/cmd/tui/tui_author.go @@ -137,6 +137,33 @@ func intitialErrorModel() *errorModel { } } +func createGHTempAuthorModel(old_m *model, user utils.User) model_ca { + parent_m = old_m + m := model_ca{ + inputs: make([]textinput.Model, 2), + errorModel: intitialErrorModel(), + } + var t textinput.Model + for i := range m.inputs { + t = textinput.New() + t.Cursor.Style = cursorStyle + switch i { + case 0: + t.Placeholder = "Username (e.g. JohnDoe-gh)" + t.SetValue(user.Username) + t.Focus() + t.PromptStyle = focusedStyle + t.TextStyle = focusedStyle + case 1: + t.Placeholder = "Email (e.g. JohnDoe@domain.do)" + t.SetValue(user.Email) + } + m.inputs[i] = t + } + tempAuthorToggle = true + return m +} + func createGHAuthorModel(old_m *model, user utils.User) model_ca { parent_m = old_m diff --git a/src/cmd/tui/tui_list.go b/src/cmd/tui/tui_list.go index 29884ff..3247622 100644 --- a/src/cmd/tui/tui_list.go +++ b/src/cmd/tui/tui_list.go @@ -47,6 +47,7 @@ type listKeyMap struct { createAuthor key.Binding deleteAuthor key.Binding tempAdd key.Binding + ghAdd key.Binding } func newListKeyMap() *listKeyMap { @@ -79,6 +80,10 @@ func newListKeyMap() *listKeyMap { key.WithKeys("T"), key.WithHelp("T", "Add temporary author"), ), + ghAdd: key.NewBinding( + key.WithKeys("c"), + key.WithHelp("c", "Add GitHub author"), + ), } } @@ -180,6 +185,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // Handle keys from keyList (help menu) switch { + case key.Matches(msg, m.keys.ghAdd): + sub_model = NewGitHubUserForm(&m) + return m, tea.ClearScreen + case key.Matches(msg, m.keys.negation): i, ok := m.list.SelectedItem().(item) if ok { From 02962b7c00322e45665b69501417016ac414a691 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Thu, 10 Apr 2025 22:31:44 +0200 Subject: [PATCH 3/5] refactor: no args means tui opens --- src/cmd/gh.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cmd/gh.go b/src/cmd/gh.go index b29de2c..c7ce7bc 100644 --- a/src/cmd/gh.go +++ b/src/cmd/gh.go @@ -1,13 +1,13 @@ -/* -Copyright © 2025 NAME HERE -*/ package cmd import ( "fmt" + "os" + "strings" "github.com/Slug-Boi/cocommit/src/cmd/tui" "github.com/Slug-Boi/cocommit/src/cmd/utils" + //"github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" ) @@ -20,7 +20,7 @@ func GHCmd () *cobra.Command { Long: `This command will create add a github profile to your author list. You just have to run the command with a username of the github profile you want to add. The email will be added manually by following the TUI or adding the email flag to the command.`, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { email, _ := cmd.Flags().GetString("email") shortname, _ := cmd.Flags().GetString("shortname") @@ -29,6 +29,19 @@ func GHCmd () *cobra.Command { groups, _ := cmd.Flags().GetStringSlice("groups") exclude, _ := cmd.Flags().GetBool("exclude") + if len(args) == 0 { + username, email_out, err := tui.RunForm() + if err != nil { + panic(fmt.Sprintf("Error: %v", err)) + } + if username == "" { + os.Exit(0) + } + + args = append(args, username) + email = strings.TrimSpace(email_out) + } + user := utils.FetchGithubProfile(args[0]) // Update values if flags are set From 55b78b066cbe1361c9974a76cb51276f9df73ca9 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Fri, 11 Apr 2025 15:32:50 +0200 Subject: [PATCH 4/5] fix: fix tui wrap around issue with temp author --- src/cmd/tui/tui_author.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cmd/tui/tui_author.go b/src/cmd/tui/tui_author.go index 0848e07..22d0da6 100644 --- a/src/cmd/tui/tui_author.go +++ b/src/cmd/tui/tui_author.go @@ -50,7 +50,7 @@ func errorGetMissingFields(m model_ca) { } if len(m.inputs) > 0 { - for i := 0; i < inpLen-1; i++ { + for i := 0; i < inpLen; i++ { if m.inputs[i].Value() == "" { m.errorModel.missing = append(m.errorModel.missing, "- "+strings.Split(m.inputs[i].Placeholder," (")[0]) } @@ -330,10 +330,15 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.focusIndex++ } - if m.focusIndex > len(m.inputs)+1 { + inpNum := len(m.inputs) + if !tempAuthorToggle { + inpNum++ + } + + if m.focusIndex > inpNum { m.focusIndex = 0 } else if m.focusIndex < 0 { - m.focusIndex = len(m.inputs) + m.focusIndex = inpNum } cmds := make([]tea.Cmd, len(m.inputs)) From c6fec9e840ce8f5f283a6f48811058d85f214a01 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Fri, 11 Apr 2025 15:33:05 +0200 Subject: [PATCH 5/5] fix: clear screen to remove ghost elements --- src/cmd/tui/tui_github.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/tui/tui_github.go b/src/cmd/tui/tui_github.go index bd9b61f..e5f0c99 100644 --- a/src/cmd/tui/tui_github.go +++ b/src/cmd/tui/tui_github.go @@ -88,9 +88,9 @@ func (m GitHubUserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { user.Email = m.inputs[1].Value() } if m.tempAuth { - return createGHTempAuthorModel(parent_m,user), nil + return createGHTempAuthorModel(parent_m,user), tea.ClearScreen } - return createGHAuthorModel(parent_m,user), nil + return createGHAuthorModel(parent_m,user), tea.ClearScreen } else if s == "enter" && m.focusIndex == len(m.inputs) && m.tempAuthShow { //toggle temp mode