hyprland: simplify duplicate code

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