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"
"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:

View File

@@ -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
}