Debian Codegraph @

lib/notification/notify_darwin.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package notification
18
19 import (
20 "fmt"
21 "os/exec"
22 )
23
24 var terminalNotifierAvailable = false
25
26 func init() {
27 if err := exec.Command("which", "terminal-notifier").Run(); err != nil {
28 terminalNotifierAvailable = false
29 }
30 terminalNotifierAvailable = true
31 }
32
33 const sendScript = `on run {notifText, notifTitle}
34 display notification notifText with title "gomuks" subtitle notifTitle
35 end run`
36
37 func Send(title, text string, critical, sound bool) error {
38 if terminalNotifierAvailable {
39 args := []string{"-title", "gomuks", "-subtitle", title, "-message", text}
40 if critical {
41 args = append(args, "-timeout", "15")
42 } else {
43 args = append(args, "-timeout", "4")
44 }
45 if sound {
46 args = append(args, "-sound", "default")
47 }
48 //if len(iconPath) > 0 {
49 // args = append(args, "-appIcon", iconPath)
50 //}
51 return exec.Command("terminal-notifier", args...).Run()
52 }
53 cmd := exec.Command("osascript", "-", text, title)
54 if stdin, err := cmd.StdinPipe(); err != nil {
55 return fmt.Errorf("failed to get stdin pipe for osascript: %w", err)
56 } else if _, err = stdin.Write([]byte(sendScript)); err != nil {
57 return fmt.Errorf("failed to write notification script to osascript: %w", err)
58 } else if err = cmd.Run(); err != nil {
59 return fmt.Errorf("failed to run notification script: %w", err)
60 } else if !cmd.ProcessState.Success() {
61 return fmt.Errorf("notification script exited unsuccessfully")
62 } else {
63 return nil
64 }
65 }