-
Notifications
You must be signed in to change notification settings - Fork 1
/
default_decorators.go
172 lines (141 loc) · 4.25 KB
/
default_decorators.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
package gogress
import (
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/snakeice/gogress/format"
)
func bar(frame *FrameContext, cols int) string {
var bar string
cols -= len(frame.Format().BoxStart) + len(frame.Format().BoxEnd)
switch {
case cols <= 0:
bar = ""
case frame.Max > 0:
decPercent := float64(frame.Current) / float64(frame.Max)
doneSize := int(math.Ceil(decPercent * float64(cols)))
emptySize := cols - doneSize
if emptySize < 0 {
emptySize = 0
}
if doneSize > cols {
doneSize = cols
}
bar += frame.Format().BoxStart
cursorLen := format.EscapeAwareRuneCountInString(frame.Format().Completed)
if emptySize <= 0 {
bar += strings.Repeat(frame.Format().Completed, doneSize/cursorLen)
} else if doneSize > 0 {
cursorEndLen := format.EscapeAwareRuneCountInString(frame.Format().Completed)
cursorRepetitions := (doneSize - cursorEndLen) / cursorLen
bar += strings.Repeat(frame.Format().Completed, cursorRepetitions)
bar += frame.Format().Current
}
emptyLen := format.EscapeAwareRuneCountInString(frame.Format().Empty)
bar += strings.Repeat(frame.Format().Empty, emptySize/emptyLen)
bar += frame.Format().BoxEnd
default:
pos := cols - int(frame.Current)%int(cols)
bar += frame.Format().BoxStart
if pos-1 > 0 {
bar += strings.Repeat(frame.Format().Empty, pos-1)
}
bar += frame.Format().Current
if cols-pos-1 > 0 {
bar += strings.Repeat(frame.Format().Empty, cols-pos-1)
}
bar += frame.Format().BoxEnd
}
return bar
}
func prefix(frame *FrameContext, cols int) string {
msg := frame.MessagePrefix
if cols <= 3 {
msg = strings.Repeat(".", cols)
} else if len(msg) > cols {
msg = msg[:cols-3] + "..."
}
// else if len(msg) < cols {
// //msg += strings.Repeat(" ", cols-len(msg))
// }
return msg
}
func counter(frame *FrameContext, cols int) string {
var counterBox string
current := format.Format(frame.Current).To(frame.bar.Units).Width(frame.bar.UnitsWidth)
if frame.Max > 0 {
totalS := format.Format(frame.Max).To(frame.bar.Units).Width(frame.bar.UnitsWidth)
counterBox = fmt.Sprintf("%s/%s", current, totalS)
} else {
counterBox = fmt.Sprintf("%s/?", current)
}
return counterBox
}
func timeSpent(frame *FrameContext, cols int) string {
var left time.Duration
var timeSpentBox string
var fromStart time.Duration
if frame.IsFinish {
fromStart = frame.bar.finishedTime.Sub(frame.bar.startTime)
left = (fromStart / time.Second) * time.Second
timeSpentBox = left.String()
} else {
fromStart = time.Since(frame.bar.startTime)
timeSpentBox = ((fromStart / time.Second) * time.Second).String()
}
return timeSpentBox
}
func speed(frame *FrameContext, cols int) string {
var fromStart time.Duration
if frame.IsFinish {
fromStart = frame.bar.finishedTime.Sub(frame.bar.startTime)
} else {
fromStart = time.Since(frame.bar.startTime)
}
currentFromStart := frame.Current - frame.bar.startValue
var speedBox string
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
speedBox = format.Format(int64(speed)).To(frame.bar.Units).Width(frame.bar.UnitsWidth).PerSec().String()
return speedBox
}
func percent(frame *FrameContext, cols int) string {
percent := float64(frame.Current) / (float64(frame.Max) / 100.0)
return fmt.Sprintf("%.02f%%", percent)
}
func timeLeft(frame *FrameContext, cols int) string {
currentFromStart := frame.Current - frame.bar.startValue
lastChangeTime := frame.bar.changeTime
fromChange := lastChangeTime.Sub(frame.bar.startTime)
var left time.Duration
var timeLeftBox string
select {
case <-frame.bar.ctx.Done():
default:
if currentFromStart > 0 {
perEntry := fromChange / time.Duration(currentFromStart)
if frame.Max > 0 {
left = time.Duration(frame.Max-frame.Current) * perEntry
left -= time.Since(lastChangeTime)
left = (left / time.Second) * time.Second
}
if left > 0 {
timeLeftBox = format.Format(int64(left)).To(format.U_DURATION).String()
}
}
}
return timeLeftBox
}
func spin(frame *FrameContext, cols int) string {
spin := frame.Format().Spin
spinLen := len(spin)
if spinLen == 0 {
return " "
}
part := frame.frameNo % int64(spinLen)
return string(spin[part])
}
func frameNo(frame *FrameContext, cols int) string {
return strconv.FormatInt(frame.FrameNo(), 10)
}