-
Notifications
You must be signed in to change notification settings - Fork 0
/
anybar.go
95 lines (77 loc) · 1.99 KB
/
anybar.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
// Package anybar provides a client for interacting with the
// AnyBar macOS menubar status indicator app.
package anybar // import "tantalic.com/anybar"
import "net"
import "fmt"
// Client is used to send commands to the AnyBar application.
type Client struct {
// Port is the UDP port used to control the AnyBar application.
Port int
// Hostname is the host where AnyBar is running.
Hostname string
}
// The following default values will be used on anybar.Client
// if not set.
const (
DefaultPort = 1738
DefaultHostname = "localhost"
)
// Set changes the style of the AnyBar menu bar icon.
func (c *Client) Set(s Style) error {
return c.send([]byte(s))
}
// Quit tells the AnyBar app to exit.
func (c *Client) Quit() error {
return c.send([]byte(quit))
}
func (c *Client) send(b []byte) error {
laddr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
return err
}
anybarAddr, err := net.ResolveUDPAddr("udp4", c.addr())
if err != nil {
return err
}
conn, err := net.ListenUDP("udp4", laddr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.WriteToUDP(b, anybarAddr)
if err != nil {
return err
}
return nil
}
func (c *Client) addr() string {
if c.Hostname == "" {
c.Hostname = DefaultHostname
}
if c.Port == 0 {
c.Port = DefaultPort
}
return fmt.Sprintf("%s:%d", c.Hostname, c.Port)
}
// Style is used to represent the various appearances that can be applied
// to AnyBar icon in the menu bar. You should not create your own Style
// instances but instead use one of the provided constants in the package.
type Style string
// The following styles are available for controlling the appearance of the
// AnyBar menu bar icon.
const (
White = Style("white")
Red = Style("red")
Orange = Style("orange")
Yellow = Style("yellow")
Green = Style("green")
Cyan = Style("cyan")
Blue = Style("blue")
Purple = Style("purple")
Black = Style("black")
Question = Style("question")
Exclamation = Style("exclamation")
)
const (
quit = "quit"
)