add list selectable modules

parent f6fe4f0e
...@@ -30,6 +30,10 @@ func main() { ...@@ -30,6 +30,10 @@ func main() {
Value: "text", Value: "text",
Aliases: []string{"o"}, Aliases: []string{"o"},
}, },
&cli.BoolFlag{
Name: "all",
Usage: "show full Waybar module names",
},
}, },
}, },
{ {
...@@ -87,7 +91,11 @@ func listModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -87,7 +91,11 @@ func listModulesCommand(ctx context.Context, cmd *cli.Command) error {
return err return err
} }
names := registry.Names() names := registry.SelectableNames()
if cmd.Bool("all") {
names = registry.Names()
}
switch cmd.String("format") { switch cmd.String("format") {
case "json": case "json":
data, err := json.MarshalIndent(names, "", " ") data, err := json.MarshalIndent(names, "", " ")
......
...@@ -113,6 +113,19 @@ func (r Registry) Names() []string { ...@@ -113,6 +113,19 @@ func (r Registry) Names() []string {
return names return names
} }
func (r Registry) SelectableNames() []string {
names := make(map[string]struct{}, len(r.Modules))
wm := currentWM()
for name := range r.Modules {
selectableName := selectableModuleName(name, wm)
if selectableName != "" {
names[selectableName] = struct{}{}
}
}
return sortedKeys(names)
}
func (r Registry) Resolve(name, position string) (string, error) { func (r Registry) Resolve(name, position string) (string, error) {
explicit := strings.Contains(name, "/") || strings.Contains(name, "#") explicit := strings.Contains(name, "/") || strings.Contains(name, "#")
if explicit { if explicit {
...@@ -154,6 +167,44 @@ func (r Registry) Resolve(name, position string) (string, error) { ...@@ -154,6 +167,44 @@ func (r Registry) Resolve(name, position string) (string, error) {
return "", fmt.Errorf("module %q not found", name) return "", fmt.Errorf("module %q not found", name)
} }
func selectableModuleName(name, wm string) string {
namespace, base, hasNamespace := strings.Cut(name, "/")
if !hasNamespace {
return trimVerticalVariant(name)
}
if isKnownWM(namespace) {
if wm != "" && namespace != wm {
return ""
}
return trimVerticalVariant(base)
}
return trimVerticalVariant(name)
}
func trimVerticalVariant(name string) string {
return strings.TrimSuffix(name, "#vertical")
}
func isKnownWM(name string) bool {
switch name {
case "hyprland", "niri":
return true
default:
return false
}
}
func sortedKeys(values map[string]struct{}) []string {
keys := make([]string, 0, len(values))
for key := range values {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func (r Registry) suffixMatches(name string) []string { func (r Registry) suffixMatches(name string) []string {
suffix := "/" + name suffix := "/" + name
var matches []string var matches []string
......
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