Checkpoint
This commit is contained in:
223 files changed
+14770
-18352
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/gohcl"
|
||||
"github.com/hashicorp/hcl/v2/hclsyntax"
|
||||
"github.com/hashicorp/hcl/v2/hclwrite"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Settings are the user-adjustable settings.
|
||||
type Settings struct {
|
||||
Display DisplaySettings `hcl:"display,block" icon:"🖥️"`
|
||||
Player PlayerSettings ` icon:"🎧"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type PlayerSettings struct {
|
||||
Pause bool
|
||||
}
|
||||
|
||||
type SpectrumSettings struct {
|
||||
Waterfall bool `hcl:"waterfall" menu:"Use waterfall mode"`
|
||||
Mono bool `hcl:"mono" menu:"Use mono"`
|
||||
Hold uint8 `hcl:"hold" min:"0" max:"100"`
|
||||
MinFrequency int `hcl:"min" min:"50" max:"22050" step:"50" menu:"Low pass filter"`
|
||||
MaxFrequency int `hcl:"max" min:"50" max:"22050" step:"50" menu:"High pass filter"`
|
||||
}
|
||||
|
||||
type SystemSettings struct {
|
||||
LoadAverage string `label:"Load average"`
|
||||
LocalTime string `label:"Local time"`
|
||||
}
|
||||
|
||||
var DefaultSettings = Settings{
|
||||
Display: DisplaySettings{
|
||||
Contrast: 0x80,
|
||||
Sleep: "hour",
|
||||
},
|
||||
Spectrum: SpectrumSettings{
|
||||
Hold: 10,
|
||||
MinFrequency: 100,
|
||||
MaxFrequency: 15000,
|
||||
},
|
||||
}
|
||||
|
||||
func LoadUserSettings(name string) *Settings {
|
||||
log := logrus.WithField("path", name)
|
||||
log.Debug("loading configuration")
|
||||
|
||||
source, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warn("user settings do not exist; using defaults")
|
||||
} else {
|
||||
log.WithError(err).Warn("user settings contains errors; using defaults")
|
||||
}
|
||||
return &DefaultSettings
|
||||
}
|
||||
|
||||
var (
|
||||
settings = new(Settings)
|
||||
ctx *hcl.EvalContext
|
||||
file *hcl.File
|
||||
diag hcl.Diagnostics
|
||||
)
|
||||
|
||||
start := hcl.Pos{Line: 1, Column: 1}
|
||||
if file, diag = hclsyntax.ParseConfig(source, name, start); diag.HasErrors() {
|
||||
log.WithError(diag).Warn("user settings contains errors; using defaults")
|
||||
return &DefaultSettings
|
||||
}
|
||||
|
||||
if diag = gohcl.DecodeBody(file.Body, ctx, settings); diag.HasErrors() {
|
||||
log.WithError(diag).Warn("user settings contains errors; using defaults")
|
||||
return &DefaultSettings
|
||||
}
|
||||
|
||||
return settings
|
||||
}
|
||||
|
||||
func SaveUserSettings(name string, settings *Settings) error {
|
||||
file := hclwrite.NewEmptyFile()
|
||||
root := file.Body()
|
||||
gohcl.EncodeIntoBody(settings, root)
|
||||
root.AppendUnstructuredTokens(hclwrite.Tokens{
|
||||
&hclwrite.Token{
|
||||
Type: hclsyntax.TokenComment,
|
||||
Bytes: []byte(fmt.Sprintf("\n# BeatGlow user settings\n# Saved at %s\n", time.Now())),
|
||||
SpacesBefore: 0,
|
||||
},
|
||||
})
|
||||
//logrus.Println(string(file.Bytes()[:]))
|
||||
|
||||
_ = os.Rename(name, name+".bak")
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
_ = os.Rename(name+".bak", name)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = file.WriteTo(f); err != nil {
|
||||
_ = f.Close()
|
||||
_ = os.Rename(name+".bak", name)
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Close()
|
||||
}
|
||||
Reference in new issue
Block a user