From 50cf9c7dd5f790dc3921b60ac274a2c27cd2135b Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 25 Feb 2023 17:51:37 -0800 Subject: [PATCH] add selectable item type --- cmd/commithook/gico.go | 3 --- ui/settings.go | 43 +++++++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/cmd/commithook/gico.go b/cmd/commithook/gico.go index 52a147a..2a2c7a7 100644 --- a/cmd/commithook/gico.go +++ b/cmd/commithook/gico.go @@ -6,7 +6,6 @@ import ( "os" "github.com/taigrr/gico/types" - "github.com/taigrr/gico/ui" ) var days [366]int @@ -29,8 +28,6 @@ func main() { increment() case "graph": printGraph() - case "interactive": - ui.InteractiveGraph() case "loadRepo": loadRepo() default: diff --git a/ui/settings.go b/ui/settings.go index 6f1c97f..5c8d267 100644 --- a/ui/settings.go +++ b/ui/settings.go @@ -18,9 +18,9 @@ const ( type ( SettingsCursor int Settings struct { - AllAuthors map[string]bool + AllAuthors selectablelist SelectedAuthors []string - AllRepos map[string]bool + AllRepos selectablelist SelectedRepos []string cursor SettingsCursor highlightedEntry int @@ -29,6 +29,25 @@ type ( } ) +type selectablelist []selectable + +type selectable struct { + text string + selected bool +} + +func (i selectable) Title() string { return i.text } +func (i selectable) FilterValue() string { return i.text } +func (i selectablelist) GetSelected() []string { + selected := []string{} + for _, v := range i { + if v.selected { + selected = append(selected, v.text) + } + } + return selected +} + var settingsKey = key.NewBinding( key.WithKeys("ctrl+g"), key.WithHelp("", "press ctrl+g to open settings"), @@ -69,16 +88,16 @@ func NewSettings() (Settings, error) { return m, err } - m.AllRepos = make(map[string]bool) + m.AllRepos = []selectable{} for _, v := range allRepos { - m.AllRepos[v] = true + m.AllRepos = append(m.AllRepos, selectable{text: v, selected: true}) } - m.AllAuthors = make(map[string]bool) + m.AllAuthors = []selectable{} for _, v := range allAuthors { - m.AllAuthors[v] = false + m.AllAuthors = append(m.AllAuthors, selectable{text: v, selected: false}) } - m.SelectedRepos = allRepos + m.SelectedRepos = m.AllRepos.GetSelected() email, _ := commits.GetAuthorEmail() if email != "" { m.SelectedAuthors = append(m.SelectedAuthors, email) @@ -87,8 +106,14 @@ func NewSettings() (Settings, error) { if name != "" { m.SelectedAuthors = append(m.SelectedAuthors, name) } - for _, v := range m.SelectedRepos { - m.AllAuthors[v] = true + for _, v := range m.SelectedAuthors { + inner: + for i, s := range m.AllAuthors { + if s.text == v { + m.AllAuthors[i].selected = true + break inner + } + } } return m, nil }