Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1435c02354 | |||
| 19895e6049 | |||
| 73ae475a7d | |||
| 639e43cfa7 | |||
| 46b2ca3d43 | |||
| 3cfe8be7bb | |||
| 863e415310 | |||
| 05ddfef584 | |||
| e60a990d10 | |||
| a42dd60a16 | |||
| a6e4ed9934 | |||
| 9d2872f256 |
@@ -1,8 +1,5 @@
|
||||
package x
|
||||
|
||||
// TODO: rename this file? lol
|
||||
|
||||
// import "fmt"
|
||||
import "unicode"
|
||||
import "github.com/jezek/xgb/xproto"
|
||||
import "github.com/jezek/xgbutil/keybind"
|
||||
@@ -112,6 +109,10 @@ var keypadCodeTable = map[xproto.Keysym] stone.Button {
|
||||
0xffb9: stone.Button('9'),
|
||||
}
|
||||
|
||||
// keycodeToButton converts an X keycode to a stone button code. It implements
|
||||
// a more fleshed out version of some of the logic found in
|
||||
// xgbutil/keybind/encoding.go to get a full keycode to keysym conversion, but
|
||||
// eliminates redundant work by going straight to a button code.
|
||||
func (backend *Backend) keycodeToButton (
|
||||
keycode xproto.Keycode,
|
||||
state uint16,
|
||||
@@ -119,19 +120,6 @@ func (backend *Backend) keycodeToButton (
|
||||
button stone.Button,
|
||||
numberPad bool,
|
||||
) {
|
||||
shift :=
|
||||
state & xproto.ModMaskShift > 0 ||
|
||||
state & backend.modifierMasks.shiftLock > 0
|
||||
capsLock := state & backend.modifierMasks.capsLock > 0
|
||||
numLock := state & backend.modifierMasks.numLock > 0
|
||||
|
||||
symbol1 := keybind.KeysymGet(backend.connection, keycode, 0)
|
||||
symbol2 := keybind.KeysymGet(backend.connection, keycode, 1)
|
||||
symbol3 := keybind.KeysymGet(backend.connection, keycode, 2)
|
||||
symbol4 := keybind.KeysymGet(backend.connection, keycode, 3)
|
||||
|
||||
cased := false
|
||||
|
||||
// PARAGRAPH 3
|
||||
//
|
||||
// A list of KeySyms is associated with each KeyCode. The list is
|
||||
@@ -145,6 +133,10 @@ func (backend *Backend) keycodeToButton (
|
||||
// treated as if it were the list ``K1 K2 K3 NoSymbol''. When an
|
||||
// explicit ``void'' element is desired in the list, the value
|
||||
// VoidSymbol can be used.
|
||||
symbol1 := keybind.KeysymGet(backend.connection, keycode, 0)
|
||||
symbol2 := keybind.KeysymGet(backend.connection, keycode, 1)
|
||||
symbol3 := keybind.KeysymGet(backend.connection, keycode, 2)
|
||||
symbol4 := keybind.KeysymGet(backend.connection, keycode, 3)
|
||||
switch {
|
||||
case symbol2 == 0 && symbol3 == 0 && symbol4 == 0:
|
||||
symbol3 = symbol1
|
||||
@@ -154,15 +146,11 @@ func (backend *Backend) keycodeToButton (
|
||||
case symbol4 == 0:
|
||||
symbol4 = 0
|
||||
}
|
||||
|
||||
symbol1Rune := keysymToRune(symbol1)
|
||||
symbol2Rune := keysymToRune(symbol2)
|
||||
symbol3Rune := keysymToRune(symbol3)
|
||||
symbol4Rune := keysymToRune(symbol4)
|
||||
|
||||
// FIXME: we ignore mode switch stuff
|
||||
_ = symbol4Rune
|
||||
|
||||
// PARAGRAPH 4
|
||||
//
|
||||
// The first four elements of the list are split into two groups of
|
||||
@@ -175,6 +163,7 @@ func (backend *Backend) keycodeToButton (
|
||||
// group should be treated as if the first element were the lowercase
|
||||
// form of ``K'' and the second element were the uppercase form of
|
||||
// ``K.''
|
||||
cased := false
|
||||
if symbol2 == 0 {
|
||||
upper := unicode.IsUpper(symbol1Rune)
|
||||
lower := unicode.IsLower(symbol1Rune)
|
||||
@@ -200,15 +189,59 @@ func (backend *Backend) keycodeToButton (
|
||||
}
|
||||
}
|
||||
|
||||
var selectedKeysym xproto.Keysym
|
||||
var selectedRune rune
|
||||
// PARAGRAPH 5
|
||||
//
|
||||
// The standard rules for obtaining a KeySym from a KeyPress event make
|
||||
// use of only the Group 1 and Group 2 KeySyms; no interpretation of/
|
||||
// other KeySyms in the list is given. Which group to use is determined
|
||||
// by the modifier state. Switching between groups is controlled by the
|
||||
// KeySym named MODE SWITCH, by attaching that KeySym to some KeyCode
|
||||
// and attaching that KeyCode to any one of the modifiers Mod1 through
|
||||
// Mod5. This modifier is called the group modifier. For any KeyCode,
|
||||
// Group 1 is used when the group modifier is off, and Group 2 is used
|
||||
// when the group modifier is on.
|
||||
modeSwitch := state & backend.modifierMasks.modeSwitch > 0
|
||||
if modeSwitch {
|
||||
symbol1 = symbol3
|
||||
symbol1Rune = symbol3Rune
|
||||
symbol2 = symbol4
|
||||
symbol2Rune = symbol4Rune
|
||||
|
||||
}
|
||||
|
||||
_, symbol2IsNumPad := keypadCodeTable[symbol2]
|
||||
// PARAGRAPH 6
|
||||
//
|
||||
// The Lock modifier is interpreted as CapsLock when the KeySym named
|
||||
// XK_Caps_Lock is attached to some KeyCode and that KeyCode is attached
|
||||
// to the Lock modifier. The Lock modifier is interpreted as ShiftLock
|
||||
// when the KeySym named XK_Shift_Lock is attached to some KeyCode and
|
||||
// that KeyCode is attached to the Lock modifier. If the Lock modifier
|
||||
// could be interpreted as both CapsLock and ShiftLock, the CapsLock
|
||||
// interpretation is used.
|
||||
shift :=
|
||||
state & xproto.ModMaskShift > 0 ||
|
||||
state & backend.modifierMasks.shiftLock > 0
|
||||
capsLock := state & backend.modifierMasks.capsLock > 0
|
||||
|
||||
// "PARAGRAPH" 5
|
||||
// PARAGRAPH 7
|
||||
//
|
||||
// The operation of keypad keys is controlled by the KeySym named
|
||||
// XK_Num_Lock, by attaching that KeySym to some KeyCode and attaching
|
||||
// that KeyCode to any one of the modifiers Mod1 through Mod5 . This
|
||||
// modifier is called the numlock modifier. The standard KeySyms with
|
||||
// the prefix ``XK_KP_'' in their name are called keypad KeySyms; these
|
||||
// are KeySyms with numeric value in the hexadecimal range 0xFF80 to
|
||||
// 0xFFBD inclusive. In addition, vendor-specific KeySyms in the
|
||||
// hexadecimal range 0x11000000 to 0x1100FFFF are also keypad KeySyms.
|
||||
numLock := state & backend.modifierMasks.numLock > 0
|
||||
|
||||
// PARAGRAPH 8
|
||||
//
|
||||
// Within a group, the choice of KeySym is determined by applying the
|
||||
// first rule that is satisfied from the following list:
|
||||
var selectedKeysym xproto.Keysym
|
||||
var selectedRune rune
|
||||
_, symbol2IsNumPad := keypadCodeTable[symbol2]
|
||||
switch {
|
||||
case numLock && symbol2IsNumPad:
|
||||
// The numlock modifier is on and the second KeySym is a keypad
|
||||
@@ -261,6 +294,10 @@ func (backend *Backend) keycodeToButton (
|
||||
selectedRune = symbol2Rune
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// all of the below stuff is specific to stone's button codes. //
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
// look up in control code table
|
||||
var isControl bool
|
||||
button, isControl = buttonCodeTable[selectedKeysym]
|
||||
@@ -276,6 +313,9 @@ func (backend *Backend) keycodeToButton (
|
||||
return
|
||||
}
|
||||
|
||||
// keysymToRune takes in an X keysym and outputs a utf32 code point. This
|
||||
// function does not and should not handle keypad keys, as those are handled
|
||||
// by Backend.keycodeToButton.
|
||||
func keysymToRune (keysym xproto.Keysym) (character rune) {
|
||||
// X keysyms like 0xFF.. or 0xFE.. are non-character keys. these cannot
|
||||
// be converted so we return a zero.
|
||||
10
buffer.go
10
buffer.go
@@ -192,9 +192,13 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
|
||||
bytesWritten = len(bytes)
|
||||
|
||||
for _, character := range text {
|
||||
buffer.setRune(buffer.dot.x, buffer.dot.y, character)
|
||||
buffer.dot.x ++
|
||||
if buffer.dot.x > buffer.width { break }
|
||||
if character == '\n' {
|
||||
buffer.dot.x = 0
|
||||
buffer.dot.y ++
|
||||
} else {
|
||||
buffer.setRune(buffer.dot.x, buffer.dot.y, character)
|
||||
buffer.dot.x ++
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
202
config.go
202
config.go
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
324
config/config.go
Normal file
324
config/config.go
Normal file
@@ -0,0 +1,324 @@
|
||||
package config
|
||||
|
||||
import "io"
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "sort"
|
||||
import "bufio"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "image/color"
|
||||
import "path/filepath"
|
||||
|
||||
// when making changes to this file, look at
|
||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
|
||||
// Error represents an error that can be returned by functions or methods in
|
||||
// this module.
|
||||
type Error int
|
||||
|
||||
const (
|
||||
// ErrorIllegalName is thrown when an application name contains illegal
|
||||
// characters such as a slash.
|
||||
ErrorIllegalName Error = iota
|
||||
|
||||
// ErrorNoSeparator is thrown when a configuration file has an
|
||||
// incorrectly formatted key-value pair.
|
||||
ErrorNoSeparator
|
||||
|
||||
// ErrorUnknownParameter is thrown when an unknown key is encountered in
|
||||
// a configuration file.
|
||||
ErrorUnknownParameter
|
||||
|
||||
// ErrorWrongColorLength is thrown when a configuration file has a color
|
||||
// literal with a total length unequal to 7.
|
||||
ErrorWrongColorLength
|
||||
|
||||
// ErrorMalformedColorLiteral is thrown when a configuration file has an
|
||||
// improperly formatted color literal, or a color literal was expected
|
||||
// and something else was encountered.
|
||||
ErrorMalformedColorLiteral
|
||||
|
||||
// ErrorMalformedIntegerLiteral is thrown when a configuration file has
|
||||
// an improperly formatted integer literal, or an integer literal was
|
||||
// expected and something else was encountered.
|
||||
ErrorMalformedIntegerLiteral
|
||||
|
||||
// ErrorMalformedFloatLiteral is thrown when a configuration file has
|
||||
// an improperly formatted float literal, or a float literal was
|
||||
// expected and something else was encountered.
|
||||
ErrorMalformedFloatLiteral
|
||||
)
|
||||
|
||||
// Error returns a description of the error.
|
||||
func (err Error) Error () (description string) {
|
||||
switch err {
|
||||
case ErrorIllegalName:
|
||||
description = "name contains illegal characters"
|
||||
case ErrorNoSeparator:
|
||||
description = "key:value pair has no separator"
|
||||
case ErrorUnknownParameter:
|
||||
description = "unknown parameter"
|
||||
case ErrorWrongColorLength:
|
||||
description = "color literal has the wrong length"
|
||||
case ErrorMalformedColorLiteral:
|
||||
description = "malformed color literal"
|
||||
case ErrorMalformedIntegerLiteral:
|
||||
description = "malformed integer literal"
|
||||
case ErrorMalformedFloatLiteral:
|
||||
description = "malformed float literal"
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Type represents the data type of a configuration parameter.
|
||||
type Type int
|
||||
|
||||
const (
|
||||
// string
|
||||
// It is just a basic string with inner whitespace preserved. No quotes
|
||||
// should be used in the file.
|
||||
TypeString Type = iota
|
||||
|
||||
// Type: image/color.RGBA
|
||||
// Represented as a 24 bit hexadecimal number (case insensitive)
|
||||
// preceded with a # sign where the first two digits represent the red
|
||||
// channel, the middle two digits represent the green channel, and the
|
||||
// last two digits represent the blue channel.
|
||||
TypeColor
|
||||
|
||||
// Type: int
|
||||
// An integer literal, like 123456789
|
||||
TypeInteger
|
||||
|
||||
// Type: float64
|
||||
// A floating point literal, like 1234.56789
|
||||
TypeFloat
|
||||
|
||||
// Type: bool
|
||||
// Values true, yes, on, and 1 are all truthy (case insensitive) and
|
||||
// anything else is falsy.
|
||||
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/xdg/<name>/<name>.conf and
|
||||
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
|
||||
// variables are set - then it uses those.
|
||||
func (config *Config) Load (name string) (err error) {
|
||||
if nameIsIllegal(name) {
|
||||
err = ErrorIllegalName
|
||||
return
|
||||
}
|
||||
|
||||
for _, directory := range configDirs {
|
||||
path := filepath.Join(directory, name, name + ".conf")
|
||||
|
||||
file, fileErr := os.Open(path)
|
||||
if fileErr != nil { continue }
|
||||
parseErr := config.LoadFrom(file)
|
||||
defer file.Close()
|
||||
|
||||
if parseErr != nil {
|
||||
println (
|
||||
"config: error in file", path +
|
||||
":", parseErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// LoadFrom parses a configuration file from an io.Reader. Configuration files
|
||||
// are divided into lines where each line may be blank, a comment, or a
|
||||
// key-value pair. If the line is blank or begins with a # character, it is
|
||||
// ignored. Else, the line must have a key and a value separated by a colon.
|
||||
// Before they are processed, leading and trailing whitespace is trimmed from
|
||||
// the key and the value. Keys are case sensitive.
|
||||
func (config *Config) LoadFrom (reader io.Reader) (err error) {
|
||||
if config.LegalParameters == nil {
|
||||
config.LegalParameters = make(map[string] Type)
|
||||
}
|
||||
|
||||
if config.Parameters == nil {
|
||||
config.Parameters = make(map[string] any)
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
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 {
|
||||
err = ErrorNoSeparator
|
||||
return
|
||||
}
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.TrimSpace(value)
|
||||
|
||||
what, isKnown := config.LegalParameters[key]
|
||||
if !isKnown {
|
||||
err = ErrorUnknownParameter
|
||||
return
|
||||
}
|
||||
|
||||
switch what {
|
||||
case TypeString:
|
||||
config.Parameters[key] = value
|
||||
|
||||
case TypeColor:
|
||||
var valueColor color.Color
|
||||
valueColor, err = parseColor(value)
|
||||
if err != nil { return }
|
||||
config.Parameters[key] = valueColor
|
||||
|
||||
case TypeInteger:
|
||||
var valueInt int
|
||||
valueInt, err = strconv.Atoi(value)
|
||||
if err != nil {
|
||||
err = ErrorMalformedIntegerLiteral
|
||||
return
|
||||
}
|
||||
config.Parameters[key] = valueInt
|
||||
|
||||
case TypeFloat:
|
||||
var valueFloat float64
|
||||
valueFloat, err = strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
err = ErrorMalformedFloatLiteral
|
||||
return
|
||||
}
|
||||
config.Parameters[key] = valueFloat
|
||||
|
||||
case TypeBoolean:
|
||||
value = strings.ToLower(value)
|
||||
truthy :=
|
||||
value == "true" ||
|
||||
value == "yes" ||
|
||||
value == "on" ||
|
||||
value == "1"
|
||||
config.Parameters[key] = truthy
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Save overwrites the main user configuration file, which is located at
|
||||
// <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in
|
||||
// which case the value of that variable is used instead.
|
||||
func (config *Config) Save (name string) (err error) {
|
||||
if nameIsIllegal(name) {
|
||||
err = ErrorIllegalName
|
||||
return
|
||||
}
|
||||
|
||||
err = os.MkdirAll(configHome, 0755)
|
||||
if err != nil { return }
|
||||
|
||||
file, err := os.OpenFile (
|
||||
filepath.Join(configHome, name, name + ".conf"),
|
||||
os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0744)
|
||||
if err != nil { return }
|
||||
defer file.Close()
|
||||
|
||||
err = config.SaveTo(file)
|
||||
if err != nil { return }
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SaveTo writes the configuration data to the specified io.Writer. Keys are
|
||||
// alphabetically sorted.
|
||||
func (config *Config) SaveTo (writer io.Writer) (err error) {
|
||||
keys := make([]string, len(config.Parameters))
|
||||
index := 0
|
||||
for key, _ := range config.Parameters {
|
||||
keys[index] = key
|
||||
index ++
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
value := config.Parameters[key]
|
||||
switch value.(type) {
|
||||
case string:
|
||||
fmt.Fprintf(writer,"%s: %s\n", key, value.(string))
|
||||
|
||||
case color.RGBA:
|
||||
colorValue := value.(color.RGBA)
|
||||
colorInt :=
|
||||
uint64(colorValue.R) << 16 |
|
||||
uint64(colorValue.G) << 8 |
|
||||
uint64(colorValue.B)
|
||||
fmt.Fprintf(writer,"%s: #%06x\n", key, colorInt)
|
||||
|
||||
case int:
|
||||
fmt.Fprintf(writer,"%s: %d\n", key, value.(int))
|
||||
|
||||
case float64:
|
||||
fmt.Fprintf(writer,"%s: %f\n", key, value.(float64))
|
||||
|
||||
case bool:
|
||||
fmt.Fprintf(writer,"%s: %t\n", key, value.(bool))
|
||||
default:
|
||||
fmt.Fprintf(writer,"# %s: unknown type\n", key)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func parseColor (value string) (valueColor color.Color, err error) {
|
||||
if value[0] == '#' {
|
||||
if len(value) != 7 {
|
||||
err = ErrorWrongColorLength
|
||||
return
|
||||
}
|
||||
|
||||
var colorInt uint64
|
||||
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
|
||||
if err != nil {
|
||||
err = ErrorMalformedColorLiteral
|
||||
return
|
||||
}
|
||||
|
||||
valueColor = color.RGBA {
|
||||
R: uint8(colorInt >> 16),
|
||||
G: uint8(colorInt >> 8),
|
||||
B: uint8(colorInt),
|
||||
A: 0xFF,
|
||||
}
|
||||
} else {
|
||||
err = ErrorMalformedColorLiteral
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func nameIsIllegal (name string) (legal bool) {
|
||||
legal = strings.ContainsAny(name, "/\\|:.%")
|
||||
return
|
||||
}
|
||||
54
config/xdg.go
Normal file
54
config/xdg.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
import "strings"
|
||||
import "path/filepath"
|
||||
|
||||
var homeDirectory string
|
||||
var configHome string
|
||||
var configDirs []string
|
||||
var dataHome string
|
||||
var cacheHome string
|
||||
|
||||
func init () {
|
||||
var err error
|
||||
homeDirectory, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic("could not get user home directory: " + err.Error())
|
||||
}
|
||||
|
||||
configHome = os.Getenv("XDG_CONFIG_HOME")
|
||||
if configHome == "" {
|
||||
configHome = filepath.Join(homeDirectory, "/.config/")
|
||||
}
|
||||
|
||||
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
|
||||
if configDirsString == "" {
|
||||
configDirsString = "/etc/xdg/"
|
||||
}
|
||||
|
||||
configDirs = append(strings.Split(configDirsString, ":"), configHome)
|
||||
|
||||
configHome = os.Getenv("XDG_DATA_HOME")
|
||||
if configHome == "" {
|
||||
configHome = filepath.Join(homeDirectory, "/.local/share/")
|
||||
}
|
||||
|
||||
cacheHome = os.Getenv("XDG_CACHE_HOME")
|
||||
if cacheHome == "" {
|
||||
configHome = filepath.Join(homeDirectory, "/.cache/")
|
||||
}
|
||||
}
|
||||
|
||||
// DataHome returns the path to the directory where user data should be stored.
|
||||
func DataHome (name string) (home string) {
|
||||
home = filepath.Join(dataHome, name)
|
||||
return
|
||||
}
|
||||
|
||||
// CacheHome returns the path to the directory where cache files should be
|
||||
// stored.
|
||||
func CacheHome (name string) (home string) {
|
||||
home = filepath.Join(cacheHome, name)
|
||||
return
|
||||
}
|
||||
106
examples/confview/confview.go
Normal file
106
examples/confview/confview.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "image"
|
||||
import "image/color"
|
||||
import _ "image/png"
|
||||
import "git.tebibyte.media/sashakoshka/stone"
|
||||
import "git.tebibyte.media/sashakoshka/stone/config"
|
||||
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
|
||||
|
||||
var application = &stone.Application { }
|
||||
var inputState struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
var globalConfig config.Config
|
||||
|
||||
func main () {
|
||||
application.SetTitle("configuration viewer")
|
||||
application.SetSize(32, 16)
|
||||
|
||||
iconFile16, err := os.Open("assets/scaffold16.png")
|
||||
if err != nil { panic(err) }
|
||||
icon16, _, err := image.Decode(iconFile16)
|
||||
if err != nil { panic(err) }
|
||||
iconFile16.Close()
|
||||
iconFile32, err := os.Open("assets/scaffold32.png")
|
||||
if err != nil { panic(err) }
|
||||
icon32, _, err := image.Decode(iconFile32)
|
||||
if err != nil { panic(err) }
|
||||
iconFile16.Close()
|
||||
|
||||
application.SetIcon([]image.Image { icon16, icon32 })
|
||||
|
||||
application.OnPress(onPress)
|
||||
application.OnRelease(onRelease)
|
||||
application.OnMouseMove(onMouseMove)
|
||||
application.OnStart(onStart)
|
||||
application.OnResize(redraw)
|
||||
|
||||
err = application.Run()
|
||||
if err != nil { panic(err) }
|
||||
}
|
||||
|
||||
func onStart () {
|
||||
// this is just copy pasted from config.go
|
||||
globalConfig = 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 },
|
||||
},
|
||||
}
|
||||
globalConfig.Load("stone")
|
||||
redraw()
|
||||
}
|
||||
|
||||
func redraw () {
|
||||
// i fucking love go interfaces
|
||||
application.Clear()
|
||||
application.SetDot(0, 0)
|
||||
globalConfig.SaveTo(application)
|
||||
}
|
||||
|
||||
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
||||
}
|
||||
|
||||
func onRelease (button stone.Button) {
|
||||
}
|
||||
|
||||
func onMouseMove (x, y int) {
|
||||
inputState.x = x
|
||||
inputState.y = y
|
||||
}
|
||||
@@ -50,7 +50,8 @@ func onRelease (button stone.Button) {
|
||||
}
|
||||
}
|
||||
|
||||
func onMouseMove (x, y int) { if mousePressed {
|
||||
func onMouseMove (x, y int) {
|
||||
if mousePressed {
|
||||
application.SetRune(x, y, '#')
|
||||
application.Draw()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user