hyprland/var: rewrite unset to use manager

parent 762c7f44
......@@ -2,15 +2,11 @@ package hyprland
import (
"context"
"fmt"
"ximperconf/config"
"ximperconf/locale"
"ximperconf/ui"
"fmt"
"os"
"regexp"
"strings"
"github.com/fatih/color"
"github.com/urfave/cli/v3"
)
......@@ -111,46 +107,17 @@ func HyprlandVarSetCommand(ctx context.Context, cmd *cli.Command) error {
return nil
}
func hyprlandVarUnset(name string) error {
if name == "" {
color.Red(locale.T("specify variable name"))
os.Exit(1)
}
if !regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`).MatchString(name) {
color.Red(locale.T("Invalid variable name: %s"), name)
os.Exit(1)
}
if !config.FileExists(config.Env.Hyprland.Config) {
color.Red(locale.T("Configuration not found: %s"), config.Env.Hyprland.Config)
os.Exit(1)
}
data, _ := os.ReadFile(config.Env.Hyprland.Config)
lines := strings.Split(string(data), "\n")
re := regexp.MustCompile(`^\s*\$` + regexp.QuoteMeta(name) + `\s*=`)
newLines := make([]string, 0, len(lines))
removed := false
for _, l := range lines {
if re.MatchString(l) {
removed = true
continue
}
newLines = append(newLines, l)
func HyprlandVarUnsetCommand(ctx context.Context, cmd *cli.Command) error {
manager, err := GetHyprlandManager(ctx)
if err != nil {
return err
}
if !removed {
color.Red(locale.T("variable '%s' not found"), name)
os.Exit(1)
name := cmd.Args().Get(0)
if err := manager.UnsetVar(name); err != nil {
return err
}
color.Green(locale.T("Variable '%s' removed"), name)
return os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(newLines, "\n")), 0644)
}
func HyprlandVarUnsetCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandVarUnset(cmd.Args().Get(0))
return nil
}
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