Tomo will call the parse functions in Theme and Config

This commit is contained in:
Sasha Koshka 2023-02-03 17:50:45 -05:00
parent 2ff32ca8ea
commit 43fea5c8ba
4 changed files with 71 additions and 4 deletions

View File

@ -38,6 +38,8 @@ func NewBackend () (output tomo.Backend, err error) {
backend := &Backend {
windows: map[xproto.Window] *Window { },
doChannel: make(chan func (), 0),
theme: theme.Default { },
config: config.Default { },
}
// connect to X
@ -71,7 +73,11 @@ func (backend *Backend) Run () (err error) {
// Stop gracefully closes the connection and stops the event loop.
func (backend *Backend) Stop () {
backend.assert()
toClose := []*Window { }
for _, window := range backend.windows {
toClose = append(toClose, window)
}
for _, window := range toClose {
window.Close()
}
xevent.Quit(backend.connection)

View File

@ -2,8 +2,8 @@ package config
import "io"
// Parse parses a configuration file and returns it as a Config.
func Parse (source io.Reader) (config Config) {
// Parse parses one or more configuration files and returns them as a Config.
func Parse (sources ...io.Reader) (config Config) {
// TODO
return Default { }
}

View File

@ -2,8 +2,8 @@ package theme
import "io"
// Parse parses a theme file and returns it as a Theme.
func Parse (io.Reader) (Theme) {
// Parse parses one or more theme files and returns them as a Theme.
func Parse (sources ...io.Reader) (Theme) {
// TODO
return Default { }
}

61
tomo.go
View File

@ -1,5 +1,9 @@
package tomo
import "os"
import "io"
import "path/filepath"
import "git.tebibyte.media/sashakoshka/tomo/dirs"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
@ -12,6 +16,9 @@ var backend Backend
// the backend experiences a fatal error.
func Run (callback func ()) (err error) {
backend, err = instantiateBackend()
config := parseConfig()
backend.SetConfig(config)
backend.SetTheme(parseTheme(config.ThemePath()))
if callback != nil { callback() }
err = backend.Run()
backend = nil
@ -63,6 +70,60 @@ func SetConfig (config config.Config) {
backend.SetConfig(config)
}
func parseConfig () (config.Config) {
return parseMany [config.Config] (
dirs.ConfigDirs("tomo/tomo.conf"),
config.Parse,
config.Default { })
}
func parseTheme (path string) (theme.Theme) {
if path == "" { return theme.Default { } }
path = filepath.Join(path, "tomo")
// find all tomo pattern graph files in the directory
directory, err := os.Open(path)
if err != nil { return theme.Default { } }
names, _ := directory.Readdirnames(0)
paths := []string { }
for _, name := range names {
if filepath.Ext(name) == ".tpg" {
paths = append(paths, filepath.Join(path, name))
}
}
// parse them
return parseMany [theme.Theme] (
paths,
theme.Parse,
theme.Default { })
}
func parseMany [OBJECT any] (
paths []string,
parser func (...io.Reader) OBJECT,
fallback OBJECT,
) (
object OBJECT,
) {
// convert all paths into readers
sources := []io.Reader { }
for _, path := range paths {
file, err := os.Open(path)
if err != nil { continue }
sources = append(sources, file)
defer file.Close()
}
if sources == nil {
// if there are no readers, return the fallback object
return fallback
} else {
// if there are readers, parse them
return parser(sources...)
}
}
func assertBackend () {
if backend == nil { panic("no backend is running") }
}