-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.go
More file actions
182 lines (152 loc) · 3.89 KB
/
Copy pathprogress.go
File metadata and controls
182 lines (152 loc) · 3.89 KB
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
package main
import (
"fmt"
"math"
"os"
"regexp"
"strings"
"time"
pbar "github.com/tj/go-progress"
spin "github.com/tj/go-spin"
"golang.org/x/sys/unix"
)
// Progress is tty progress indicator
type Progress interface {
Update(stats TransferStats)
String() string
}
// FiniteProgress represents a progress indicator with a known ending state
type FiniteProgress struct {
progressBar *pbar.Bar
size int64
currentStat TransferStats
}
// Update updates progress with current value
func (p *FiniteProgress) Update(v TransferStats) {
p.currentStat = v
p.progressBar.ValueInt(int(v.transferredBytes))
}
// String returns a tty representation of current progres
func (p *FiniteProgress) String() string {
a := fmt.Sprintf("%s %s", byteCountBinary(p.currentStat.transferredBytes), p.progressBar.String())
return CenterLine(a)
}
// InfiniteProgress represents a progress indicator without a known ending state
type InfiniteProgress struct {
currentStat TransferStats
spinner *spin.Spinner
}
// Update updates progress with current value
func (p *InfiniteProgress) Update(v TransferStats) {
p.currentStat = v
}
// String returns a tty representation of current progres
func (p *InfiniteProgress) String() string {
return fmt.Sprintf("\r \033[36m\033[m %s transfering: %s, %s, %s",
p.spinner.Next(),
byteCountBinary(p.currentStat.transferredBytes),
fmtDuration(p.currentStat.elapsedTime),
fmtAvgSpeed(p.currentStat))
}
func fmtAvgSpeed(tf TransferStats) string {
var speed float64
if tf.elapsedTime > 0 {
speed = float64(tf.transferredBytes) / tf.elapsedTime.Seconds()
}
return fmt.Sprintf("%s/s", byteCountBinary(int64(speed)))
}
func fmtDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
// NewProgress returns a finite progress indicator if totalSize > 0 otherwise, an infinite progress indicator
func NewProgress(totalSize int64) Progress {
var progress Progress
if totalSize < 0 {
b := pbar.NewInt(int(totalSize))
b.Width = getWidth() - 20
progress = &FiniteProgress{
progressBar: b,
size: totalSize,
currentStat: TransferStats{},
}
} else {
progress = &InfiniteProgress{
spinner: spin.New(),
currentStat: TransferStats{},
}
}
return progress
}
func getAverageSpeed(transferredBytes int64, elapsedTime time.Duration) float64 {
if elapsedTime == 0 {
return 0
}
return float64(transferredBytes) / (elapsedTime.Seconds())
}
func byteCountBinary(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func digitsInInt(val int64) int {
return int(math.Floor(math.Log10(float64(val))))
}
func CenterLine(s string) string {
r := strings.Repeat
w := getWidth()
s = "\n" + s
//h := getHeight()
xpad := int(math.Abs(float64((int(w) - Length(s)) / 2)))
ypad := 1 // int(h / 2)
MoveUp(3)
return r("\n", ypad) + r(" ", xpad) + s + r("\n", ypad)
}
func MoveUp(n int) {
fmt.Fprintf(os.Stderr, "\033[%dF", n)
}
func getWinsize() (*unix.Winsize, error) {
ws, err := unix.IoctlGetWinsize(int(os.Stderr.Fd()), unix.TIOCGWINSZ)
if err != nil {
return nil, os.NewSyscallError("GetWinsize", err)
}
return ws, nil
}
func getWidth() int {
size, err := getWinsize()
if err != nil {
return -1
}
return int(size.Col)
}
func getHeight() int {
size, err := getWinsize()
if err != nil {
return -1
}
return int(size.Row)
}
// strip regexp.
var strip = regexp.MustCompile(`\x1B\[[0-?]*[ -/]*[@-~]`)
func Strip(s string) string {
return strip.ReplaceAllString(s, "")
}
// Length of characters with ansi escape sequences stripped.
func Length(s string) (n int) {
for range Strip(s) {
n++
}
return
}