/
util.go
141 lines (130 loc) · 3.25 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
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
package cli
import (
"fmt"
"os"
"os/exec"
"github.com/ipfs/go-cid"
"github.com/manifoldco/promptui"
"github.com/textileio/textile/buckets/local"
"github.com/textileio/textile/cmd"
"github.com/textileio/uiprogress"
)
func getConfirm(label string, auto bool) local.ConfirmDiffFunc {
return func(diff []local.Change) bool {
if auto {
return true
}
for _, c := range diff {
cf := local.ChangeColor(c.Type)
cmd.Message("%s %s", cf(local.ChangeType(c.Type)), cf(c.Rel))
}
prompt := promptui.Prompt{
Label: fmt.Sprintf(label, len(diff)),
IsConfirm: true,
}
if _, err := prompt.Run(); err != nil {
return false
}
return true
}
}
func handleProgressBars(p *uiprogress.Progress, events chan local.PathEvent) {
bars := make(map[string]*uiprogress.Bar)
for e := range events {
switch e.Type {
case local.FileStart:
bars[e.Path] = addBar(p, e.Path, e.Size)
case local.FileProgress, local.FileComplete:
bar, ok := bars[e.Path]
if ok {
_ = bar.Set(int(e.Progress))
if e.Type == local.FileComplete {
finishBar(p, bar, e.Path, e.Cid, false)
delete(bars, e.Path)
}
}
case local.FileRemoved:
bar := p.AddBar(int(e.Size))
finishBar(p, bar, e.Path, e.Cid, true)
}
}
}
func addBar(p *uiprogress.Progress, pth string, size int64) *uiprogress.Bar {
bar := p.AddBar(int(size)).AppendCompleted()
pre := "+ " + pth + ":"
total := formatBytes(size, true)
setBarWidth(bar, pre, total, 9)
bar.PrependFunc(func(b *uiprogress.Bar) string {
c := formatBytes(int64(b.Current()), true)
return pre + " " + c + " / " + total
})
return bar
}
func finishBar(p *uiprogress.Progress, bar *uiprogress.Bar, pth string, c cid.Cid, removal bool) {
if removal {
bar.Final = "- " + pth
} else {
bar.Final = "+ " + pth + ": " + c.String()
}
p.Print()
}
func setBarWidth(bar *uiprogress.Bar, pre, size string, of int) {
tw, _ := getTermDim()
w := tw - len(pre) - (2*len(size) + 3) - of // Make space for overflow chars
if w > 0 {
bar.Width = w
} else {
bar.Width = 10
}
}
func getTermDim() (w, h int) {
c := exec.Command("stty", "size")
c.Stdin = os.Stdin
termDim, err := c.Output()
cmd.ErrCheck(err)
_, err = fmt.Sscan(string(termDim), &h, &w)
cmd.ErrCheck(err)
return w, h
}
// Copied from https://github.com/cheggaaa/pb/blob/master/v3/util.go
const (
_KiB = 1024
_MiB = 1048576
_GiB = 1073741824
_TiB = 1099511627776
_kB = 1e3
_MB = 1e6
_GB = 1e9
_TB = 1e12
)
// Copied from https://github.com/cheggaaa/pb/blob/master/v3/util.go
func formatBytes(i int64, useSIPrefix bool) (result string) {
if !useSIPrefix {
switch {
case i >= _TiB:
result = fmt.Sprintf("%.02f TiB", float64(i)/_TiB)
case i >= _GiB:
result = fmt.Sprintf("%.02f GiB", float64(i)/_GiB)
case i >= _MiB:
result = fmt.Sprintf("%.02f MiB", float64(i)/_MiB)
case i >= _KiB:
result = fmt.Sprintf("%.02f KiB", float64(i)/_KiB)
default:
result = fmt.Sprintf("%d B", i)
}
} else {
switch {
case i >= _TB:
result = fmt.Sprintf("%.02f TB", float64(i)/_TB)
case i >= _GB:
result = fmt.Sprintf("%.02f GB", float64(i)/_GB)
case i >= _MB:
result = fmt.Sprintf("%.02f MB", float64(i)/_MB)
case i >= _kB:
result = fmt.Sprintf("%.02f kB", float64(i)/_kB)
default:
result = fmt.Sprintf("%d B", i)
}
}
return
}