-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
86 lines (72 loc) · 1.47 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
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
)
func getTerminalSize() int {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err1 := cmd.Output()
res := string(out)
var rows, cols int
_, err2 := fmt.Sscanf(res, "%d %d", &rows, &cols)
if err1 != nil || err2 != nil || cols == 0 {
return 80
}
return cols
}
func coalesce(a, b string) string {
if a == "" {
return b
}
return a
}
func quote(s string) string {
return strconv.Quote(s)
}
func fatal(val ...interface{}) {
fmt.Fprintln(os.Stderr, val...)
os.Exit(1)
}
func eprintln(vals ...interface{}) (int, error) {
return fmt.Fprintln(os.Stderr, vals...)
}
func eprintf(format string, vals ...interface{}) (int, error) {
return fmt.Fprintf(os.Stderr, format, vals...)
}
func eprint(vals ...interface{}) (int, error) {
return fmt.Fprint(os.Stderr, vals...)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func check(e error, extra ...interface{}) {
if e != nil {
fmt.Fprintln(os.Stderr, extra...)
fatal(e.Error())
}
}
func log(vals ...interface{}) {
if options.Debug {
fmt.Fprintln(os.Stderr, vals...)
}
}
func size(bytes int64) string {
sizes := []int64{1000 * 1000 * 1000, 1000 * 1000, 1000}
names := []string{"GB", "MB", "KB"}
if bytes == -1 {
return "Unknown length"
}
for i, sz := range sizes {
if bytes > sz {
units := float64(bytes) / float64(sz)
return strconv.FormatFloat(units, 'f', 1, 64) + names[i]
}
}
return strconv.FormatInt(bytes, 10) + "B"
}