add selectable item type

This commit is contained in:
2023-02-25 17:51:37 -08:00
parent 91ae77071e
commit 50cf9c7dd5
2 changed files with 34 additions and 12 deletions

View File

@@ -6,7 +6,6 @@ import (
"os" "os"
"github.com/taigrr/gico/types" "github.com/taigrr/gico/types"
"github.com/taigrr/gico/ui"
) )
var days [366]int var days [366]int
@@ -29,8 +28,6 @@ func main() {
increment() increment()
case "graph": case "graph":
printGraph() printGraph()
case "interactive":
ui.InteractiveGraph()
case "loadRepo": case "loadRepo":
loadRepo() loadRepo()
default: default:

View File

@@ -18,9 +18,9 @@ const (
type ( type (
SettingsCursor int SettingsCursor int
Settings struct { Settings struct {
AllAuthors map[string]bool AllAuthors selectablelist
SelectedAuthors []string SelectedAuthors []string
AllRepos map[string]bool AllRepos selectablelist
SelectedRepos []string SelectedRepos []string
cursor SettingsCursor cursor SettingsCursor
highlightedEntry int 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( var settingsKey = key.NewBinding(
key.WithKeys("ctrl+g"), key.WithKeys("ctrl+g"),
key.WithHelp("", "press ctrl+g to open settings"), key.WithHelp("", "press ctrl+g to open settings"),
@@ -69,16 +88,16 @@ func NewSettings() (Settings, error) {
return m, err return m, err
} }
m.AllRepos = make(map[string]bool) m.AllRepos = []selectable{}
for _, v := range allRepos { 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 { 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() email, _ := commits.GetAuthorEmail()
if email != "" { if email != "" {
m.SelectedAuthors = append(m.SelectedAuthors, email) m.SelectedAuthors = append(m.SelectedAuthors, email)
@@ -87,8 +106,14 @@ func NewSettings() (Settings, error) {
if name != "" { if name != "" {
m.SelectedAuthors = append(m.SelectedAuthors, name) m.SelectedAuthors = append(m.SelectedAuthors, name)
} }
for _, v := range m.SelectedRepos { for _, v := range m.SelectedAuthors {
m.AllAuthors[v] = true inner:
for i, s := range m.AllAuthors {
if s.text == v {
m.AllAuthors[i].selected = true
break inner
}
}
} }
return m, nil return m, nil
} }