Skip to content

Commit

Permalink
Prevent choose template more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
ntk148v committed Nov 11, 2022
1 parent 34d63d1 commit 3b73d71
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

var chosenTemplates = map[string]struct{}{}

type delegateKeyMap struct {
// choose is the only option at this time
choose key.Binding
Expand Down Expand Up @@ -69,11 +71,20 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
case key.Matches(msg, keys.choose):
// Reset filter when template was chosen.
defer m.ResetFilter()

if _, ok := chosenTemplates[title]; ok {
return m.NewStatusMessage(statusMessageStyle("Template " + title + " is chosen once, skip..."))
}

// Copy .gitignore template
pwd, _ := os.Getwd()
if err := copyTemplate(path, filepath.Join(pwd, ".gitignore")); err != nil {
return m.NewStatusMessage(errorMessageStyle(err.Error()))
}

// Record the chosen template to prevent duplicate
chosenTemplates[title] = struct{}{}

return m.NewStatusMessage(statusMessageStyle("Use template " + title))
}
}
Expand Down

0 comments on commit 3b73d71

Please sign in to comment.