From 852cd6196abdf3503028b0f3fb69177f7e272fa1 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Fri, 6 Sep 2024 14:23:40 -0700 Subject: [PATCH] go through each email with autocomplete --- cmd/generate/config/config.go | 68 +++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/cmd/generate/config/config.go b/cmd/generate/config/config.go index 6bc9eb6..fc9f713 100644 --- a/cmd/generate/config/config.go +++ b/cmd/generate/config/config.go @@ -9,8 +9,6 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" - "github.com/jpmcb/gopherlogs" - "github.com/open-sauced/pizza-cli/pkg/logging" "github.com/spf13/cobra" "github.com/charmbracelet/bubbles/help" @@ -80,8 +78,6 @@ func NewConfigCommand() *cobra.Command { } func run(opts *Options) error { - logger, err := gopherlogs.NewLogger() - attributionMap := make(map[string][]string) // Open repo @@ -92,13 +88,14 @@ func run(opts *Options) error { commitIter, err := repo.CommitObjects() + var uniqueEmails []string commitIter.ForEach(func(c *object.Commit) error { name := c.Author.Name email := c.Author.Email // TODO: edge case- same email multiple names // eg: 'coding@zeu.dev' = 'zeudev' & 'Zeu Capua' - + if !opts.isInteractive { doesEmailExist := slices.Contains(attributionMap[name], email) if !doesEmailExist { @@ -106,19 +103,18 @@ func run(opts *Options) error { attributionMap[name] = append(attributionMap[name], email) } } else { - // TODO: INTERACTIVE: per unique email, set a name (existing or new or ignore) - var uniqueEmails []string - if slices.Contains(uniqueEmails, email) { + if !slices.Contains(uniqueEmails, email) { uniqueEmails = append(uniqueEmails, email) } - program := tea.NewProgram(initialModel(uniqueEmails)) - if _, err := program.Run(); err != nil { - logger.V(logging.LogError).Info(err.Error()) - } } return nil }) + // TODO: INTERACTIVE: per unique email, set a name (existing or new or ignore) + program := tea.NewProgram(initialModel(uniqueEmails)) + if _, err := program.Run(); err != nil { + return fmt.Errorf(err.Error()) + } // generate an output file // default: `~/.sauced.yaml` @@ -136,12 +132,12 @@ func run(opts *Options) error { type model struct { textInput textinput.Model - help help.Model - keymap keymap + help help.Model + keymap keymap attributionMap map[string][]string - uniqueEmails []string - currentIndex int + uniqueEmails []string + currentIndex int } type keymap struct{} @@ -151,7 +147,9 @@ func (k keymap) ShortHelp() []key.Binding { key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "complete")), key.NewBinding(key.WithKeys("ctrl+n"), key.WithHelp("ctrl+n", "next")), key.NewBinding(key.WithKeys("ctrl+p"), key.WithHelp("ctrl+p", "prev")), + key.NewBinding(key.WithKeys("ctrl+i"), key.WithHelp("ctrl+i", "ignore email")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "quit")), + key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "submit")), } } @@ -167,12 +165,12 @@ func initialModel(uniqueEmails []string) model { return model{ textInput: ti, - help: help.New(), - keymap: keymap{}, + help: help.New(), + keymap: keymap{}, attributionMap: make(map[string][]string), - uniqueEmails: uniqueEmails, - currentIndex: 0, + uniqueEmails: uniqueEmails, + currentIndex: 0, } } @@ -184,23 +182,31 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd currentEmail := m.uniqueEmails[m.currentIndex] - - existingUsers := make([]string, len(m.attributionMap)) + existingUsers := make([]string, 0, len(m.attributionMap)) for k := range m.attributionMap { - existingUsers = append(existingUsers, k) + existingUsers = append(existingUsers, k) } m.textInput.SetSuggestions(existingUsers) switch msg := msg.(type) { - case tea.KeyMsg: - switch msg.Type { - case tea.KeyCtrlC, tea.KeyEsc: - return m, tea.Quit - - case tea.KeyEnter: - m.attributionMap[currentEmail] = append(m.attributionMap[currentEmail], m.textInput.Value()) + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC, tea.KeyEsc: + return m, tea.Quit + + case tea.KeyCtrlI: + m.currentIndex++ + return m, nil + + case tea.KeyEnter: + m.attributionMap[m.textInput.Value()] = append(m.attributionMap[currentEmail], currentEmail) + m.currentIndex++ + if m.currentIndex > len(m.attributionMap) { + return m, tea.Quit } + return m, nil + } } m.textInput, cmd = m.textInput.Update(msg) @@ -212,7 +218,7 @@ func (m model) View() string { return fmt.Sprintf( "Found email %s - who to attribute to?: %s\n\n%s\n", m.uniqueEmails[m.currentIndex], - m.textInput.View(), + m.textInput.View(), m.help.View(m.keymap), ) }