stone/config.go

172 lines
3.9 KiB
Go

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.
type Config struct {
colors [8]color.Color
padding int
center bool
fontSize int
fontName string
}
// Color returns the color value at the specified index.
func (config *Config) Color (index Color) (value color.Color) {
value = config.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
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
}
// FontSize specifies how big the font should be.
func (config *Config) FontSize () (fontSize int) {
fontSize = config.fontSize
return
}
// FontName specifies the name of the font to use.
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
}
}
}