commit a8e1b1b
delthas
·
2022-12-16 23:40:16 +0000 UTC
parent dcf5017
Move /NP code to go-libnp This enables /NP to work on Windows. See: https://github.com/delthas/go-libnp
3 files changed,
+26,
-71
+20,
-67
1@@ -11,8 +11,8 @@ import (
2
3 "git.sr.ht/~taiite/senpai/irc"
4 "git.sr.ht/~taiite/senpai/ui"
5+ "github.com/delthas/go-libnp"
6 "github.com/gdamore/tcell/v2"
7- "github.com/godbus/dbus/v5"
8 "golang.org/x/net/context"
9 )
10
11@@ -795,77 +795,30 @@ func (app *App) handleInput(buffer, content string) error {
12 func getSong() string {
13 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
14 defer cancel()
15-
16- bus, err := dbus.ConnectSessionBus(dbus.WithContext(ctx))
17+ info, err := libnp.GetInfo(ctx)
18 if err != nil {
19 return ""
20 }
21- defer bus.Close()
22-
23- var names []string
24- if err := bus.BusObject().CallWithContext(ctx, "org.freedesktop.DBus.ListNames", 0).Store(&names); err != nil {
25+ if info == nil {
26 return ""
27 }
28- song := ""
29- for _, name := range names {
30- if !strings.HasPrefix(name, "org.mpris.MediaPlayer2.") {
31- continue
32- }
33- var playing bool
34- o := bus.Object(name, "/org/mpris/MediaPlayer2")
35- var status string
36- if err := o.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, "org.mpris.MediaPlayer2.Player", "PlaybackStatus").Store(&status); err != nil {
37- continue
38- }
39- switch status {
40- case "Playing":
41- playing = true
42- case "Paused":
43- playing = false
44- default:
45- continue
46- }
47- var metadata map[string]dbus.Variant
48- if err := o.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, "org.mpris.MediaPlayer2.Player", "Metadata").Store(&metadata); err != nil {
49- continue
50- }
51- var trackURL string
52- var title string
53- var album string
54- var artist string
55- if e, ok := metadata["xesam:title"].Value().(string); ok {
56- title = e
57- }
58- if e, ok := metadata["xesam:album"].Value().(string); ok {
59- album = e
60- }
61- if e, ok := metadata["xesam:url"].Value().(string); ok {
62- trackURL = e
63- }
64- if e, ok := metadata["xesam:artist"].Value().([]string); ok && len(e) > 0 {
65- artist = e[0]
66- }
67- if title == "" {
68- continue
69- }
70- var sb strings.Builder
71- fmt.Fprintf(&sb, "\x02%s\x02", title)
72- if artist != "" {
73- fmt.Fprintf(&sb, " by \x02%s\x02", artist)
74- }
75- if album != "" {
76- fmt.Fprintf(&sb, " from \x02%s\x02", album)
77- }
78- if u, err := url.Parse(trackURL); err == nil {
79- switch u.Scheme {
80- case "http", "https":
81- fmt.Fprintf(&sb, " — %s", trackURL)
82- }
83- }
84- song = sb.String()
85- if playing {
86- return song
87+ if info.Title == "" {
88+ return ""
89+ }
90+
91+ var sb strings.Builder
92+ fmt.Fprintf(&sb, "\x02%s\x02", info.Title)
93+ if len(info.Artists) > 0 {
94+ fmt.Fprintf(&sb, " by \x02%s\x02", info.Artists[0])
95+ }
96+ if info.Album != "" {
97+ fmt.Fprintf(&sb, " from \x02%s\x02", info.Album)
98+ }
99+ if u, err := url.Parse(info.URL); err == nil {
100+ switch u.Scheme {
101+ case "http", "https":
102+ fmt.Fprintf(&sb, " — %s", info.URL)
103 }
104 }
105- return song
106+ return sb.String()
107 }
M
go.mod
+2,
-2
1@@ -4,9 +4,9 @@ go 1.16
2
3 require (
4 git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc
5- github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1
6+ github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59
7+ github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118
8 github.com/gdamore/tcell/v2 v2.5.4-0.20221017224006-ede1dd5ee680
9- github.com/godbus/dbus/v5 v5.1.0
10 github.com/mattn/go-runewidth v0.0.14
11 golang.org/x/net v0.0.0-20220722155237-a158d28d115b
12 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
M
go.sum
+4,
-2
1@@ -2,8 +2,10 @@ git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc h1:51BD67xFX+bozd
2 git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U=
3 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1 h1:sCu9bac1vv1LMBOsnUymZsi4iTO5PBQqn2Z3KJiGXXg=
6-github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1/go.mod h1:sG54BxlyQgIskYURLrg7mvhoGBe0Qq12DNtYRALwNa4=
7+github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59 h1:6EV6z3mGroSw761ChMFxys6qaY4iS9cfLmgpTjOEpYc=
8+github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59/go.mod h1:aGVXnhWpDlt5U4SphG97o1gszctZKvBTXy320E8Buw4=
9+github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118 h1:Xzf9ra1QRJXD62gwudjI2iBq7x9CusvHd83Dg2OnUmE=
10+github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118/go.mod h1:sG54BxlyQgIskYURLrg7mvhoGBe0Qq12DNtYRALwNa4=
11 github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
12 github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
13 github.com/gdamore/tcell/v2 v2.5.4-0.20221017224006-ede1dd5ee680 h1:bCjGvZsZNvhzJZ+gDmXpKdDPyf358nSPqepaTI1AsMQ=