hyprland: simplify duplicate code

parent ee3864db
...@@ -74,8 +74,10 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -74,8 +74,10 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
} }
func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error {
module := cmd.Args().Get(0)
user := cmd.Bool("user")
if cmd.Args().Get(0) == "" { if module == "" {
return errors.New(locale.T("specify module name")) return errors.New(locale.T("specify module name"))
} }
...@@ -84,13 +86,13 @@ func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -84,13 +86,13 @@ func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error {
return err return err
} }
moduleinfo := manager.GetModuleInfo(cmd.Args().Get(0), cmd.Bool("user")) moduleinfo := manager.GetModuleInfo(module, user)
if !moduleinfo.Available { if !moduleinfo.Available {
return errors.New(locale.T("invalid module name")) return errors.New(locale.T("invalid module name"))
} }
result, err := manager.CheckModule(cmd.Args().Get(0), cmd.Bool("user")) result, err := manager.CheckModule(module, user)
if err != nil { if err != nil {
return err return err
......
...@@ -45,14 +45,14 @@ func HyprlandSyncSystemLayouts(ctx context.Context, cmd *cli.Command) error { ...@@ -45,14 +45,14 @@ func HyprlandSyncSystemLayouts(ctx context.Context, cmd *cli.Command) error {
hyprLayouts := manager.GetVar("kb_layout") hyprLayouts := manager.GetVar("kb_layout")
if force || hyprLayouts == "" { if hyprLayouts != "" && !force {
if _, err := manager.SetVar("kb_layout", sysLayouts); err != nil {
return fmt.Errorf(locale.T("failed to update kb_layout in Hyprland: %w"), err)
}
color.Green(locale.T("Layout updated!"))
} else {
return errors.New(locale.T("layout is already set, use '--force' for forced update")) return errors.New(locale.T("layout is already set, use '--force' for forced update"))
} }
if _, err := manager.SetVar("kb_layout", sysLayouts); err != nil {
return fmt.Errorf(locale.T("failed to update kb_layout in Hyprland: %w"), err)
}
color.Green(locale.T("Layout updated!"))
return nil return nil
} }
...@@ -91,9 +91,9 @@ func NewHyprlandManager() (*HyprlandManager, error) { ...@@ -91,9 +91,9 @@ func NewHyprlandManager() (*HyprlandManager, error) {
manager.Vars = manager.GetVarList() manager.Vars = manager.GetVarList()
manager.Sources = manager.parseSourceLines() manager.Sources = manager.parseSourceLines()
manager.UserModules = manager.scanModulesDir(true) manager.UserModules = scanDir(manager.GetModuleDir(true), ".conf")
manager.SystemModules = manager.scanModulesDir(false) manager.SystemModules = scanDir(manager.GetModuleDir(false), ".conf")
manager.Plugins = manager.scanPluginsDir() manager.Plugins = scanDir(manager.PluginsDir, ".so")
manager.LoadedPlugins = manager.GetLoadedPlugins() manager.LoadedPlugins = manager.GetLoadedPlugins()
for _, p := range manager.LoadedPlugins { for _, p := range manager.LoadedPlugins {
...@@ -176,72 +176,65 @@ func (m *HyprlandManager) CheckModule( ...@@ -176,72 +176,65 @@ func (m *HyprlandManager) CheckModule(
func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
sysFile := m.GetModuleFile(module, user) sysFile := m.GetModuleFile(module, user)
var ConfPath string var confPath string
if user { if user {
ConfPath = "~" + strings.TrimPrefix(sysFile, m.Home) confPath = "~" + strings.TrimPrefix(sysFile, m.Home)
} else { } else {
ConfPath = sysFile confPath = sysFile
} }
Available := true available := !slices.Contains(config.HyprlandSkipModules, module)
fileExists := config.FileExists(sysFile)
for _, skipModule := range config.HyprlandSkipModules {
if module == skipModule {
Available = false
}
}
FileExists := config.FileExists(sysFile)
// Конфиг Hyprland отсутствует // Конфиг Hyprland отсутствует
if !config.FileExists(config.Env.Hyprland.Config) { if !config.FileExists(config.Env.Hyprland.Config) {
if FileExists { if fileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: config.ModuleStatus.Unused, Status: config.ModuleStatus.Unused,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: confPath,
Available: Available, Available: available,
Meta: ParseModuleMeta(sysFile), Meta: ParseModuleMeta(sysFile),
} }
} }
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: config.ModuleStatus.Missing, Status: config.ModuleStatus.Missing,
Available: Available, Available: available,
} }
} }
foundActive := false foundActive := false
foundCommented := false foundCommented := false
var LineNumber int var lineNumber int
for _, src := range m.Sources { for _, src := range m.Sources {
if src.Path != ConfPath { if src.Path != confPath {
continue continue
} }
if src.Commented { if src.Commented {
foundCommented = true foundCommented = true
LineNumber = src.LineNumber lineNumber = src.LineNumber
continue continue
} }
foundActive = true foundActive = true
LineNumber = src.LineNumber lineNumber = src.LineNumber
break break
} }
// подключен // подключен
if foundActive { if foundActive {
// файла нет // файла нет
if !FileExists { if !fileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: config.ModuleStatus.Missing, Status: config.ModuleStatus.Missing,
LineNumber: LineNumber, LineNumber: lineNumber,
Available: Available, Available: available,
} }
} }
// файл есть // файл есть
...@@ -249,9 +242,9 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -249,9 +242,9 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
Name: module, Name: module,
Status: config.ModuleStatus.Enabled, Status: config.ModuleStatus.Enabled,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: confPath,
LineNumber: LineNumber, LineNumber: lineNumber,
Available: Available, Available: available,
Meta: ParseModuleMeta(sysFile), Meta: ParseModuleMeta(sysFile),
} }
} }
...@@ -262,11 +255,11 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -262,11 +255,11 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
Name: module, Name: module,
Status: config.ModuleStatus.Disabled, Status: config.ModuleStatus.Disabled,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: confPath,
LineNumber: LineNumber, LineNumber: lineNumber,
Available: Available, Available: available,
} }
if FileExists { if fileExists {
mod.Meta = ParseModuleMeta(sysFile) mod.Meta = ParseModuleMeta(sysFile)
} else { } else {
mod.Path = "" mod.Path = ""
...@@ -275,13 +268,13 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -275,13 +268,13 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
} }
// файл есть, но строка отсутствует // файл есть, но строка отсутствует
if FileExists { if fileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: config.ModuleStatus.Unused, Status: config.ModuleStatus.Unused,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: confPath,
Available: Available, Available: available,
Meta: ParseModuleMeta(sysFile), Meta: ParseModuleMeta(sysFile),
} }
} }
...@@ -290,7 +283,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -290,7 +283,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: config.ModuleStatus.Unknown, Status: config.ModuleStatus.Unknown,
Available: Available, Available: available,
} }
} }
...@@ -1126,40 +1119,19 @@ func (m *HyprlandManager) FindModuleByPath(filePath string) (name string, isUser ...@@ -1126,40 +1119,19 @@ func (m *HyprlandManager) FindModuleByPath(filePath string) (name string, isUser
return "", false, false return "", false, false
} }
func (m *HyprlandManager) scanModulesDir(user bool) []string { func scanDir(dir, suffix string) []string {
dir := m.GetModuleDir(user)
entries, err := os.ReadDir(dir) entries, err := os.ReadDir(dir)
if err != nil { if err != nil {
return nil return nil
} }
var names []string
var modules []string
for _, e := range entries { for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".conf") { if e.IsDir() || !strings.HasSuffix(e.Name(), suffix) {
continue continue
} }
modules = append(modules, strings.TrimSuffix(e.Name(), ".conf")) names = append(names, strings.TrimSuffix(e.Name(), suffix))
} }
return modules return names
}
func (m *HyprlandManager) scanPluginsDir() []string {
var list []string
entries, err := os.ReadDir(m.PluginsDir)
if err != nil {
return list
}
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".so") {
continue
}
name := strings.TrimSuffix(e.Name(), ".so")
list = append(list, name)
}
return list
} }
func normalizeHyprlandLine(line, home string) string { func normalizeHyprlandLine(line, home string) string {
......
...@@ -59,12 +59,8 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -59,12 +59,8 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error {
for _, plugin := range plugins { for _, plugin := range plugins {
status := manager.GetPluginStatus(plugin) status := manager.GetPluginStatus(plugin)
desc := "" desc := ""
if status.IsEqual(config.PluginStatus.Loaded) { if plug, ok := manager.LoadedPluginsMap[plugin]; ok {
for _, plug := range manager.LoadedPlugins { desc = plug.Description
if plug.Name == plugin {
desc = plug.Description
}
}
} }
items = append(items, ui.TreeItem{ items = append(items, ui.TreeItem{
Name: plugin, Name: plugin,
......
...@@ -38,7 +38,7 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -38,7 +38,7 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error {
return err return err
} }
info := manager.GetVarList() info := manager.Vars
if len(info) == 0 { if len(info) == 0 {
if config.IsJSON(cmd) { if config.IsJSON(cmd) {
......
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