-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (141 loc) · 4.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"fmt"
"os"
"github.com/godbus/dbus/v5"
)
const (
nextUnicode = "\uf051"
prevUnicode = "\uf048"
pauseUnicode = "\uf04b"
playUnicode = "\uf04c"
dashUnicode = "\u2014"
)
func trim_or_pad(s string, n int) string {
if len(s) > n {
return s[:n]
} else {
pad := ""
for i := 0; i < (n - len(s)); i++ {
pad = pad + " "
}
return s + pad
}
}
func main() {
var playPause = flag.Bool("playpause", false, "Toggle Play/Pause, depending on current status")
var playPauseIcon = flag.Bool("playpause-icon", false, "Print the icon for play/pause")
var next = flag.Bool("next", false, "Go to next song")
var nextIcon = flag.Bool("nextIcon", false, "Print the next icon")
var prev = flag.Bool("prev", false, "Go to previous song")
var prevIcon = flag.Bool("prevIcon", false, "Print the prev icon")
var justify = flag.Int("justify", 75, "Justifies the output to the specified number of characters, padding or trimming")
var printAlbum = flag.Bool("album", false, "Print also the album information in addition to name and artist")
flag.Parse()
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
os.Exit(1)
}
defer conn.Close()
obj := conn.Object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
currentStatus, err := obj.GetProperty("org.mpris.MediaPlayer2.Player.PlaybackStatus")
if err != nil {
os.Exit(1)
}
status := currentStatus.String()
if status != "\"Playing\"" && status != "\"Paused\"" && status != "\"Stopped\"" {
os.Exit(0)
}
switch {
case *next:
obj.Call("org.mpris.MediaPlayer2.Player.Next", 0)
os.Exit(0)
case *nextIcon:
fmt.Println(nextUnicode)
os.Exit(0)
case *prev:
obj.Call("org.mpris.MediaPlayer2.Player.Previous", 0)
os.Exit(0)
case *prevIcon:
fmt.Println(prevUnicode)
os.Exit(0)
case *playPause:
obj.Call("org.mpris.MediaPlayer2.Player.PlayPause", 0)
os.Exit(0)
case *playPauseIcon:
if status != "\"Playing\"" {
fmt.Println(pauseUnicode)
} else {
fmt.Println(playUnicode)
}
if err = conn.AddMatchSignal(
dbus.WithMatchObjectPath("/org/mpris/MediaPlayer2"),
); err != nil {
panic(err)
}
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
for _ = range c {
currentStatus, err := obj.GetProperty("org.mpris.MediaPlayer2.Player.PlaybackStatus")
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get property:", err)
os.Exit(1)
}
status := currentStatus.String()
if status != "\"Playing\"" {
fmt.Println(pauseUnicode)
} else {
fmt.Println(playUnicode)
}
}
default:
metadata, err := obj.GetProperty("org.mpris.MediaPlayer2.Player.Metadata")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var status_string string
var artist string
values := metadata.Value()
title := values.(map[string]dbus.Variant)["xesam:title"]
artist_data := values.(map[string]dbus.Variant)["xesam:artist"].Value().([]string)
if len(artist_data) > 0 {
artist = values.(map[string]dbus.Variant)["xesam:artist"].Value().([]string)[0]
} else {
os.Exit(1)
}
if *printAlbum {
album := values.(map[string]dbus.Variant)["xesam:album"]
status_string = fmt.Sprintf("%s %s %s (%s)", title, dashUnicode, artist, album)
} else {
status_string = fmt.Sprintf("%s %s %s", title, dashUnicode, artist)
}
fmt.Println(trim_or_pad(status_string, *justify))
if err = conn.AddMatchSignal(
dbus.WithMatchObjectPath("/org/mpris/MediaPlayer2"),
); err != nil {
panic(err)
}
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
for _ = range c {
metadata, err := obj.GetProperty("org.mpris.MediaPlayer2.Player.Metadata")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
values := metadata.Value()
title := values.(map[string]dbus.Variant)["xesam:title"]
artist := values.(map[string]dbus.Variant)["xesam:artist"].Value().([]string)[0]
if *printAlbum {
album := values.(map[string]dbus.Variant)["xesam:album"]
status_string = fmt.Sprintf("%s %s %s (%s)", title, dashUnicode, artist, album)
} else {
status_string = fmt.Sprintf("%s %s %s", title, dashUnicode, artist)
}
fmt.Println(trim_or_pad(status_string, *justify))
}
}
}