waveterm/wavesrv/pkg/configstore/termthemes.go

84 lines
2.1 KiB
Go
Raw Normal View History

Simplified terminal theming (#570) * save work * reusable StyleBlock component * StyleBlock in elements dir * root level * ability to inherit root styles * change prop from classname to selector * selector should always be :root * remove selector prop from StyleBlock * working * cleanup * loadThemeStyles doesn't have to be async * revert changes in tabs2.less * remove old implementation * cleanup * remove file from another branch * fix issue where line in history view doesn't reflect the terminal theme * add key and value validation * add label to tab settings terminal theme dropdown * save work * save work * save work * working * trigger componentDidUpdate when switching tabs and sessions * cleanup * save work * save work * use UpdatePacket for theme changes as well * make methods cohesive * use themes coming from backend * reload terminal when styel block is unmounted and mounted * fix validation * re-render terminal when theme is updated * remove test styles * cleanup * more cleanup * revert unneeded change * more cleanup * fix type * more cleanup * render style blocks in the header instead of body using portal * add ability to reuse and dispose TermThemes instance and file watcher * remove comment * minor change * separate filewatcher as singleton * do not render app when term theme style blocks aren't rendered first * only render main when termstyles have been rendered already * add comment * use DoUpdate to send themes to front-end * support to watch subdirectories * added support for watch subdirectories * make watcher more flexible so it can be closed anywhere * cleanup * undo the app/main split * use TermThemesType in creating initial value for Themes field * simplify code * fix issue where dropdown label doesn't float when the theme selected is Inherit * remove unsed var * start watcher in main, merge themes (don't overwrite) on event. * ensure terminal-themes directory is created on startup * ah, wait for termThemes to be set (the connect packet needs to have been processed to proceed with rendering)
2024-04-24 08:22:35 +02:00
package configstore
import (
"encoding/json"
"errors"
"log"
"os"
"path"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbase"
)
const ConfigReturnTypeStr = "termthemes"
const configDir = "config/terminal-themes/"
var configDirAbsPath = path.Join(scbase.GetWaveHomeDir(), configDir)
type ConfigReturn map[string]map[string]string
func (tt ConfigReturn) GetType() string {
return ConfigReturnTypeStr
}
func getNameAndPath(event fsnotify.Event) (string, string) {
filePath := event.Name
fileName := filepath.Base(filePath)
// Normalize the file path for consistency across platforms
normalizedPath := filepath.ToSlash(filePath)
return fileName, normalizedPath
}
// readFileContents reads and unmarshals the JSON content from a file.
func readFileContents(filePath string) (map[string]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var content map[string]string
if err := json.Unmarshal(data, &content); err != nil {
return nil, err
}
return content, nil
}
// ScanConfigs reads all JSON files in the specified directory and its subdirectories.
func ScanConfigs() (ConfigReturn, error) {
config := make(ConfigReturn)
if _, err := os.Stat(configDirAbsPath); errors.Is(err, os.ErrNotExist) {
log.Printf("directory does not exist: %s", configDirAbsPath)
return ConfigReturn{}, nil
}
err := filepath.Walk(configDirAbsPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".json" {
content, err := readFileContents(path)
if err != nil {
log.Printf("error reading file %s: %v", path, err)
return nil // continue walking despite error in reading file
}
// Use the relative path from the directory as the key to store themes
relPath, err := filepath.Rel(configDirAbsPath, path)
if err != nil {
log.Printf("error getting relative file path %s: %v", path, err)
return nil // continue walking despite error in getting relative path
}
config[relPath] = content
}
return nil
})
if err != nil {
return nil, err
}
return config, nil
}