"Fossies" - the Fresh Open Source Software Archive 
Member "gdrive-2.1.1/drive/util.go" (28 May 2021, 3518 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 package drive
2
3 import (
4 "fmt"
5 "math"
6 "os"
7 "path/filepath"
8 "strconv"
9 "strings"
10 "time"
11 "unicode/utf8"
12 )
13
14 type kv struct {
15 key string
16 value string
17 }
18
19 func formatList(a []string) string {
20 return strings.Join(a, ", ")
21 }
22
23 func formatSize(bytes int64, forceBytes bool) string {
24 if bytes == 0 {
25 return ""
26 }
27
28 if forceBytes {
29 return fmt.Sprintf("%v B", bytes)
30 }
31
32 units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
33
34 var i int
35 value := float64(bytes)
36
37 for value > 1000 {
38 value /= 1000
39 i++
40 }
41 return fmt.Sprintf("%.1f %s", value, units[i])
42 }
43
44 func calcRate(bytes int64, start, end time.Time) int64 {
45 seconds := float64(end.Sub(start).Seconds())
46 if seconds < 1.0 {
47 return bytes
48 }
49 return round(float64(bytes) / seconds)
50 }
51
52 func round(n float64) int64 {
53 if n < 0 {
54 return int64(math.Ceil(n - 0.5))
55 }
56 return int64(math.Floor(n + 0.5))
57 }
58
59 func formatBool(b bool) string {
60 return strings.Title(strconv.FormatBool(b))
61 }
62
63 func formatDatetime(iso string) string {
64 t, err := time.Parse(time.RFC3339, iso)
65 if err != nil {
66 return iso
67 }
68 local := t.Local()
69 year, month, day := local.Date()
70 hour, min, sec := local.Clock()
71 return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)
72 }
73
74 // Truncates string to given max length, and inserts ellipsis into
75 // the middle of the string to signify that the string has been truncated
76 func truncateString(str string, maxRunes int) string {
77 indicator := "..."
78
79 // Number of runes in string
80 runeCount := utf8.RuneCountInString(str)
81
82 // Return input string if length of input string is less than max length
83 // Input string is also returned if max length is less than 9 which is the minmal supported length
84 if runeCount <= maxRunes || maxRunes < 9 {
85 return str
86 }
87
88 // Number of remaining runes to be removed
89 remaining := (runeCount - maxRunes) + utf8.RuneCountInString(indicator)
90
91 var truncated string
92 var skip bool
93
94 for leftOffset, char := range str {
95 rightOffset := runeCount - (leftOffset + remaining)
96
97 // Start skipping chars when the left and right offsets are equal
98 // Or in the case where we wont be able to do an even split: when the left offset is larger than the right offset
99 if leftOffset == rightOffset || (leftOffset > rightOffset && !skip) {
100 skip = true
101 truncated += indicator
102 }
103
104 if skip && remaining > 0 {
105 // Skip char and decrement the remaining skip counter
106 remaining--
107 continue
108 }
109
110 // Add char to result string
111 truncated += string(char)
112 }
113
114 // Return truncated string
115 return truncated
116 }
117
118 func fileExists(path string) bool {
119 _, err := os.Stat(path)
120 if err == nil {
121 return true
122 }
123 return false
124 }
125
126 func mkdir(path string) error {
127 dir := filepath.Dir(path)
128 if fileExists(dir) {
129 return nil
130 }
131 return os.MkdirAll(dir, 0775)
132 }
133
134 func intMax() int64 {
135 return 1<<(strconv.IntSize-1) - 1
136 }
137
138 func pathLength(path string) int {
139 return strings.Count(path, string(os.PathSeparator))
140 }
141
142 func parentFilePath(path string) string {
143 dir, _ := filepath.Split(path)
144 return filepath.Dir(dir)
145 }
146
147 func pow(x int, y int) int {
148 f := math.Pow(float64(x), float64(y))
149 return int(f)
150 }
151
152 func min(x int, y int) int {
153 n := math.Min(float64(x), float64(y))
154 return int(n)
155 }
156
157 func openFile(path string) (*os.File, os.FileInfo, error) {
158 f, err := os.Open(path)
159 if err != nil {
160 return nil, nil, fmt.Errorf("Failed to open file: %s", err)
161 }
162
163 info, err := f.Stat()
164 if err != nil {
165 return nil, nil, fmt.Errorf("Failed getting file metadata: %s", err)
166 }
167
168 return f, info, nil
169 }