From 9039742a2f66563f246d3a46eaf3df5200f68778 Mon Sep 17 00:00:00 2001 From: Francois Lachese Date: Sun, 24 May 2026 00:33:57 +0200 Subject: [PATCH] feat: respect OnlyShowIn and NotShowIn xdg specs https://specifications.freedesktop.org/autostart/latest/#id-1.3.4.4 --- cmd/lsdesktop/main.go | 22 ++++++++++++++++++++++ internal/desktop/desktop.go | 21 +++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cmd/lsdesktop/main.go b/cmd/lsdesktop/main.go index 03f743f..d31ff03 100644 --- a/cmd/lsdesktop/main.go +++ b/cmd/lsdesktop/main.go @@ -5,12 +5,21 @@ import ( "io/fs" "os" "path/filepath" + "slices" "strings" "github.com/FLchs/lsdesktop/internal/desktop" "github.com/FLchs/lsdesktop/internal/history" ) +func currentDesktops() []string { + val := os.Getenv("XDG_CURRENT_DESKTOP") + if val == "" { + return nil + } + return strings.Split(val, ":") +} + func appDirs() []string { dirs := make([]string, 0, 4) @@ -38,6 +47,8 @@ func appDirs() []string { } func main() { + desktops := currentDesktops() + hist, err := history.ReadHistory() if err != nil { fmt.Fprintf(os.Stderr, "Error: read history: %v\n", err) @@ -76,6 +87,17 @@ func main() { return nil } + if len(entry.OnlyShowIn) > 0 && !slices.ContainsFunc(entry.OnlyShowIn, func(s string) bool { + return slices.Contains(desktops, s) + }) { + return nil + } + if len(entry.NotShowIn) > 0 && slices.ContainsFunc(entry.NotShowIn, func(s string) bool { + return slices.Contains(desktops, s) + }) { + return nil + } + if _, ok := seen[entry.Name]; ok { return nil } diff --git a/internal/desktop/desktop.go b/internal/desktop/desktop.go index ff123a4..c3bedb9 100644 --- a/internal/desktop/desktop.go +++ b/internal/desktop/desktop.go @@ -8,6 +8,7 @@ import ( func ParseEntry(data string) (DesktopEntry, bool) { var name string visible := true + var entry DesktopEntry for line := range strings.SplitSeq(data, "\n") { line = strings.TrimSpace(line) @@ -30,6 +31,20 @@ func ParseEntry(data string) (DesktopEntry, bool) { visible = false } } + if v, ok := strings.CutPrefix(line, "OnlyShowIn="); ok { + for s := range strings.SplitSeq(v, ";") { + if s != "" { + entry.OnlyShowIn = append(entry.OnlyShowIn, s) + } + } + } + if v, ok := strings.CutPrefix(line, "NotShowIn="); ok { + for s := range strings.SplitSeq(v, ";") { + if s != "" { + entry.NotShowIn = append(entry.NotShowIn, s) + } + } + } } return DesktopEntry{Name: name}, visible && name != "" @@ -49,8 +64,10 @@ func CleanExec(execLine string) string { } type DesktopEntry struct { - Name string - Path string + Name string + Path string + OnlyShowIn []string + NotShowIn []string } func EntryString(name, path string) string {