add safe mode

parent 2df20d57
......@@ -29,6 +29,12 @@ Hidden debug command:
ximper-shell-panel generate
```
Safe mode ignores user config, user modules, and user styles:
```sh
XIMPER_SHELL_SAFEMODE=1 ximper-shell-panel restart
```
Supported positions:
```text
......
......@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)
const (
......@@ -15,6 +16,7 @@ const (
ModulesFile = SystemWaybarDir + "/modules.json"
StyleFile = SystemWaybarDir + "/style.css"
DarkStyleFile = SystemWaybarDir + "/style-dark.css"
SafeModeEnv = "XIMPER_SHELL_SAFEMODE"
)
type Config struct {
......@@ -91,6 +93,16 @@ func UserDarkStylePath() string {
return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "style-dark.css")
}
func SafeMode() bool {
value := strings.ToLower(strings.TrimSpace(os.Getenv(SafeModeEnv)))
switch value {
case "", "0", "false", "no", "off":
return false
default:
return true
}
}
func RuntimeDir() string {
return filepath.Join(os.TempDir(), "ximper-shell", "panel")
}
......
......@@ -109,26 +109,31 @@ func WriteRuntimeStyles() error {
if err := os.MkdirAll(RuntimeWaybarDir(), 0755); err != nil {
return err
}
if err := writeRuntimeStyle(RuntimeStylePath("style.css"), StyleFile, UserStylePath()); err != nil {
return err
}
userStyle := UserStylePath()
userDarkStyle := UserStylePath()
if fileExists(UserDarkStylePath()) {
if SafeMode() {
userStyle = ""
userDarkStyle = ""
} else if fileExists(UserDarkStylePath()) {
userDarkStyle = UserDarkStylePath()
}
if err := writeRuntimeStyle(RuntimeStylePath("style.css"), StyleFile, userStyle); err != nil {
return err
}
return writeRuntimeStyle(RuntimeStylePath("style-dark.css"), DarkStyleFile, userDarkStyle)
}
func writeRuntimeStyle(runtimePath, systemPath, userPath string) error {
data := "@import url(\"" + systemPath + "\");\n"
if fileExists(userPath) {
if userPath != "" && fileExists(userPath) {
data += "@import url(\"" + userPath + "\");\n"
}
return os.WriteFile(runtimePath, []byte(data), 0644)
}
func loadInputs() (Config, Registry, error) {
cfg, err := LoadConfig(UserConfigPath())
cfg, err := loadConfig()
if err != nil {
return Config{}, Registry{}, err
}
......@@ -140,7 +145,17 @@ func loadInputs() (Config, Registry, error) {
return cfg, registry, nil
}
func loadConfig() (Config, error) {
if SafeMode() {
return DefaultConfig(), nil
}
return LoadConfig(UserConfigPath())
}
func loadRegistry() (Registry, error) {
if SafeMode() {
return LoadRegistry(ModulesFile)
}
return LoadMergedRegistry(ModulesFile, UserModulesPath())
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment