-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go through each email with autocomplete
- Loading branch information
Showing
1 changed file
with
37 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,33 +88,33 @@ 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: '[email protected]' = 'zeudev' & 'Zeu Capua' | ||
|
||
if !opts.isInteractive { | ||
doesEmailExist := slices.Contains(attributionMap[name], email) | ||
if !doesEmailExist { | ||
// AUTOMATIC: set every name and associated emails | ||
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), | ||
) | ||
} |