/
util.go
132 lines (111 loc) · 3.23 KB
/
util.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/pkg/browser"
)
func copyToClipboard(data string) error {
logrus.Infof("copy data: %s", strings.Replace(truncate(data, 50), "\n", "\\n", -1))
var cmd *exec.Cmd
// TODO support more than OSX and tmux
if _, err := exec.LookPath("pbcopy"); err == nil {
cmd = exec.Command("pbcopy")
} else if _, err := exec.LookPath("clip"); err == nil {
cmd = exec.Command("clip")
} else if _, err := exec.LookPath("xclip"); err == nil {
cmd = exec.Command("xclip", "-selection", "c")
} else if _, err := exec.LookPath("tmux"); err == nil {
cmd = exec.Command("tmux", "load-buffer", "-")
}
cmd.Stdin = strings.NewReader(data)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func openURL(data string, isHTML bool) error {
if isHTML {
tmpfile, err := ioutil.TempFile("", "pmbopenurl")
if err != nil {
return err
}
_, err = tmpfile.Write([]byte(data))
if err != nil {
return err
}
err = tmpfile.Close()
if err != nil {
return err
}
nameWithSuffix := fmt.Sprintf("%s.html", tmpfile.Name())
err = os.Rename(tmpfile.Name(), nameWithSuffix)
if err != nil {
return err
}
go func() {
time.Sleep(15 * time.Second)
logrus.Infof("cleaning up temporary file: %s", nameWithSuffix)
os.Remove(nameWithSuffix)
}()
logrus.Infof("opening file: %s", nameWithSuffix)
return browser.OpenFile(nameWithSuffix)
}
logrus.Infof("opening url: %s", data)
return browser.OpenURL(data)
}
func truncate(data string, length int) string {
if len(data) > length {
return fmt.Sprintf("%s (truncated)", data[0:length])
}
return data
}
func displayNotice(message string, sticky bool) error {
stickyText := "sticky"
if !sticky {
stickyText = "not sticky"
}
logrus.Infof("display message: %s (%s)", message, stickyText)
var cmd *exec.Cmd
path := os.Getenv("PATH")
logrus.Debugf("looking for notifiers in path: %s", path)
if _, err := exec.LookPath("growlnotify"); err == nil {
cmdParts := []string{"growlnotify", "-m", message}
if sticky {
cmdParts = append(cmdParts, "-s")
}
logrus.Debugf("Using growlnotify for notification.")
cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
} else if _, err := exec.LookPath("terminal-notifier"); err == nil {
cmd = exec.Command("terminal-notifier", "-message", message)
logrus.Debugf("Using terminal-notifier for notification.")
} else if _, err := exec.LookPath("SnoreToast"); err == nil {
cmd = exec.Command("SnoreToast", "-silent", "-t", "PMB", "-m", message)
logrus.Debugf("Using SnoreToast for notification.")
} else if _, err := exec.LookPath("notify-send"); err == nil {
cmdParts := []string{"notify-send", "pmb", message}
if sticky {
cmdParts = append(cmdParts, "-t", "60")
} else {
cmdParts = append(cmdParts, "-t", "3")
}
logrus.Infof("Using notify-send for notification.")
cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
} else if _, err := exec.LookPath("tmux"); err == nil {
cmd = exec.Command("tmux", "display-message", message)
logrus.Debugf("Using tmux for notification.")
} else {
logrus.Warningf("Unable to display notice.")
return nil
}
err := cmd.Run()
if err != nil {
return err
}
return nil
}