optimize the management of hyprland modules

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