Added a configuration system

This commit is contained in:
Sasha Koshka 2022-11-26 20:28:32 -05:00
parent a6e4ed9934
commit a42dd60a16
2 changed files with 249 additions and 132 deletions

202
config.go
View File

@ -1,15 +1,13 @@
package stone
import "os"
import "bufio"
import "strings"
import "strconv"
import "image/color"
import "path/filepath"
// Config stores configuration parameters. Backends only should honor parameters
// that they can support.
import "git.tebibyte.media/sashakoshka/stone/config"
// Config stores global, read-only configuration parameters that apply to all
// applications. Backends only should honor parameters that they can support.
type Config struct {
private config.Config
colors [8]color.Color
padding int
center bool
@ -18,154 +16,94 @@ type Config struct {
}
// Color returns the color value at the specified index.
func (config *Config) Color (index Color) (value color.Color) {
value = config.colors[index]
func (public *Config) Color (index Color) (value color.Color) {
value = public.colors[index]
return
}
// Padding specifies how many cell's worth of padding should be on all sides of
// the buffer.
func (config *Config) Padding () (padding int) {
padding = config.padding
func (public *Config) Padding () (padding int) {
padding = public.padding
return
}
// Center returns whether the buffer should be displayed in the center of the
// window like in kitty, or aligned to one corner like in gnome-terminal.
func (config *Config) Center () (center bool) {
center = config.center
func (public *Config) Center () (center bool) {
center = public.center
return
}
// FontSize specifies how big the font should be.
func (config *Config) FontSize () (fontSize int) {
fontSize = config.fontSize
func (public *Config) FontSize () (fontSize int) {
fontSize = public.fontSize
return
}
// FontName specifies the name of the font to use.
func (config *Config) FontName () (fontName string) {
fontName = config.fontName
func (public *Config) FontName () (fontName string) {
fontName = public.fontName
return
}
func (config *Config) load () {
config.colors = [8]color.Color {
// background
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
// foreground
color.RGBA { R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF },
// dim
color.RGBA { R: 0x80, G: 0x80, B: 0x80, A: 0xFF },
// red
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
// yellow
color.RGBA { R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF },
// green
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
// blue
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
// purple
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
func (public *Config) load () {
public.private = config.Config {
LegalParameters: map[string] config.Type {
"fontNormal": config.TypeString,
"fontSize": config.TypeInteger,
"padding": config.TypeInteger,
"center": config.TypeBoolean,
"colorBackground": config.TypeColor,
"colorForeground": config.TypeColor,
"colorDim": config.TypeColor,
"colorRed": config.TypeColor,
"colorYellow": config.TypeColor,
"colorGreen": config.TypeColor,
"colorBlue": config.TypeColor,
"colorPurple": config.TypeColor,
},
Parameters: map[string] any {
"fontNormal": "",
"fontSize": 11,
"padding": 2,
"center": false,
"colorBackground":
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
"colorForeground":
color.RGBA { R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF },
"colorDim":
color.RGBA { R: 0x80, G: 0x80, B: 0x80, A: 0xFF },
"colorRed":
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
"colorYellow":
color.RGBA { R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF },
"colorGreen":
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
"colorBlue":
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
"colorPurple":
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
},
}
config.fontName = ""
config.fontSize = 11
config.padding = 2
config.loadFile("/etc/stone/stone.conf")
homeDirectory, err := os.UserHomeDir()
if err != nil { return }
config.loadFile(filepath.Join(homeDirectory, "/.config/stone/stone.conf"))
public.private.Load("stone")
params := public.private.Parameters
public.fontName = params["fontNormal"].(string)
public.fontSize = params["fontSize"].(int)
public.padding = params["padding"].(int)
public.center = params["center"].(bool)
public.colors[ColorBackground] = params["colorBackground"].(color.RGBA)
public.colors[ColorForeground] = params["colorForeground"].(color.RGBA)
public.colors[ColorDim] = params["colorDim" ].(color.RGBA)
public.colors[ColorRed] = params["colorRed" ].(color.RGBA)
public.colors[ColorYellow] = params["colorYellow" ].(color.RGBA)
public.colors[ColorGreen] = params["colorGreen" ].(color.RGBA)
public.colors[ColorBlue] = params["colorBlue" ].(color.RGBA)
public.colors[ColorPurple] = params["colorPurple" ].(color.RGBA)
return
}
func (config *Config) loadFile (path string) {
file, err := os.Open(path)
if err != nil { return }
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if line[0] == '#' {
continue
}
key, value, found := strings.Cut(scanner.Text(), ":")
if !found {
println (
"config: error in file", path +
": key-value separator missing")
println(scanner.Text())
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
var valueInt int
var valueColor color.Color
var valueBoolean bool
if value == "true" {
valueBoolean = true
}
if value[0] == '#' {
if len(value) != 7 {
println (
"config: error in file", path +
": malformed color literal")
continue
}
colorInt, err := strconv.ParseUint(value[1:7], 16, 24)
if err != nil {
println (
"config: error in file", path +
": malformed color literal")
continue
}
valueColor = color.RGBA {
R: uint8(colorInt >> 16),
G: uint8(colorInt >> 8),
B: uint8(colorInt),
A: 0xFF,
}
} else {
valueInt, _ = strconv.Atoi(value)
}
switch key {
case "fontNormal":
config.fontName = value
case "fontSize":
config.fontSize = valueInt
case "padding":
config.padding = valueInt
case "center":
config.center = valueBoolean
case "colorBackground":
config.colors[ColorBackground] = valueColor
case "colorForeground":
config.colors[ColorForeground] = valueColor
case "colorDim":
config.colors[ColorDim] = valueColor
case "colorRed":
config.colors[ColorRed] = valueColor
case "colorYellow":
config.colors[ColorYellow] = valueColor
case "colorGreen":
config.colors[ColorGreen] = valueColor
case "colorBlue":
config.colors[ColorBlue] = valueColor
case "colorPurple":
config.colors[ColorPurple] = valueColor
}
}
}

179
config/config.go Normal file
View File

@ -0,0 +1,179 @@
package config
import "os"
import "bufio"
import "errors"
import "strings"
import "strconv"
import "image/color"
import "path/filepath"
// Type represents the data type of a configuration parameter.
type Type int
const (
// string
TypeString Type = iota
// image/color.RGBA
TypeColor
// int
TypeInteger
// float64
TypeFloat
// bool
TypeBoolean
)
// Config holds a list of configuration parameters.
type Config struct {
// LegalParameters holds the names and types of all parameters that can
// be parsed. If the parser runs into a parameter that is not listed
// here, it will print out an error message and keep on parsing.
LegalParameters map[string] Type
// Parameters holds the names and values of all parsed parameters. If a
// value is non-nil, it can be safely type asserted into whatever type
// was requested.
Parameters map[string] any
}
// Load loads and parses the files /etc/<name>/<name>.conf and
// <home>/.config/<name>/<name>.conf.
func (config *Config) Load (name string) (err error) {
if config.LegalParameters == nil {
config.LegalParameters = make(map[string] Type)
}
if config.Parameters == nil {
config.Parameters = make(map[string] any)
}
config.loadFile("/etc/" + name + "/" + name + ".conf")
var homeDirectory string
homeDirectory, err = os.UserHomeDir()
if err != nil { return }
config.loadFile(filepath.Join(homeDirectory, "/.config/" + name + "/" + name + ".conf"))
return
}
func (config *Config) loadFile (path string) {
file, err := os.Open(path)
if err != nil { return }
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if line[0] == '#' {
continue
}
key, value, found := strings.Cut(scanner.Text(), ":")
if !found {
println (
"config: error in file", path +
": key-value separator missing")
println(scanner.Text())
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
what, isKnown := config.LegalParameters[key]
if !isKnown {
println (
"config: error in file", path +
": unknown parameter")
println(scanner.Text())
continue
}
switch what {
case TypeString:
config.Parameters[key] = value
case TypeColor:
var valueColor color.Color
valueColor, err = parseColor(value)
if err != nil {
println (
"config: error in file", path +
":", err)
println(scanner.Text())
continue
}
config.Parameters[key] = valueColor
case TypeInteger:
var valueInt int
valueInt, err = strconv.Atoi(value)
if err != nil {
println (
"config: error in file", path +
": malformed integer literal")
println(scanner.Text())
continue
}
config.Parameters[key] = valueInt
case TypeFloat:
var valueFloat float64
valueFloat, err = strconv.ParseFloat(value, 64)
if err != nil {
println (
"config: error in file", path +
": malformed float literal")
println(scanner.Text())
continue
}
config.Parameters[key] = valueFloat
case TypeBoolean:
value = strings.ToLower(value)
truthy :=
value == "true" ||
value == "yes" ||
value == "on" ||
value == "1"
config.Parameters[key] = truthy
}
}
return
}
func parseColor (value string) (valueColor color.Color, err error) {
if value[0] == '#' {
if len(value) != 7 {
err = errors.New("wrong length color literal")
return
}
var colorInt uint64
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
if err != nil {
err = errors.New("malformed color literal")
return
}
valueColor = color.RGBA {
R: uint8(colorInt >> 16),
G: uint8(colorInt >> 8),
B: uint8(colorInt),
A: 0xFF,
}
} else {
err = errors.New("malformed color literal")
return
}
return
}