summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordelthas <delthas@dille.cc>2022-12-17 00:40:16 +0100
committerdelthas <delthas@dille.cc>2022-12-17 00:41:39 +0100
commita8e1b1b416c28985c58f8a5c4a4c8867d0eaf660 (patch)
tree89ce2c5b818d758008a1bd61dd478c980e192233
parentPrevent possible deadlock in the typing system (diff)
Move /NP code to go-libnp
This enables /NP to work on Windows. See: https://github.com/delthas/go-libnp
-rw-r--r--commands.go87
-rw-r--r--go.mod4
-rw-r--r--go.sum6
3 files changed, 26 insertions, 71 deletions
diff --git a/commands.go b/commands.go
index 5a2c71b..3428ff2 100644
--- a/commands.go
+++ b/commands.go
@@ -11,8 +11,8 @@ import (
"git.sr.ht/~taiite/senpai/irc"
"git.sr.ht/~taiite/senpai/ui"
+ "github.com/delthas/go-libnp"
"github.com/gdamore/tcell/v2"
- "github.com/godbus/dbus/v5"
"golang.org/x/net/context"
)
@@ -795,77 +795,30 @@ func (app *App) handleInput(buffer, content string) error {
func getSong() string {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
-
- bus, err := dbus.ConnectSessionBus(dbus.WithContext(ctx))
+ info, err := libnp.GetInfo(ctx)
if err != nil {
return ""
}
- defer bus.Close()
-
- var names []string
- if err := bus.BusObject().CallWithContext(ctx, "org.freedesktop.DBus.ListNames", 0).Store(&names); err != nil {
+ if info == nil {
return ""
}
- song := ""
- for _, name := range names {
- if !strings.HasPrefix(name, "org.mpris.MediaPlayer2.") {
- continue
- }
- var playing bool
- o := bus.Object(name, "/org/mpris/MediaPlayer2")
- var status string
- if err := o.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, "org.mpris.MediaPlayer2.Player", "PlaybackStatus").Store(&status); err != nil {
- continue
- }
- switch status {
- case "Playing":
- playing = true
- case "Paused":
- playing = false
- default:
- continue
- }
- var metadata map[string]dbus.Variant
- if err := o.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, "org.mpris.MediaPlayer2.Player", "Metadata").Store(&metadata); err != nil {
- continue
- }
- var trackURL string
- var title string
- var album string
- var artist string
- if e, ok := metadata["xesam:title"].Value().(string); ok {
- title = e
- }
- if e, ok := metadata["xesam:album"].Value().(string); ok {
- album = e
- }
- if e, ok := metadata["xesam:url"].Value().(string); ok {
- trackURL = e
- }
- if e, ok := metadata["xesam:artist"].Value().([]string); ok && len(e) > 0 {
- artist = e[0]
- }
- if title == "" {
- continue
- }
- var sb strings.Builder
- fmt.Fprintf(&sb, "\x02%s\x02", title)
- if artist != "" {
- fmt.Fprintf(&sb, " by \x02%s\x02", artist)
- }
- if album != "" {
- fmt.Fprintf(&sb, " from \x02%s\x02", album)
- }
- if u, err := url.Parse(trackURL); err == nil {
- switch u.Scheme {
- case "http", "https":
- fmt.Fprintf(&sb, " — %s", trackURL)
- }
- }
- song = sb.String()
- if playing {
- return song
+ if info.Title == "" {
+ return ""
+ }
+
+ var sb strings.Builder
+ fmt.Fprintf(&sb, "\x02%s\x02", info.Title)
+ if len(info.Artists) > 0 {
+ fmt.Fprintf(&sb, " by \x02%s\x02", info.Artists[0])
+ }
+ if info.Album != "" {
+ fmt.Fprintf(&sb, " from \x02%s\x02", info.Album)
+ }
+ if u, err := url.Parse(info.URL); err == nil {
+ switch u.Scheme {
+ case "http", "https":
+ fmt.Fprintf(&sb, " — %s", info.URL)
}
}
- return song
+ return sb.String()
}
diff --git a/go.mod b/go.mod
index 8cfff1b..2c09afc 100644
--- a/go.mod
+++ b/go.mod
@@ -4,9 +4,9 @@ go 1.16
require (
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc
- github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1
+ github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59
+ github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118
github.com/gdamore/tcell/v2 v2.5.4-0.20221017224006-ede1dd5ee680
- github.com/godbus/dbus/v5 v5.1.0
github.com/mattn/go-runewidth v0.0.14
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
diff --git a/go.sum b/go.sum
index b421a96..65d2427 100644
--- a/go.sum
+++ b/go.sum
@@ -2,8 +2,10 @@ git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc h1:51BD67xFX+bozd
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1 h1:sCu9bac1vv1LMBOsnUymZsi4iTO5PBQqn2Z3KJiGXXg=
-github.com/delthas/go-localeinfo v0.0.0-20221115102303-5a7785a1acc1/go.mod h1:sG54BxlyQgIskYURLrg7mvhoGBe0Qq12DNtYRALwNa4=
+github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59 h1:6EV6z3mGroSw761ChMFxys6qaY4iS9cfLmgpTjOEpYc=
+github.com/delthas/go-libnp v0.0.0-20221216233833-93742f416e59/go.mod h1:aGVXnhWpDlt5U4SphG97o1gszctZKvBTXy320E8Buw4=
+github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118 h1:Xzf9ra1QRJXD62gwudjI2iBq7x9CusvHd83Dg2OnUmE=
+github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118/go.mod h1:sG54BxlyQgIskYURLrg7mvhoGBe0Qq12DNtYRALwNa4=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.5.4-0.20221017224006-ede1dd5ee680 h1:bCjGvZsZNvhzJZ+gDmXpKdDPyf358nSPqepaTI1AsMQ=