forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
158 lines (142 loc) · 3.12 KB
/
utils.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
148
149
150
151
152
153
154
155
156
157
158
package wallutils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
var h24 = time.Hour * 24
// c formats a timestamp as HH:MM
func c(t time.Time) string {
return fmt.Sprintf("%.2d:%.2d", t.Hour(), t.Minute())
}
// has checks if a string slice has the given element
func has(sl []string, e string) bool {
for _, s := range sl {
if s == e {
return true
}
}
return false
}
// unique removes all repeated elements from a slice of strings
func unique(sl []string) []string {
var nl []string
for _, s := range sl {
if !has(nl, s) {
nl = append(nl, s)
}
}
return nl
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
// firstname finds the part of a filename before the extension
func firstname(filename string) string {
ext := filepath.Ext(filename)
return filename[:len(filename)-len(ext)]
}
// exists checks if the given path exists
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func which(executable string) string {
p, err := exec.LookPath(executable)
if err != nil {
return ""
}
return p
}
func run(shellCommand string, verbose bool) error {
if verbose {
fmt.Println(shellCommand)
}
cmd := exec.Command("sh", "-c", shellCommand)
if _, err := cmd.CombinedOutput(); err != nil {
return err
}
return nil
}
func output(shellCommand string, verbose bool) string {
if verbose {
fmt.Println(shellCommand)
}
cmd := exec.Command("sh", "-c", shellCommand)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
return ""
}
return string(stdoutStderr)
}
func containsE(envVar, subString string) bool {
return strings.Contains(os.Getenv(envVar), subString)
}
func hasE(envVar string) bool {
return os.Getenv(envVar) != ""
}
// CommonPrefix will find the longest common prefix in a slice of strings
func CommonPrefix(sl []string) string {
if len(sl) == 0 {
return ""
}
shortestLength := len(sl[0])
shortestString := sl[0]
for _, s := range sl {
if len(s) < shortestLength {
shortestLength = len(s)
shortestString = s
}
}
if shortestLength == 0 {
return ""
}
for i := 1; i < shortestLength; i++ {
for _, s := range sl {
if !strings.HasPrefix(s, shortestString[:i]) {
return shortestString[:i-1]
}
}
}
return shortestString
}
// CommonPrefix will find the longest common suffix in a slice of strings
func CommonSuffix(sl []string) string {
if len(sl) == 0 {
return ""
}
shortestLength := len(sl[0])
shortestString := sl[0]
for _, s := range sl {
if len(s) < shortestLength {
shortestLength = len(s)
shortestString = s
}
}
if shortestLength == 0 {
return ""
}
for i := 1; i < shortestLength; i++ {
for _, s := range sl {
if !strings.HasSuffix(s, shortestString[shortestLength-i:]) {
return shortestString[shortestLength-(i-1):]
}
}
}
return shortestString
}
// Meat returns the meat of the string: the part that is after the prefix and
// before the suffix. Will return the given string if it is too short to
// contain the prefix and suffix.
func Meat(s, prefix, suffix string) string {
if len(s) < (len(prefix) + len(suffix)) {
return s
}
return s[len(prefix) : len(s)-len(suffix)]
}