hyprland/var: rewrite unset to use manager

parent 762c7f44
...@@ -2,15 +2,11 @@ package hyprland ...@@ -2,15 +2,11 @@ package hyprland
import ( import (
"context" "context"
"fmt"
"ximperconf/config" "ximperconf/config"
"ximperconf/locale" "ximperconf/locale"
"ximperconf/ui" "ximperconf/ui"
"fmt"
"os"
"regexp"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
...@@ -111,46 +107,17 @@ func HyprlandVarSetCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -111,46 +107,17 @@ func HyprlandVarSetCommand(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func hyprlandVarUnset(name string) error { func HyprlandVarUnsetCommand(ctx context.Context, cmd *cli.Command) error {
if name == "" { manager, err := GetHyprlandManager(ctx)
color.Red(locale.T("specify variable name")) if err != nil {
os.Exit(1) return err
}
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)
} }
if !removed { name := cmd.Args().Get(0)
color.Red(locale.T("variable '%s' not found"), name) if err := manager.UnsetVar(name); err != nil {
os.Exit(1) return err
} }
color.Green(locale.T("Variable '%s' removed"), name) 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 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