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
  | 
package drive
import (
    "os"
    "fmt"
    "strings"
    "strconv"
    "unicode/utf8"
    "time"
)
type kv struct {
    key string
    value string
}
func formatList(a []string) string {
    return strings.Join(a, ", ")
}
func formatSize(bytes int64, forceBytes bool) string {
    if bytes == 0 {
        return ""
    }
    if forceBytes {
        return fmt.Sprintf("%v B", bytes)
    }
    units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
    var i int
    value := float64(bytes)
    for value > 1000 {
        value /= 1000
        i++
    }
    return fmt.Sprintf("%.1f %s", value, units[i])
}
func formatBool(b bool) string {
    return strings.Title(strconv.FormatBool(b))
}
func formatDatetime(iso string) string {
    t, err := time.Parse(time.RFC3339, iso)
    if err != nil {
        return iso
    }
    local := t.Local()
    year, month, day := local.Date()
    hour, min, sec := local.Clock()
    return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)
}
// Truncates string to given max length, and inserts ellipsis into
// the middle of the string to signify that the string has been truncated
func truncateString(str string, maxRunes int) string {
    indicator := "..."
    // Number of runes in string
    runeCount := utf8.RuneCountInString(str)
    // Return input string if length of input string is less than max length
    // Input string is also returned if max length is less than 9 which is the minmal supported length
    if runeCount <= maxRunes || maxRunes < 9 {
        return str
    }
    // Number of remaining runes to be removed
    remaining := (runeCount - maxRunes) + utf8.RuneCountInString(indicator)
    var truncated string
    var skip bool
    for leftOffset, char := range str {
        rightOffset := runeCount - (leftOffset + remaining)
        // Start skipping chars when the left and right offsets are equal
        // Or in the case where we wont be able to do an even split: when the left offset is larger than the right offset
        if leftOffset == rightOffset || (leftOffset > rightOffset && !skip) {
            skip = true
            truncated += indicator
        }
        if skip && remaining > 0 {
            // Skip char and decrement the remaining skip counter
            remaining--
            continue
        }
        // Add char to result string
        truncated += string(char)
    }
    // Return truncated string
    return truncated
}
func fileExists(path string) bool {
    _, err := os.Stat(path)
    if err == nil {
        return true
    }
    return false
}
func intMax() int64 {
    return 1 << (strconv.IntSize - 1) - 1
}
  |