🐛 fix(software-list): Flatten array

This commit is contained in:
punkfairie 2024-09-07 18:01:05 -07:00
parent b4c80068bf
commit c915314647
3 changed files with 51 additions and 13 deletions

29
flatten.go Normal file
View file

@ -0,0 +1,29 @@
package main
import (
"reflect"
)
func flatten(v ...interface{}) []string {
args := flattenDeep(nil, reflect.ValueOf(v))
var strings []string
for _, i := range args {
strings = append(strings, i.(string))
}
return strings
}
func flattenDeep(args []interface{}, v reflect.Value) []interface{} {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
args = flattenDeep(args, v.Index(i))
}
} else {
args = append(args, v.Interface())
}
return args
}

21
main.go
View file

@ -17,7 +17,7 @@ import (
var width, height, _ = term.GetSize(int(os.Stdout.Fd()))
type menu struct {
order SoftwarePackages
order []string
current int
keys keyMap
help help.Model
@ -26,7 +26,10 @@ type menu struct {
quitting bool
}
const softwareInstructionsFile = "/Users/marley/hackin/install.fairie/software-custom.yml"
const (
softwareInstructionsFile = "/Users/marley/hackin/install.fairie/home/.chezmoidata.yaml"
softwareGroup = "_Full-Desktop"
)
func initialModel() menu {
s := spinner.New()
@ -65,14 +68,14 @@ func (k keyMap) FullHelp() [][]key.Binding {
}
}
type yamlMsg YamlStructure
type softwareListMsg []string
type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
func (m menu) Init() tea.Cmd {
return tea.Batch(readYaml(softwareInstructionsFile), m.spinner.Tick)
return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick)
}
func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -81,8 +84,8 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case yamlMsg:
m.order = msg.SoftwarePackages
case softwareListMsg:
m.order = msg
return m, nil
case tea.KeyMsg:
@ -125,10 +128,8 @@ func (m menu) View() string {
software := list.New().Enumerator(softwareListEnumerator)
keys := sortMapKeys(m.order)
for _, k := range keys {
software.Item(m.order[k].Name)
for _, item := range m.order {
software.Item(item)
}
sidebarContent := software.String()

14
yaml.go
View file

@ -71,20 +71,28 @@ type osNames struct {
Windows *string `yaml:"windows"`
}
func readYaml(file string) tea.Cmd {
type ChezmoiData struct {
SoftwareGroups SoftwareGroups `yaml:"softwareGroups"`
}
type SoftwareGroups map[string]any
func getSoftwareList(file string) tea.Cmd {
return func() tea.Msg {
fileData, fileErr := os.ReadFile(file)
if fileErr != nil {
return errMsg{fileErr}
}
var parsedYaml YamlStructure
var parsedYaml ChezmoiData
yamlErr := yaml.Unmarshal(fileData, &parsedYaml)
if yamlErr != nil {
return errMsg{yamlErr}
}
return yamlMsg(parsedYaml)
list := flatten(parsedYaml.SoftwareGroups[softwareGroup])
return softwareListMsg(list)
}
}