-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
128 lines (106 loc) · 2.6 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
package util
import (
"database/sql"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"runtime"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
func EnsureDir(fileName string) {
dirName := filepath.Dir(fileName)
if _, err := os.Stat(dirName); err != nil {
if err = os.MkdirAll(dirName, os.ModePerm); err != nil {
panic(err)
}
}
}
func ToNullString(s string) sql.NullString {
return sql.NullString{String: s, Valid: s != ""}
}
func CenterStr(s string, w int) string {
offset := (w + len(s)) / 2
if (w+len(s))%2 != 0 {
offset++
}
return fmt.Sprintf("%[1]*s", -w, fmt.Sprintf("%[1]*s", offset, s))
}
func GetTerminalSize() (int, int) {
width, height, err := terminal.GetSize(0)
if err != nil {
width = 80
height = 25
}
return width, height
}
func ReplaceRuneAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}
func StrPad(input string, padLength int, padString string, padType string) string {
var output string
inputLength := len(input)
padStringLength := len(padString)
if inputLength >= padLength {
return input
}
repeat := math.Ceil(float64(1) + (float64(padLength-padStringLength))/float64(padStringLength))
switch padType {
case "RIGHT":
output = input + strings.Repeat(padString, int(repeat))
output = output[:padLength]
case "LEFT":
output = strings.Repeat(padString, int(repeat)) + input
output = output[len(output)-padLength:]
case "BOTH":
length := (float64(padLength - inputLength)) / float64(2)
repeat = math.Ceil(length / float64(padStringLength))
output = strings.Repeat(padString, int(repeat))[:int(math.Floor(float64(length)))] +
input + strings.Repeat(padString, int(repeat))[:int(math.Ceil(float64(length)))]
}
return output
}
func Contains(ss []string, s string) bool {
for _, x := range ss {
if x == s {
return true
}
}
return false
}
func UserDataDir() (string, error) {
var dir string
switch runtime.GOOS {
// case "windows":
// dir = Getenv("LocalAppData")
// if dir == "" {
// return "", errors.New("%LocalAppData% is not defined")
// }
// case "darwin", "ios":
// dir = Getenv("HOME")
// if dir == "" {
// return "", errors.New("$HOME is not defined")
// }
// dir += "/Library/Caches"
// case "plan9":
// dir = Getenv("home")
// if dir == "" {
// return "", errors.New("$home is not defined")
// }
// dir += "/lib/cache"
default: // Unix
dir = os.Getenv("XDG_DATA_HOME")
if dir == "" {
dir = os.Getenv("HOME")
if dir == "" {
return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
}
dir += "/.local/share"
}
}
return dir, nil
}