stone/config.go

172 lines
3.9 KiB
Go
Raw Permalink Normal View History

2022-10-31 13:51:28 -06:00
package stone
import "os"
import "bufio"
import "strings"
import "strconv"
2022-10-31 13:51:28 -06:00
import "image/color"
import "path/filepath"
2022-10-31 13:51:28 -06:00
2022-11-08 23:01:13 -07:00
// Config stores configuration parameters. Backends only should honor parameters
// that they can support.
2022-10-31 13:51:28 -06:00
type Config struct {
2022-11-15 09:16:29 -07:00
colors [8]color.Color
2022-10-31 13:51:28 -06:00
padding int
center bool
2022-10-31 13:51:28 -06:00
fontSize int
fontName string
}
2022-11-08 23:01:13 -07:00
// Color returns the color value at the specified index.
2022-10-31 13:51:28 -06:00
func (config *Config) Color (index Color) (value color.Color) {
value = config.colors[index]
return
}
2022-11-08 23:01:13 -07:00
// Padding specifies how many cell's worth of padding should be on all sides of
// the buffer.
2022-10-31 13:51:28 -06:00
func (config *Config) Padding () (padding int) {
padding = config.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
return
}
2022-11-08 23:01:13 -07:00
// FontSize specifies how big the font should be.
2022-10-31 13:51:28 -06:00
func (config *Config) FontSize () (fontSize int) {
fontSize = config.fontSize
return
}
2022-11-08 23:01:13 -07:00
// FontName specifies the name of the font to use.
2022-10-31 13:51:28 -06:00
func (config *Config) FontName () (fontName string) {
fontName = config.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 },
// red
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
// orange
color.RGBA { R: 0xFF, G: 0x80, 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 },
}
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"))
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 "colorRed":
config.colors[ColorRed] = valueColor
case "colorOrange":
config.colors[ColorOrange] = 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
}
}
}