Tomo will call the parse functions in Theme and Config
This commit is contained in:
		
							parent
							
								
									2ff32ca8ea
								
							
						
					
					
						commit
						43fea5c8ba
					
				@ -38,6 +38,8 @@ func NewBackend () (output tomo.Backend, err error) {
 | 
				
			|||||||
	backend := &Backend {
 | 
						backend := &Backend {
 | 
				
			||||||
		windows: map[xproto.Window] *Window { },
 | 
							windows: map[xproto.Window] *Window { },
 | 
				
			||||||
		doChannel: make(chan func (), 0),
 | 
							doChannel: make(chan func (), 0),
 | 
				
			||||||
 | 
							theme:  theme.Default  { },
 | 
				
			||||||
 | 
							config: config.Default { },
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// connect to X
 | 
						// connect to X
 | 
				
			||||||
@ -71,7 +73,11 @@ func (backend *Backend) Run () (err error) {
 | 
				
			|||||||
// Stop gracefully closes the connection and stops the event loop.
 | 
					// Stop gracefully closes the connection and stops the event loop.
 | 
				
			||||||
func (backend *Backend) Stop () {
 | 
					func (backend *Backend) Stop () {
 | 
				
			||||||
	backend.assert()
 | 
						backend.assert()
 | 
				
			||||||
 | 
						toClose := []*Window { }
 | 
				
			||||||
	for _, window := range backend.windows {
 | 
						for _, window := range backend.windows {
 | 
				
			||||||
 | 
							toClose = append(toClose, window)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, window := range toClose {
 | 
				
			||||||
		window.Close()
 | 
							window.Close()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	xevent.Quit(backend.connection)
 | 
						xevent.Quit(backend.connection)
 | 
				
			||||||
 | 
				
			|||||||
@ -2,8 +2,8 @@ package config
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import "io"
 | 
					import "io"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Parse parses a configuration file and returns it as a Config.
 | 
					// Parse parses one or more configuration files and returns them as a Config.
 | 
				
			||||||
func Parse (source io.Reader) (config Config) {
 | 
					func Parse (sources ...io.Reader) (config Config) {
 | 
				
			||||||
	// TODO
 | 
						// TODO
 | 
				
			||||||
	return Default { }
 | 
						return Default { }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,8 +2,8 @@ package theme
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import "io"
 | 
					import "io"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Parse parses a theme file and returns it as a Theme.
 | 
					// Parse parses one or more theme files and returns them as a Theme.
 | 
				
			||||||
func Parse (io.Reader) (Theme) {
 | 
					func Parse (sources ...io.Reader) (Theme) {
 | 
				
			||||||
	// TODO
 | 
						// TODO
 | 
				
			||||||
	return Default { }
 | 
						return Default { }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										61
									
								
								tomo.go
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								tomo.go
									
									
									
									
									
								
							@ -1,5 +1,9 @@
 | 
				
			|||||||
package tomo
 | 
					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/data"
 | 
				
			||||||
import "git.tebibyte.media/sashakoshka/tomo/theme"
 | 
					import "git.tebibyte.media/sashakoshka/tomo/theme"
 | 
				
			||||||
import "git.tebibyte.media/sashakoshka/tomo/config"
 | 
					import "git.tebibyte.media/sashakoshka/tomo/config"
 | 
				
			||||||
@ -12,6 +16,9 @@ var backend Backend
 | 
				
			|||||||
// the backend experiences a fatal error.
 | 
					// the backend experiences a fatal error.
 | 
				
			||||||
func Run (callback func ()) (err error) {
 | 
					func Run (callback func ()) (err error) {
 | 
				
			||||||
	backend, err = instantiateBackend()
 | 
						backend, err = instantiateBackend()
 | 
				
			||||||
 | 
						config := parseConfig()
 | 
				
			||||||
 | 
						backend.SetConfig(config)
 | 
				
			||||||
 | 
						backend.SetTheme(parseTheme(config.ThemePath()))
 | 
				
			||||||
	if callback != nil { callback() }
 | 
						if callback != nil { callback() }
 | 
				
			||||||
	err = backend.Run()
 | 
						err = backend.Run()
 | 
				
			||||||
	backend = nil
 | 
						backend = nil
 | 
				
			||||||
@ -63,6 +70,60 @@ func SetConfig (config config.Config) {
 | 
				
			|||||||
	backend.SetConfig(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 () {
 | 
					func assertBackend () {
 | 
				
			||||||
	if backend == nil { panic("no backend is running") }
 | 
						if backend == nil { panic("no backend is running") }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user