mirror of
https://github.com/EnumeratedDev/typer.git
synced 2026-09-16 13:36:12 +00:00
Add configuration files for styles, keybindings and other options
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v3"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type TyperConfig struct {
|
||||
SelectedStyle string `yaml:"selected_style,omitempty"`
|
||||
FallbackStyle string `yaml:"fallback_style,omitempty"`
|
||||
TabIndentation int `yaml:"tab_indentation,omitempty"`
|
||||
}
|
||||
|
||||
var Config TyperConfig
|
||||
|
||||
func readConfig() {
|
||||
Config = TyperConfig{
|
||||
SelectedStyle: "default",
|
||||
FallbackStyle: "default-fallback",
|
||||
TabIndentation: 4,
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not get home directory: %s", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Join(homeDir, ".config/typer/config.yml")); err == nil {
|
||||
data, err := os.ReadFile(path.Join(homeDir, ".config/typer/config.yml"))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read config.yml: %s", err)
|
||||
}
|
||||
err = yaml.Unmarshal(data, &Config)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not unmarshal config.yml: %s", err)
|
||||
}
|
||||
} else if _, err := os.Stat("/etc/typer/config.yml"); err == nil {
|
||||
reader, err := os.Open("/etc/typer/config.yml")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read config.yml: %s", err)
|
||||
}
|
||||
err = yaml.NewDecoder(reader).Decode(&Config)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read config.yml: %s", err)
|
||||
}
|
||||
reader.Close()
|
||||
}
|
||||
|
||||
// Validate config options
|
||||
if Config.TabIndentation < 1 {
|
||||
Config.TabIndentation = 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user