-
Notifications
You must be signed in to change notification settings - Fork 8
/
toast_darwin.go
95 lines (79 loc) · 2.11 KB
/
toast_darwin.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
//go:build darwin
package toast
import (
"fmt"
"os/exec"
"strings"
)
func WithSubtitle(subtitle string) NotificationOption {
return func(n *notification) {
n.Subtitle = subtitle
}
}
const (
Basso Audio = "Basso"
Blow Audio = "Blow"
Bottle Audio = "Bottle"
Frog Audio = "Frog"
Funk Audio = "Funk"
Glass Audio = "Glass"
Hero Audio = "Hero"
Morse Audio = "Morse"
Ping Audio = "Ping"
Pop Audio = "Pop"
Purr Audio = "Purr"
Sosumi Audio = "Sosumi"
Submarine Audio = "Submarine"
Tink Audio = "Tink"
)
var _ notifier = (*notification)(nil)
func newNotification(message string, opts ...NotificationOption) *notification {
n := ¬ification{
Title: "GO APP",
Message: message,
}
for _, fn := range opts {
fn(n)
}
return n
}
func (n *notification) push() error {
if n._useObjC {
return n.pushWithObjC()
}
script := n.template()
osa, err := exec.LookPath("osascript")
if err != nil {
return err
}
cmd := exec.Command(osa, "-e", script)
return cmd.Run()
}
func (n *notification) template() (script string) {
tpl := `display notification "%s" with title "%s"`
script = fmt.Sprintf(tpl, escapeNotificationString(n.Message), escapeNotificationString(n.Title))
if len(n.Subtitle) != 0 {
script += fmt.Sprintf(` subtitle "%s"`, escapeNotificationString(n.Subtitle))
}
if len(n.Audio) != 0 {
script += fmt.Sprintf(` sound name "%s"`, escapeNotificationString(string(n.Audio)))
}
return
}
type notification struct {
// The main title/heading for the notification.
Title string `json:"title"`
Subtitle string `json:"subtitle"`
// The single/multi line message to display for the notification.
Message string `json:"message"`
// The audio to play when displaying the notification
Audio Audio `json:"audio"`
_useObjC bool
// Fakes the sender application of the notification.
// This uses the specified application’s icon, and will launch it when the notification is clicked.
BundleID string `json:"bundle_id"`
}
func escapeNotificationString(s string) string {
s = strings.ReplaceAll(s, `\`, `\\`)
return strings.ReplaceAll(s, `"`, `\"`)
}