Files
Archduino/internal/tui/state.go

79 lines
2.1 KiB
Go

package tui
import (
"sort"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/config"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/module"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/theming"
)
// Theme is a named colorscheme presented in the wizard. It is a thin
// display value for the dropdown; the authoritative theme catalogue lives
// in the theming package (theming.List / theming.Get).
type Theme struct {
ID string
Name string
}
// ModuleChoice is one row in the module selector.
type ModuleChoice struct {
ID string
Name string
Description string
Selected bool
}
// WizardState is the payload the wizard fills in and the dashboard reads.
type WizardState struct {
ThemeID string
Modules []ModuleChoice
Parallelism int // advanced; reserved for a future concurrent engine
HaltOnError bool // advanced
Confirmed bool // true once the user hits Confirm on the last screen
}
// NewWizardState builds an initial state from the running config and the
// registered modules. Pre-ticks all modules except those disabled in the
// overlay, defaults the theme to the first built-in theme.
func NewWizardState(cfg config.Config, reg *module.Registry) WizardState {
disabled := map[string]bool{}
for _, id := range cfg.DisabledModules {
disabled[id] = true
}
mods := reg.All()
choices := make([]ModuleChoice, 0, len(mods))
for _, m := range mods {
meta := m.Meta()
choices = append(choices, ModuleChoice{
ID: meta.ID,
Name: meta.Name,
Description: meta.Description,
Selected: !disabled[meta.ID],
})
}
sort.Slice(choices, func(i, j int) bool { return choices[i].ID < choices[j].ID })
theme := theming.Default().ID
if override, ok := cfg.ThemeOverrides["__active"]; ok && override != "" {
theme = override
}
return WizardState{
ThemeID: theme,
Modules: choices,
Parallelism: 1,
}
}
// SelectedIDs returns only the checked module IDs.
func (s WizardState) SelectedIDs() []string {
out := make([]string, 0, len(s.Modules))
for _, m := range s.Modules {
if m.Selected {
out = append(out, m.ID)
}
}
return out
}