/
progress-bar.go
173 lines (145 loc) · 4.42 KB
/
progress-bar.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"io"
"runtime"
"strings"
"time"
"github.com/cheggaaa/pb"
"github.com/fatih/color"
"github.com/minio/pkg/v2/console"
)
// progress extender.
type progressBar struct {
*pb.ProgressBar
}
func newPB(total int64) *pb.ProgressBar {
// Progress bar specific theme customization.
console.SetColor("Bar", color.New(color.FgGreen, color.Bold))
// get the new original progress bar.
bar := pb.New64(total)
// Set new human friendly print units.
bar.SetUnits(pb.U_BYTES)
// Refresh rate for progress bar is set to 125 milliseconds.
bar.SetRefreshRate(time.Millisecond * 125)
// Do not print a newline by default handled, it is handled manually.
bar.NotPrint = true
// Show current speed is true.
bar.ShowSpeed = true
// Custom callback with colorized bar.
bar.Callback = func(s string) {
console.Print(console.Colorize("Bar", "\r"+s))
}
// Use different unicodes for Linux, OS X and Windows.
switch runtime.GOOS {
case "linux":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format("┃\x00▓\x00█\x00░\x00┃")
case "darwin":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format(" \x00▓\x00 \x00░\x00 ")
default:
// Default to non unicode characters.
bar.Format("[=> ]")
}
// Start the progress bar.
return bar.Start()
}
func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader {
bar := newPB(total)
if caption != "" {
bar.Prefix(caption)
}
return bar.NewProxyReader(r)
}
// newProgressBar - instantiate a progress bar.
func newProgressBar(total int64) *progressBar {
bar := newPB(total)
// Return new progress bar here.
return &progressBar{ProgressBar: bar}
}
// Set caption.
func (p *progressBar) SetCaption(caption string) *progressBar {
caption = fixateBarCaption(caption, getFixedWidth(p.ProgressBar.GetWidth(), 18))
p.ProgressBar.Prefix(caption)
return p
}
func (p *progressBar) Finish() {
p.ProgressBar.Finish()
}
func (p *progressBar) Set64(length int64) *progressBar {
p.ProgressBar = p.ProgressBar.Set64(length)
return p
}
func (p *progressBar) Read(buf []byte) (n int, err error) {
defer func() {
// Upload retry can read one object twice; Avoid read to be greater than Total
if n, t := p.ProgressBar.Get(), p.ProgressBar.Total; t > 0 && n > t {
p.ProgressBar.Set64(t)
}
}()
return p.ProgressBar.Read(buf)
}
func (p *progressBar) SetTotal(total int64) {
p.ProgressBar.Total = total
}
// cursorAnimate - returns a animated rune through read channel for every read.
func cursorAnimate() <-chan string {
cursorCh := make(chan string)
var cursors []string
switch runtime.GOOS {
case "linux":
// cursors = "➩➪➫➬➭➮➯➱"
// cursors = "▁▃▄▅▆▇█▇▆▅▄▃"
cursors = []string{"◐", "◓", "◑", "◒"}
// cursors = "←↖↑↗→↘↓↙"
// cursors = "◴◷◶◵"
// cursors = "◰◳◲◱"
// cursors = "⣾⣽⣻⢿⡿⣟⣯⣷"
case "darwin":
cursors = []string{"◐", "◓", "◑", "◒"}
default:
cursors = []string{"|", "/", "-", "\\"}
}
go func() {
for {
for _, cursor := range cursors {
cursorCh <- cursor
}
}
}()
return cursorCh
}
// fixateBarCaption - fancify bar caption based on the terminal width.
func fixateBarCaption(caption string, width int) string {
switch {
case len(caption) > width:
// Trim caption to fit within the screen
trimSize := len(caption) - width + 3
if trimSize < len(caption) {
caption = "..." + caption[trimSize:]
}
case len(caption) < width:
caption += strings.Repeat(" ", width-len(caption))
}
return caption
}
// getFixedWidth - get a fixed width based for a given percentage.
func getFixedWidth(width, percent int) int {
return width * percent / 100
}