forked from odeke-em/drive
/
misc.go
399 lines (332 loc) · 7.6 KB
/
misc.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
spinner "github.com/odeke-em/cli-spinner"
)
const (
MimeTypeJoiner = "-"
RemoteDriveRootPath = "My Drive"
)
var BytesPerKB = float64(1024)
type desktopEntry struct {
name string
url string
icon string
}
type playable struct {
play func()
pause func()
reset func()
stop func()
}
func noop() {
}
func noopPlayable() *playable {
return &playable{
play: noop,
pause: noop,
reset: noop,
stop: noop,
}
}
func newPlayable(freq int64) *playable {
spin := spinner.New(freq)
play := func() {
spin.Start()
}
return &playable{
play: play,
stop: spin.Stop,
reset: spin.Reset,
pause: spin.Stop,
}
}
func (g *Commands) playabler() *playable {
if !g.opts.canPrompt() {
return noopPlayable()
}
return newPlayable(10)
}
func rootLike(p string) bool {
return p == "/" || p == "" || p == "root"
}
func remoteRootLike(p string) bool {
return p == RemoteDriveRootPath
}
type byteDescription func(b int64) string
func memoizeBytes() byteDescription {
cache := map[int64]string{}
suffixes := []string{"B", "KB", "MB", "GB", "TB", "PB"}
maxLen := len(suffixes) - 1
return func(b int64) string {
description, ok := cache[b]
if ok {
return description
}
bf := float64(b)
i := 0
description = ""
for {
if bf/BytesPerKB < 1 || i >= maxLen {
description = fmt.Sprintf("%.2f%s", bf, suffixes[i])
break
}
bf /= BytesPerKB
i += 1
}
cache[b] = description
return description
}
}
var prettyBytes = memoizeBytes()
func sepJoin(sep string, args ...string) string {
return strings.Join(args, sep)
}
func sepJoinNonEmpty(sep string, args ...string) string {
nonEmpties := NonEmptyStrings(args...)
return sepJoin(sep, nonEmpties...)
}
func isHidden(p string, ignore bool) bool {
if strings.HasPrefix(p, ".") {
return !ignore
}
return false
}
func prompt(r *os.File, w *os.File, promptText ...interface{}) (input string) {
fmt.Fprint(w, promptText...)
flushTTYin()
fmt.Fscanln(r, &input)
return
}
func nextPage() bool {
input := prompt(os.Stdin, os.Stdout, "---More---")
if len(input) >= 1 && strings.ToLower(input[:1]) == QuitShortKey {
return false
}
return true
}
func promptForChanges(args ...interface{}) bool {
argv := []interface{}{
"Proceed with the changes? [Y/n]:",
}
if len(args) >= 1 {
argv = args
}
input := prompt(os.Stdin, os.Stdout, argv...)
if input == "" {
input = YesShortKey
}
return strings.ToUpper(input) == YesShortKey
}
func (f *File) toDesktopEntry(urlMExt *urlMimeTypeExt) *desktopEntry {
name := f.Name
if urlMExt.ext != "" {
name = sepJoin("-", f.Name, urlMExt.ext)
}
return &desktopEntry{
name: name,
url: urlMExt.url,
icon: urlMExt.mimeType,
}
}
func (f *File) serializeAsDesktopEntry(destPath string, urlMExt *urlMimeTypeExt) (int, error) {
deskEnt := f.toDesktopEntry(urlMExt)
handle, err := os.Create(destPath)
if err != nil {
return 0, err
}
defer handle.Close()
icon := strings.Replace(deskEnt.icon, UnescapedPathSep, MimeTypeJoiner, -1)
return fmt.Fprintf(handle, "[Desktop Entry]\nIcon=%s\nName=%s\nType=%s\nURL=%s\n",
icon, deskEnt.name, LinkKey, deskEnt.url)
}
func remotePathSplit(p string) (dir, base string) {
// Avoiding use of filepath.Split because of bug with trailing "/" not being stripped
sp := strings.Split(p, "/")
spl := len(sp)
dirL, baseL := sp[:spl-1], sp[spl-1:]
dir = strings.Join(dirL, "/")
base = strings.Join(baseL, "/")
return
}
func commonPrefix(values ...string) string {
vLen := len(values)
if vLen < 1 {
return ""
}
minIndex := 0
min := values[0]
minLen := len(min)
for i := 1; i < vLen; i += 1 {
st := values[i]
if st == "" {
return ""
}
lst := len(st)
if lst < minLen {
min = st
minLen = lst
minIndex = i + 0
}
}
prefix := make([]byte, minLen)
matchOn := true
for i := 0; i < minLen; i += 1 {
for j, other := range values {
if minIndex == j {
continue
}
if other[i] != min[i] {
matchOn = false
break
}
}
if !matchOn {
break
}
prefix[i] = min[i]
}
return string(prefix)
}
func readCommentedFile(p, comment string) (clauses []string, err error) {
f, fErr := os.Open(p)
if fErr != nil || f == nil {
err = fErr
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for {
if !scanner.Scan() {
break
}
line := scanner.Text()
line = strings.Trim(line, " ")
line = strings.Trim(line, "\n")
if strings.HasPrefix(line, comment) || len(line) < 1 {
continue
}
clauses = append(clauses, line)
}
return
}
func chunkInt64(v int64) chan int {
var maxInt int
maxInt = 1<<31 - 1
maxIntCast := int64(maxInt)
chunks := make(chan int)
go func() {
q, r := v/maxIntCast, v%maxIntCast
for i := int64(0); i < q; i += 1 {
chunks <- maxInt
}
if r > 0 {
chunks <- int(r)
}
close(chunks)
}()
return chunks
}
func nonEmptyStrings(fn func(string) string, v ...string) (splits []string) {
for _, elem := range v {
if fn != nil {
elem = fn(elem)
}
if elem != "" {
splits = append(splits, elem)
}
}
return
}
func NonEmptyStrings(v ...string) (splits []string) {
return nonEmptyStrings(nil, v...)
}
func NonEmptyTrimmedStrings(v ...string) (splits []string) {
return nonEmptyStrings(strings.TrimSpace, v...)
}
var regExtStrMap = map[string]string{
"csv": "text/csv",
"html?": "text/html",
"te?xt": "text/plain",
"gif": "image/gif",
"png": "image/png",
"svg": "image/svg+xml",
"jpe?g": "image/jpeg",
"odt": "application/vnd.oasis.opendocument.text",
"rtf": "application/rtf",
"pdf": "application/pdf",
"apk": "application/vnd.android.package-archive",
"bin": "application/octet-stream",
"docx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"pptx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.presentation",
"xlsx?": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
}
var regExtMap = func() map[*regexp.Regexp]string {
regMap := make(map[*regexp.Regexp]string)
for regStr, mimeType := range regExtStrMap {
regExComp, err := regexp.Compile(regStr)
if err == nil {
regMap[regExComp] = mimeType
}
}
return regMap
}()
func _mimeTyper() func(string) string {
cache := map[string]string{}
return func(ext string) string {
memoized, ok := cache[ext]
if ok {
return memoized
}
bExt := []byte(ext)
for regEx, mimeType := range regExtMap {
if regEx != nil && regEx.Match(bExt) {
memoized = mimeType
break
}
}
cache[ext] = memoized
return memoized
}
}
var mimeTypeFromExt = _mimeTyper()
func guessMimeType(p string) string {
resolvedMimeType := mimeTypeFromExt(p)
return resolvedMimeType
}
func CrudAtoi(ops ...string) CrudValue {
opValue := None
for _, op := range ops {
if len(op) < 1 {
continue
}
first := op[0]
if first == 'c' || first == 'C' {
opValue |= Create
} else if first == 'r' || first == 'R' {
opValue |= Read
} else if first == 'u' || first == 'U' {
opValue |= Update
} else if first == 'd' || first == 'D' {
opValue |= Delete
}
}
return opValue
}