diff --git a/application.go b/application.go index c966350..3d5b63b 100644 --- a/application.go +++ b/application.go @@ -1,7 +1,6 @@ package stone import "image" -import "image/color" // Application represents an application. type Application struct { @@ -25,29 +24,7 @@ func (application *Application) Run () ( if height < 1 { height = 20 } application.DamageBuffer.SetSize(width, height) - // TODO: load these from a file - application.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: 0x00, B: 0xFF, A: 0xFF }, - // purple - color.RGBA { R: 0x80, G: 0x00, B: 0xFF, A: 0xFF }, - } - application.config.fontName = "" - application.config.fontSize = 11 - - application.config.padding = 2 + application.config.load() application.backend, err = instantiateBackend(application) if err != nil { return } diff --git a/config.go b/config.go index c731075..74765ba 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,11 @@ 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. @@ -35,3 +40,117 @@ func (config *Config) FontName () (fontName string) { fontName = config.fontName return } + +func (config *Config) load () { + // TODO: load these from a file + 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: 0x00, B: 0xFF, A: 0xFF }, + // purple + color.RGBA { R: 0x80, G: 0x00, 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 + + 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 "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 + } + } +}