Refactored the menu system
This commit is contained in:
12 files changed
+474
-344
No files matched your search
+32
-6
@@ -14,15 +14,16 @@ import (
|
||||
|
||||
// Settings are the user-adjustable settings.
|
||||
type Settings struct {
|
||||
Display DisplaySettings `hcl:"display,block" icon:"🖥️"`
|
||||
Player PlayerSettings ` icon:"🎧"`
|
||||
Path string `menu:"-"`
|
||||
Display DisplaySettings `hcl:"display,block" icon:"md_monitor_speaker"`
|
||||
Player PlayerSettings ` icon:"md_headphones"`
|
||||
Spectrum SpectrumSettings `hcl:"spectrum,block" icon:"md_chart_line"`
|
||||
System SystemSettings ` icon:"md_raspberry_pi"`
|
||||
}
|
||||
|
||||
type DisplaySettings struct {
|
||||
Contrast uint8 `hcl:"contrast" menu:"Display contrast" step:"16"`
|
||||
Sleep string `hcl:"sleep" menu:"Display sleep time" options:"5min,15min,hour,day"`
|
||||
Contrast uint8 `hcl:"contrast" menu:"Display contrast" icon:"md_contrast_box"`
|
||||
Sleep string `hcl:"sleep" menu:"Display sleep time" options:"5min,15min,hour,day" icon:"md_power"`
|
||||
}
|
||||
|
||||
type PlayerSettings struct {
|
||||
@@ -54,9 +55,28 @@ var DefaultSettings = Settings{
|
||||
},
|
||||
}
|
||||
|
||||
var DefaultUserSettingsPath = []string{
|
||||
os.ExpandEnv("/etc/beatglow/settings.conf"),
|
||||
os.ExpandEnv("${HOME}/.config/beatglow/settings.conf"),
|
||||
os.ExpandEnv("${HOME}/.beatglow-settings.conf"),
|
||||
}
|
||||
|
||||
func LoadUserSettings(name string) *Settings {
|
||||
if name == "" {
|
||||
for _, item := range DefaultUserSettingsPath {
|
||||
if _, err := os.Stat(item); err == nil {
|
||||
name = item
|
||||
break
|
||||
}
|
||||
}
|
||||
if name == "" {
|
||||
logrus.Warn("user settings does not exist; using defaults")
|
||||
return &DefaultSettings
|
||||
}
|
||||
}
|
||||
|
||||
log := logrus.WithField("path", name)
|
||||
log.Debug("loading configuration")
|
||||
log.Info("loading user settings")
|
||||
|
||||
source, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
@@ -69,7 +89,7 @@ func LoadUserSettings(name string) *Settings {
|
||||
}
|
||||
|
||||
var (
|
||||
settings = new(Settings)
|
||||
settings = &Settings{Path: name}
|
||||
ctx *hcl.EvalContext
|
||||
file *hcl.File
|
||||
diag hcl.Diagnostics
|
||||
@@ -90,6 +110,12 @@ func LoadUserSettings(name string) *Settings {
|
||||
}
|
||||
|
||||
func SaveUserSettings(name string, settings *Settings) error {
|
||||
if name == "" {
|
||||
name = DefaultUserSettingsPath[len(DefaultUserSettingsPath)-1]
|
||||
}
|
||||
|
||||
logrus.WithField("path", name).Info("saving user settings")
|
||||
|
||||
file := hclwrite.NewEmptyFile()
|
||||
root := file.Body()
|
||||
gohcl.EncodeIntoBody(settings, root)
|
||||
|
||||
Reference in new issue
Block a user