hyprland/module: add --remove flag to disable command

parent 86c33e0f
...@@ -280,10 +280,6 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -280,10 +280,6 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s
// Уже включён // Уже включён
if trim == lineFull || trim == lineTilde { if trim == lineFull || trim == lineTilde {
found = true
if onlyNew {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге — пропущено", module)
}
return "", fmt.Errorf("модуль '%s' уже включён", module) return "", fmt.Errorf("модуль '%s' уже включён", module)
} }
...@@ -300,11 +296,7 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -300,11 +296,7 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s
// Если модуль нигде не найден — добавляем новую строку // Если модуль нигде не найден — добавляем новую строку
if !found { if !found {
if strings.HasPrefix(sysFile, HomeDir) {
lines = append(lines, lineTilde)
} else {
lines = append(lines, lineFull) lines = append(lines, lineFull)
}
changed = true changed = true
} }
...@@ -316,12 +308,17 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -316,12 +308,17 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s
case "disable": case "disable":
for i, l := range lines { for i, l := range lines {
trim := strings.TrimSpace(l)
// Уже выключен
if reFull.MatchString(l) || reTilde.MatchString(l) { if reFull.MatchString(l) || reTilde.MatchString(l) {
return "", fmt.Errorf("модуль '%s' уже отключён", module) return "", fmt.Errorf("модуль '%s' уже отключён", module)
} }
if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde { // Включён
if trim == lineFull || trim == lineTilde {
lines[i] = "# " + lineFull lines[i] = "# " + lineFull
out = fmt.Sprintf("Модуль '%s' отключён", module)
changed = true changed = true
} }
} }
...@@ -329,10 +326,43 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -329,10 +326,43 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s
if !changed { if !changed {
return "", fmt.Errorf("модуль '%s' не найден в конфигурации", module) return "", fmt.Errorf("модуль '%s' не найден в конфигурации", module)
} }
out = fmt.Sprintf("Модуль '%s' отключён", module)
case "remove":
// слайс для строк, которые не нужно удалять
newLines := []string{}
for _, l := range lines {
trim := strings.TrimSpace(l)
// Проверяем все возможные варианты
isModuleLine := trim == lineFull ||
trim == lineTilde ||
reFull.MatchString(l) ||
reTilde.MatchString(l)
// Если это не модуль - добавляем в новый список
if !isModuleLine {
newLines = append(newLines, l)
} else {
changed = true
}
}
// Заменяем lines на отфильтрованные строки
if changed {
lines = newLines
} }
if !changed {
return "", fmt.Errorf("модуль '%s' не найден в конфигурации", module)
}
out = fmt.Sprintf("Модуль '%s' удалён", module)
}
if changed {
_ = os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(lines, "\n")), 0644) _ = os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(lines, "\n")), 0644)
}
return out, nil return out, nil
} }
...@@ -347,7 +377,17 @@ func HyprlandModuleEnableCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -347,7 +377,17 @@ func HyprlandModuleEnableCommand(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func HyprlandModuleDisableCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleDisableCommand(ctx context.Context, cmd *cli.Command) error {
msg, err := HyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user"), false) remove := cmd.Bool("remove")
var msg string
var err error
if remove {
msg, err = HyprlandSetModule("remove", cmd.Args().Get(0), cmd.Bool("user"), false)
} else {
msg, err = HyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user"), false)
}
if err != nil { if err != nil {
color.Red(err.Error()) color.Red(err.Error())
return err return err
......
...@@ -88,6 +88,14 @@ func CommandList() *cli.Command { ...@@ -88,6 +88,14 @@ func CommandList() *cli.Command {
{ {
Name: "disable", Name: "disable",
Usage: "disable module", Usage: "disable module",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "remove",
Usage: "delete module line from config",
Aliases: []string{"r"},
Value: false,
},
},
ArgsUsage: "module", ArgsUsage: "module",
Action: HyprlandModuleDisableCommand, Action: HyprlandModuleDisableCommand,
ShellComplete: ShellCompleteModule("enabled"), ShellComplete: ShellCompleteModule("enabled"),
......
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