optimize the management of hyprland modules

parent e1373001
...@@ -62,38 +62,69 @@ func fileExists(path string) bool { ...@@ -62,38 +62,69 @@ func fileExists(path string) bool {
} }
func hyprlandModuleStatus(module string, user bool) string { func hyprlandModuleStatus(module string, user bool) string {
sysFile := hyprlandGetModuleFile(module, user) sysFile, lineFull, lineTilde := hyprlandModuleLines(module, user)
lineFull := "source = " + sysFile
lineTilde := "source = ~" + strings.TrimPrefix(sysFile, HomeDir)
if !fileExists(sysFile) {
fmt.Printf("Модуль '%s' не найден: %s", module, sysFile)
os.Exit(1)
return ""
}
// Конфиг Hyprland отсутствует
if !fileExists(config.Env.Hyprland.Config) { if !fileExists(config.Env.Hyprland.Config) {
return "disabled" if fileExists(sysFile) {
return "unused"
}
return "missing"
} }
f, err := os.Open(config.Env.Hyprland.Config) f, err := os.Open(config.Env.Hyprland.Config)
if err != nil { if err != nil {
return "disabled" if fileExists(sysFile) {
return "unused"
}
return "missing"
} }
defer f.Close() defer f.Close()
reFull := regexp.MustCompile(`^\s*` + regexp.QuoteMeta(lineFull) + `(?:\s|$)`) reFull := regexp.MustCompile(`^\s*` + regexp.QuoteMeta(lineFull) + `(?:\s|$)`)
reTilde := regexp.MustCompile(`^\s*` + regexp.QuoteMeta(lineTilde) + `(?:\s|$)`) reTilde := regexp.MustCompile(`^\s*` + regexp.QuoteMeta(lineTilde) + `(?:\s|$)`)
foundActive := false
foundCommented := false
sc := bufio.NewScanner(f) sc := bufio.NewScanner(f)
for sc.Scan() { for sc.Scan() {
line := sc.Text() line := sc.Text()
trim := strings.TrimSpace(line)
if trim == "#"+lineFull || trim == "# "+lineFull || trim == "#"+lineTilde || trim == "# "+lineTilde {
foundCommented = true
continue
}
if reFull.MatchString(line) || reTilde.MatchString(line) { if reFull.MatchString(line) || reTilde.MatchString(line) {
return "enabled" foundActive = true
break
} }
} }
return "disabled"
// подключён, но файла нет
if foundActive && !fileExists(sysFile) {
return "missing"
}
// подключён и файл есть
if foundActive && fileExists(sysFile) {
return "enabled"
}
// закомментирован
if foundCommented {
return "disabled"
}
// файл есть, но строка отсутствует
if fileExists(sysFile) && !foundActive && !foundCommented {
return "unused"
}
// Файла нет и упоминаний нет
return "missing"
} }
func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
...@@ -103,9 +134,10 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -103,9 +134,10 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
} }
func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (string, error) { func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (string, error) {
sysFile := hyprlandGetModuleFile(module, user) sysFile, lineFull, lineTilde := hyprlandModuleLines(module, user)
lineFull := "source = " + sysFile
lineTilde := "source = ~" + strings.TrimPrefix(sysFile, HomeDir) reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
var out string var out string
...@@ -133,8 +165,6 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -133,8 +165,6 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s
} }
// Был закомментирован // Был закомментирован
reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
if reFull.MatchString(l) || reTilde.MatchString(l) { if reFull.MatchString(l) || reTilde.MatchString(l) {
found = true found = true
if onlyNew { if onlyNew {
...@@ -163,8 +193,6 @@ func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (s ...@@ -163,8 +193,6 @@ 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 {
reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
if reFull.MatchString(l) || reTilde.MatchString(l) { if reFull.MatchString(l) || reTilde.MatchString(l) {
return "", fmt.Errorf("модуль '%s' уже отключён", module) return "", fmt.Errorf("модуль '%s' уже отключён", module)
} }
...@@ -257,13 +285,15 @@ func hyprlandListModules(user bool, filter string) []string { ...@@ -257,13 +285,15 @@ func hyprlandListModules(user bool, filter string) []string {
continue continue
} }
status := hyprlandModuleStatus(module, user) status := hyprlandModuleStatus(module, user)
if filter == "enabled" && status != "enabled" {
if filter == "" || filter == "all" {
modules = append(modules, module)
continue continue
} }
if filter == "disabled" && status != "disabled" {
continue if status == filter {
modules = append(modules, module)
} }
modules = append(modules, module)
} }
return modules return modules
} }
...@@ -277,22 +307,22 @@ func hyprlandInfoModules(user bool, filter string) { ...@@ -277,22 +307,22 @@ func hyprlandInfoModules(user bool, filter string) {
color.Blue("Modules:") color.Blue("Modules:")
statusMap := map[string]struct {
symbol string
color func(format string, a ...interface{}) string
}{
"enabled": {symbol: "●", color: color.GreenString},
"disabled": {symbol: "○", color: color.RedString},
"missing": {symbol: "!", color: color.YellowString},
"unused": {symbol: "-", color: color.HiBlackString},
}
for i, module := range modules { for i, module := range modules {
status := hyprlandModuleStatus(module, user) status := hyprlandModuleStatus(module, user)
info := statusMap[status]
coloredStatus := "" coloredStatus := info.color(info.symbol)
coloredModule := "" coloredModule := info.color(module)
switch status {
case "enabled":
coloredStatus = color.GreenString("●")
coloredModule = color.GreenString(module)
case "disabled":
coloredStatus = color.RedString("○")
coloredModule = color.RedString(module)
default:
coloredStatus = color.HiBlackString("○")
coloredModule = color.HiBlackString(module)
}
prefix := "├──" prefix := "├──"
if i == len(modules)-1 { if i == len(modules)-1 {
...@@ -306,3 +336,10 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -306,3 +336,10 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandInfoModules(cmd.Bool("user"), cmd.String("filter")) hyprlandInfoModules(cmd.Bool("user"), cmd.String("filter"))
return nil return nil
} }
func hyprlandModuleLines(module string, user bool) (sysFile, lineFull, lineTilde string) {
sysFile = hyprlandGetModuleFile(module, user)
lineFull = "source = " + sysFile
lineTilde = "source = ~" + strings.TrimPrefix(sysFile, HomeDir)
return
}
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