-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
55 lines (47 loc) · 882 Bytes
/
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
package main
import (
"fmt"
"strconv"
"syscall"
"unsafe"
)
// getTerminalWidth() {{{
func getTerminalWidth() int {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return int(ws.Col)
}
// }}}
// toHumanStr() {{{
func toHumanStr(value float64, human bool) string {
if !human {
return fmt.Sprint(uint(value/1024), " M")
}
units := []string{"K", "M", "G", "T", "P", "E", "Z", "Y"}
for _, unit := range units {
if value < 1024 {
return fmt.Sprintf("%.2f %s", value, unit)
}
value = value / 1024
}
return "Too much"
}
// }}}
// toFloat() {{{
func toFloat(raw string) float64 {
if raw == "" {
return 0
}
res, err := strconv.ParseFloat(raw, 64)
if err != nil {
return 0
}
return res
}
// }}}