update distro profiles

- add hyprmodules
parent 7b95545e
package create
import (
"bytes"
"ximperconf/config"
"ximperconf/hyprland"
......@@ -128,6 +129,47 @@ func processProfile(prof Profile, dryRun bool, res *Result) {
}
// ----- hyprmodules -----
for _, m := range prof.HyprModules {
if m.Module == "" {
res.HyprModules = append(res.HyprModules, "Пропущен модуль без имени")
continue
}
// Если есть if-exec
if m.IfExec != "" {
cmd := exec.Command("sh", "-c", m.IfExec)
output, err := cmd.Output()
if err != nil {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Модуль '%s' пропущен: команда '%s' завершилась с ошибкой: %v", m.Module, m.IfExec, err))
continue
}
// Если команда ничего не вывела
if len(bytes.TrimSpace(output)) == 0 {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Модуль '%s' пропущен: команда '%s' не вернула данных", m.Module, m.IfExec))
continue
}
}
// Dry-run
if dryRun {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("[Dry-run] Подключён модуль: %s", m.Module))
continue
}
if _, err := hyprland.HyprlandSetModule("enable", m.Module, m.User, m.NewOnly); err != nil {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Ошибка подключения модуля %s: %v", m.Module, err))
} else {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Подключён модуль: %s", m.Module))
}
}
// ----- синхронизация раскладок Hyprland-----
if prof.SyncSystemLayouts && !dryRun {
sysLayouts, err := hyprland.HyprlandGetKeyboardLayouts()
......@@ -385,6 +427,7 @@ func CreateConfCommand(ctx context.Context, cmd *cli.Command) error {
printTree("Создаём ссылки", res.Links)
printTree("Обновляем", res.Replaced)
printTree("Создаём переменные Hyprland", res.HyprVars)
printTree("Подключаем модули Hyprland", res.HyprModules)
printTree("Синхронизируем раскладку Hyprland", res.SyncSystemLayouts)
return nil
......
......@@ -22,6 +22,13 @@ type HyprVar struct {
Value string `json:"value"`
}
type HyprModule struct {
Module string `json:"module"`
User bool `json:"user,omitempty"`
NewOnly bool `json:"new-only,omitempty"`
IfExec string `json:"if-exec,omitempty"`
}
type Profile struct {
Binary string `json:"binary,omitempty"`
Copy []CopyEntry `json:"copy,omitempty"`
......@@ -29,8 +36,9 @@ type Profile struct {
ReleaseReplace []ReleaseReplaceEntry `json:"release-replace,omitempty"`
// ----- hyprland -----
SyncSystemLayouts bool `json:"sync-system-layouts,omitempty"`
HyprVars []HyprVar `json:"hyprvars,omitempty"`
SyncSystemLayouts bool `json:"sync-system-layouts,omitempty"`
HyprVars []HyprVar `json:"hyprvars,omitempty"`
HyprModules []HyprModule `json:"hyprmodules,omitempty"`
}
type Config struct {
......@@ -42,5 +50,6 @@ type Result struct {
Copies []string
Replaced []string
HyprVars []string
HyprModules []string
SyncSystemLayouts []string
}
......@@ -102,50 +102,71 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
return nil
}
func hyprlandSetModule(action string, module string, user bool) {
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)
var out string
if !fileExists(sysFile) {
fmt.Printf("Модуль '%s' не найден: %s", module, sysFile)
return
return "", fmt.Errorf("модуль '%s' не найден: %s", module, sysFile)
}
data, _ := os.ReadFile(config.Env.Hyprland.Config)
lines := strings.Split(string(data), "\n")
changed := false
found := false
switch action {
case "enable":
for i, l := range lines {
if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde {
fmt.Printf("Модуль '%s' уже включён", module)
return
trim := strings.TrimSpace(l)
// Уже включён
if trim == lineFull || trim == lineTilde {
found = true
if onlyNew {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге — пропущено", module)
}
return "", fmt.Errorf("модуль '%s' уже включён", module)
}
re := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
if re.MatchString(l) {
// Был закомментирован
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 {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге (закомментирован) — пропущено", module)
}
lines[i] = lineFull
changed = true
}
re = regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
if re.MatchString(l) {
lines[i] = lineFull
changed = true
}
// Если модуль нигде не найден — добавляем новую строку
if !found {
if strings.HasPrefix(sysFile, HomeDir) {
lines = append(lines, lineTilde)
} else {
lines = append(lines, lineFull)
}
changed = true
}
if !changed {
lines = append(lines, lineTilde)
return "", fmt.Errorf("модуль '%s' не изменён", module)
}
color.Green("Модуль '%s' включён", module)
out = fmt.Sprintf("Модуль '%s' включён", module)
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) {
color.Yellow("Модуль '%s' уже отключён", module)
return
return "", fmt.Errorf("модуль '%s' уже отключён", module)
}
if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde {
......@@ -155,22 +176,33 @@ func hyprlandSetModule(action string, module string, user bool) {
}
if !changed {
color.Red("Модуль '%s' не найден в конфигурации", module)
return
return "", fmt.Errorf("модуль '%s' не найден в конфигурации", module)
}
color.Yellow("Модуль '%s' отключён", module)
out = fmt.Sprintf("Модуль '%s' отключён", module)
}
_ = os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(lines, "\n")), 0644)
return out, nil
}
func HyprlandModuleEnableCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandSetModule("enable", cmd.Args().Get(0), cmd.Bool("user"))
msg, err := HyprlandSetModule("enable", cmd.Args().Get(0), cmd.Bool("user"), false)
if err != nil {
color.Red(err.Error())
return err
}
color.Green(msg)
return nil
}
func HyprlandModuleDisableCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user"))
msg, err := HyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user"), false)
if err != nil {
color.Red(err.Error())
return err
}
color.Green(msg)
return nil
}
func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error {
......@@ -178,12 +210,23 @@ func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error {
user := cmd.Bool("user")
status := hyprlandModuleStatus(module, user)
var msg string
var err error
switch status {
case "enabled":
hyprlandSetModule("disable", module, user)
msg, err = HyprlandSetModule("disable", module, user, false)
case "disabled":
hyprlandSetModule("enable", module, user)
msg, err = HyprlandSetModule("enable", module, user, false)
}
if err != nil {
color.Red(err.Error())
return err
}
color.Green(msg)
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