hyprland/module: add short names and group/module format

parent dd3540e5
......@@ -17,6 +17,13 @@ import (
"github.com/urfave/cli/v3"
)
func checkModuleAvailable(info HyprModule) error {
if !info.Available {
return errors.New(locale.T("invalid module name"))
}
return nil
}
func HyprlandFixConfigCommand(ctx context.Context, cmd *cli.Command) error {
manager, err := GetHyprlandManager(ctx)
if err != nil {
......@@ -65,8 +72,8 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
}
info := manager.GetModuleInfo(cmd.Args().Get(0))
if !info.Available {
return errors.New(locale.T("invalid module name"))
if err := checkModuleAvailable(info); err != nil {
return err
}
fmt.Println(info.Status.Label)
return nil
......@@ -85,9 +92,8 @@ func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error {
}
moduleinfo := manager.GetModuleInfo(module)
if !moduleinfo.Available {
return errors.New(locale.T("invalid module name"))
if err := checkModuleAvailable(moduleinfo); err != nil {
return err
}
result, err := manager.CheckModule(module)
......@@ -215,7 +221,7 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
jsonItems := make([]ModuleInfoJSON, len(data))
for i, d := range data {
jsonItems[i] = ModuleInfoJSON{
Name: d.info.Name,
Name: d.info.ShortName(),
User: d.info.User,
Status: d.info.Status.Label,
Errors: d.errorNum,
......@@ -237,9 +243,9 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
if d.errorNum > 0 {
parts = append(parts, fmt.Sprintf(locale.T("(errors: %d)"), d.errorNum))
}
name := d.info.Name
name := d.info.ShortName()
if d.info.Meta != nil && d.info.Meta.Group != "" {
name = d.info.Meta.Group + "/" + d.info.Name
name = d.info.Meta.Group + "/" + name
}
item := ui.TreeItem{
Name: name,
......@@ -289,13 +295,13 @@ func HyprlandModuleListCommand(ctx context.Context, cmd *cli.Command) error {
if config.IsJSON(cmd) {
names := make([]string, len(modules))
for i, m := range modules {
names[i] = m.Name
names[i] = m.ShortName()
}
return ui.PrintJSON(names)
}
for _, m := range modules {
fmt.Println(m.Name)
fmt.Println(m.ShortName())
}
return nil
}
......@@ -309,8 +315,8 @@ func HyprlandModuleEditCommand(ctx context.Context, cmd *cli.Command) error {
info := manager.GetModuleInfo(module)
if !info.Available {
return errors.New(locale.T("invalid module name"))
if err := checkModuleAvailable(info); err != nil {
return err
}
if info.Path == "" {
......@@ -344,8 +350,8 @@ func HyprlandModuleShowCommand(ctx context.Context, cmd *cli.Command) error {
}
info := manager.GetModuleInfo(module)
if !info.Available {
return errors.New(locale.T("invalid module name"))
if err := checkModuleAvailable(info); err != nil {
return err
}
errors, _ := manager.CheckModule(module)
......@@ -362,7 +368,7 @@ func HyprlandModuleShowCommand(ctx context.Context, cmd *cli.Command) error {
}
blue := color.New(color.FgBlue).SprintFunc()
fmt.Printf("%s: %s\n", blue(locale.T("Module")), info.Name)
fmt.Printf("%s: %s\n", blue(locale.T("Module")), info.ShortName())
if info.Meta != nil {
if info.Meta.Group != "" {
fmt.Printf("%s: %s\n", blue(locale.T("Group")), info.Meta.Group)
......
......@@ -279,7 +279,7 @@ func ShellCompleteModule(filter string) func(ctx context.Context, cmd *cli.Comma
modules := manager.GetModulesList(filter)
for _, m := range modules {
fmt.Println(m.Name)
fmt.Println(m.ShortName())
}
}
}
......
......@@ -47,6 +47,13 @@ type HyprModule struct {
Meta *ModuleMeta `json:"meta,omitempty"`
}
func (m HyprModule) ShortName() string {
if m.Meta != nil && m.Meta.Group != "" && strings.HasPrefix(m.Name, m.Meta.Group+"-") {
return strings.TrimPrefix(m.Name, m.Meta.Group+"-")
}
return m.Name
}
type HyprVar struct {
Name string
Value string
......@@ -170,15 +177,20 @@ func (m *HyprlandManager) CheckModule(module string) ([]HyprConfigError, error)
// Modules
func (m *HyprlandManager) resolveModule(name string) (user bool, found bool) {
func (m *HyprlandManager) resolveModule(name string) (fullName string, user bool, found bool) {
// Формат group/module → group-module
if group, short, ok := strings.Cut(name, "/"); ok {
name = group + "-" + short
}
inUser := slices.Contains(m.UserModules, name)
inSystem := slices.Contains(m.SystemModules, name)
switch {
case inUser:
return true, true
return name, true, true
case inSystem:
return false, true
return name, false, true
}
// Файла нет ни в одном каталоге — ищем по source-строкам
......@@ -187,18 +199,18 @@ func (m *HyprlandManager) resolveModule(name string) (user bool, found bool) {
for _, src := range m.Sources {
if src.Path == userConfPath {
return true, true
return name, true, true
}
if src.Path == sysConfPath {
return false, true
return name, false, true
}
}
return false, false
return name, false, false
}
func (m *HyprlandManager) GetModuleInfo(module string) HyprModule {
user, found := m.resolveModule(module)
fullName, user, found := m.resolveModule(module)
if !found {
return HyprModule{
Name: module,
......@@ -206,7 +218,7 @@ func (m *HyprlandManager) GetModuleInfo(module string) HyprModule {
Available: !slices.Contains(config.HyprlandSkipModules, module),
}
}
return m.getModuleInfo(module, user)
return m.getModuleInfo(fullName, user)
}
func (m *HyprlandManager) getModuleInfo(module string, user bool) HyprModule {
......
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