Skip to content

Commit

Permalink
go through each email with autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua committed Sep 6, 2024
1 parent 702ccef commit 852cd61
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions cmd/generate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -80,8 +78,6 @@ func NewConfigCommand() *cobra.Command {
}

func run(opts *Options) error {
logger, err := gopherlogs.NewLogger()

attributionMap := make(map[string][]string)

// Open repo
Expand All @@ -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`
Expand All @@ -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{}
Expand All @@ -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")),
}
}

Expand All @@ -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,
}
}

Expand All @@ -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)
Expand All @@ -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),
)
}

0 comments on commit 852cd61

Please sign in to comment.