Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
b173cfc5
Unverified
Commit
b173cfc5
authored
Nov 28, 2023
by
Michael Yang
Committed by
GitHub
Nov 28, 2023
Browse files
Merge pull request #1195 from jmorganca/mxyng/fix-bar-rate
progress: fix bar rate
parents
e1a69d44
424d53ac
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
82 deletions
+126
-82
format/bytes.go
format/bytes.go
+2
-0
progress/bar.go
progress/bar.go
+122
-80
progress/spinner.go
progress/spinner.go
+2
-2
No files found.
format/bytes.go
View file @
b173cfc5
...
@@ -37,6 +37,8 @@ func HumanBytes(b int64) string {
...
@@ -37,6 +37,8 @@ func HumanBytes(b int64) string {
switch
{
switch
{
case
value
>=
100
:
case
value
>=
100
:
return
fmt
.
Sprintf
(
"%d %s"
,
int
(
value
),
unit
)
return
fmt
.
Sprintf
(
"%d %s"
,
int
(
value
),
unit
)
case
value
>=
10
:
return
fmt
.
Sprintf
(
"%d %s"
,
int
(
value
),
unit
)
case
value
!=
math
.
Trunc
(
value
)
:
case
value
!=
math
.
Trunc
(
value
)
:
return
fmt
.
Sprintf
(
"%.1f %s"
,
value
,
unit
)
return
fmt
.
Sprintf
(
"%.1f %s"
,
value
,
unit
)
default
:
default
:
...
...
progress/bar.go
View file @
b173cfc5
...
@@ -2,7 +2,6 @@ package progress
...
@@ -2,7 +2,6 @@ package progress
import
(
import
(
"fmt"
"fmt"
"math"
"os"
"os"
"strings"
"strings"
"time"
"time"
...
@@ -11,12 +10,6 @@ import (
...
@@ -11,12 +10,6 @@ import (
"golang.org/x/term"
"golang.org/x/term"
)
)
type
Stats
struct
{
rate
int64
value
int64
remaining
time
.
Duration
}
type
Bar
struct
{
type
Bar
struct
{
message
string
message
string
messageWidth
int
messageWidth
int
...
@@ -26,33 +19,45 @@ type Bar struct {
...
@@ -26,33 +19,45 @@ type Bar struct {
currentValue
int64
currentValue
int64
started
time
.
Time
started
time
.
Time
stopped
time
.
Time
maxBuckets
int
buckets
[]
bucket
}
stats
Stats
type
bucket
struct
{
statted
time
.
Time
updated
time
.
Time
value
int64
}
}
func
NewBar
(
message
string
,
maxValue
,
initialValue
int64
)
*
Bar
{
func
NewBar
(
message
string
,
maxValue
,
initialValue
int64
)
*
Bar
{
return
&
Bar
{
b
:=
Bar
{
message
:
message
,
message
:
message
,
messageWidth
:
-
1
,
messageWidth
:
-
1
,
maxValue
:
maxValue
,
maxValue
:
maxValue
,
initialValue
:
initialValue
,
initialValue
:
initialValue
,
currentValue
:
initialValue
,
currentValue
:
initialValue
,
started
:
time
.
Now
(),
started
:
time
.
Now
(),
maxBuckets
:
10
,
}
if
initialValue
>=
maxValue
{
b
.
stopped
=
time
.
Now
()
}
}
return
&
b
}
}
// formatDuration limits the rendering of a time.Duration to 2 units
// formatDuration limits the rendering of a time.Duration to 2 units
func
formatDuration
(
d
time
.
Duration
)
string
{
func
formatDuration
(
d
time
.
Duration
)
string
{
if
d
>=
100
*
time
.
Hour
{
switch
{
case
d
>=
100
*
time
.
Hour
:
return
"99h+"
return
"99h+"
}
case
d
>=
time
.
Hour
:
if
d
>=
time
.
Hour
{
return
fmt
.
Sprintf
(
"%dh%dm"
,
int
(
d
.
Hours
()),
int
(
d
.
Minutes
())
%
60
)
return
fmt
.
Sprintf
(
"%dh%dm"
,
int
(
d
.
Hours
()),
int
(
d
.
Minutes
())
%
60
)
default
:
return
d
.
Round
(
time
.
Second
)
.
String
()
}
}
return
d
.
Round
(
time
.
Second
)
.
String
()
}
}
func
(
b
*
Bar
)
String
()
string
{
func
(
b
*
Bar
)
String
()
string
{
...
@@ -61,59 +66,85 @@ func (b *Bar) String() string {
...
@@ -61,59 +66,85 @@ func (b *Bar) String() string {
termWidth
=
80
termWidth
=
80
}
}
var
pre
,
mid
,
suf
strings
.
Builder
var
pre
strings
.
Builder
if
len
(
b
.
message
)
>
0
{
if
b
.
message
!=
""
{
message
:=
strings
.
TrimSpace
(
b
.
message
)
message
:=
strings
.
TrimSpace
(
b
.
message
)
if
b
.
messageWidth
>
0
&&
len
(
message
)
>
b
.
messageWidth
{
if
b
.
messageWidth
>
0
&&
len
(
message
)
>
b
.
messageWidth
{
message
=
message
[
:
b
.
messageWidth
]
message
=
message
[
:
b
.
messageWidth
]
}
}
fmt
.
Fprintf
(
&
pre
,
"%s"
,
message
)
fmt
.
Fprintf
(
&
pre
,
"%s"
,
message
)
if
b
.
messageWidth
-
pre
.
Len
()
>
=
0
{
if
padding
:=
b
.
messageWidth
-
pre
.
Len
()
;
padding
>
0
{
pre
.
WriteString
(
strings
.
R
epeat
(
" "
,
b
.
messageWidth
-
pre
.
Len
()
))
pre
.
WriteString
(
r
epeat
(
" "
,
padding
))
}
}
pre
.
WriteString
(
" "
)
pre
.
WriteString
(
" "
)
}
}
fmt
.
Fprintf
(
&
pre
,
"%3.0f%% "
,
math
.
Floor
(
b
.
percent
()))
fmt
.
Fprintf
(
&
pre
,
"%3.0f%%"
,
b
.
percent
())
fmt
.
Fprintf
(
&
suf
,
"(%s/%s"
,
format
.
HumanBytes
(
b
.
currentValue
),
format
.
HumanBytes
(
b
.
maxValue
))
var
suf
strings
.
Builder
// max 13 characters: "999 MB/999 MB"
stats
:=
b
.
Stats
()
if
b
.
stopped
.
IsZero
()
{
rate
:=
stats
.
rate
curValue
:=
format
.
HumanBytes
(
b
.
currentValue
)
if
stats
.
value
>
b
.
initialValue
&&
stats
.
value
<
b
.
maxValue
{
suf
.
WriteString
(
repeat
(
" "
,
6
-
len
(
curValue
)))
fmt
.
Fprintf
(
&
suf
,
", %s/s"
,
format
.
HumanBytes
(
int64
(
rate
)))
suf
.
WriteString
(
curValue
)
suf
.
WriteString
(
"/"
)
maxValue
:=
format
.
HumanBytes
(
b
.
maxValue
)
suf
.
WriteString
(
repeat
(
" "
,
6
-
len
(
maxValue
)))
suf
.
WriteString
(
maxValue
)
}
else
{
maxValue
:=
format
.
HumanBytes
(
b
.
maxValue
)
suf
.
WriteString
(
repeat
(
" "
,
6
-
len
(
maxValue
)))
suf
.
WriteString
(
maxValue
)
suf
.
WriteString
(
repeat
(
" "
,
7
))
}
}
fmt
.
Fprintf
(
&
suf
,
")"
)
rate
:=
b
.
rate
()
// max 10 characters: " 999 MB/s"
var
timing
string
if
b
.
stopped
.
IsZero
()
&&
rate
>
0
{
if
stats
.
value
>
b
.
initialValue
&&
stats
.
value
<
b
.
maxValue
{
suf
.
WriteString
(
" "
)
timing
=
fmt
.
Sprintf
(
"[%s:%s]"
,
formatDuration
(
time
.
Since
(
b
.
started
)),
formatDuration
(
stats
.
remaining
))
humanRate
:=
format
.
HumanBytes
(
int64
(
rate
))
suf
.
WriteString
(
repeat
(
" "
,
6
-
len
(
humanRate
)))
suf
.
WriteString
(
humanRate
)
suf
.
WriteString
(
"/s"
)
}
else
{
suf
.
WriteString
(
repeat
(
" "
,
10
))
}
}
// 44 is the maximum width for the stats on the right of the progress bar
// max 8 characters: " 59m59s"
pad
:=
44
-
suf
.
Len
()
-
len
(
timing
)
if
b
.
stopped
.
IsZero
()
&&
rate
>
0
{
if
pad
>
0
{
suf
.
WriteString
(
" "
)
suf
.
WriteString
(
strings
.
Repeat
(
" "
,
pad
))
var
remaining
time
.
Duration
if
rate
>
0
{
remaining
=
time
.
Duration
(
int64
(
float64
(
b
.
maxValue
-
b
.
currentValue
)
/
rate
))
*
time
.
Second
}
humanRemaining
:=
formatDuration
(
remaining
)
suf
.
WriteString
(
repeat
(
" "
,
6
-
len
(
humanRemaining
)))
suf
.
WriteString
(
humanRemaining
)
}
else
{
suf
.
WriteString
(
repeat
(
" "
,
8
))
}
}
suf
.
WriteString
(
timing
)
// add 3 extra spaces: 2 boundary characters and 1 space at the end
var
mid
strings
.
Builder
f
:=
termWidth
-
pre
.
Len
()
-
suf
.
Len
()
-
3
// add 5 extra spaces: 2 boundary characters and 1 space at each end
f
:=
termWidth
-
pre
.
Len
()
-
suf
.
Len
()
-
5
n
:=
int
(
float64
(
f
)
*
b
.
percent
()
/
100
)
n
:=
int
(
float64
(
f
)
*
b
.
percent
()
/
100
)
if
f
>
0
{
mid
.
WriteString
(
" ▕"
)
mid
.
WriteString
(
"▕"
)
mid
.
WriteString
(
strings
.
Repeat
(
"█"
,
n
))
if
n
>
0
{
if
f
-
n
>
0
{
mid
.
WriteString
(
repeat
(
"█"
,
n
))
mid
.
WriteString
(
strings
.
Repeat
(
" "
,
f
-
n
))
}
mid
.
WriteString
(
"▏"
)
}
}
if
f
-
n
>
0
{
mid
.
WriteString
(
repeat
(
" "
,
f
-
n
))
}
mid
.
WriteString
(
"▏ "
)
return
pre
.
String
()
+
mid
.
String
()
+
suf
.
String
()
return
pre
.
String
()
+
mid
.
String
()
+
suf
.
String
()
}
}
...
@@ -123,6 +154,21 @@ func (b *Bar) Set(value int64) {
...
@@ -123,6 +154,21 @@ func (b *Bar) Set(value int64) {
}
}
b
.
currentValue
=
value
b
.
currentValue
=
value
if
b
.
currentValue
>=
b
.
maxValue
{
b
.
stopped
=
time
.
Now
()
}
// throttle bucket updates to 1 per second
if
len
(
b
.
buckets
)
==
0
||
time
.
Since
(
b
.
buckets
[
len
(
b
.
buckets
)
-
1
]
.
updated
)
>
time
.
Second
{
b
.
buckets
=
append
(
b
.
buckets
,
bucket
{
updated
:
time
.
Now
(),
value
:
value
,
})
if
len
(
b
.
buckets
)
>
b
.
maxBuckets
{
b
.
buckets
=
b
.
buckets
[
1
:
]
}
}
}
}
func
(
b
*
Bar
)
percent
()
float64
{
func
(
b
*
Bar
)
percent
()
float64
{
...
@@ -133,41 +179,37 @@ func (b *Bar) percent() float64 {
...
@@ -133,41 +179,37 @@ func (b *Bar) percent() float64 {
return
0
return
0
}
}
func
(
b
*
Bar
)
Stats
()
Stats
{
func
(
b
*
Bar
)
rate
()
float64
{
if
time
.
Since
(
b
.
statted
)
<
time
.
Second
{
var
numerator
,
denominator
float64
return
b
.
stats
}
if
!
b
.
stopped
.
IsZero
()
{
numerator
=
float64
(
b
.
currentValue
-
b
.
initialValue
)
switch
{
denominator
=
b
.
stopped
.
Sub
(
b
.
started
)
.
Round
(
time
.
Second
)
.
Seconds
()
case
b
.
statted
.
IsZero
()
:
}
else
{
b
.
stats
=
Stats
{
switch
len
(
b
.
buckets
)
{
value
:
b
.
initialValue
,
case
0
:
rate
:
0
,
// noop
remaining
:
0
,
case
1
:
}
numerator
=
float64
(
b
.
buckets
[
0
]
.
value
-
b
.
initialValue
)
case
b
.
currentValue
>=
b
.
maxValue
:
denominator
=
b
.
buckets
[
0
]
.
updated
.
Sub
(
b
.
started
)
.
Round
(
time
.
Second
)
.
Seconds
()
b
.
stats
=
Stats
{
default
:
value
:
b
.
maxValue
,
first
,
last
:=
b
.
buckets
[
0
],
b
.
buckets
[
len
(
b
.
buckets
)
-
1
]
rate
:
0
,
numerator
=
float64
(
last
.
value
-
first
.
value
)
remaining
:
0
,
denominator
=
last
.
updated
.
Sub
(
first
.
updated
)
.
Round
(
time
.
Second
)
.
Seconds
()
}
default
:
rate
:=
b
.
currentValue
-
b
.
stats
.
value
var
remaining
time
.
Duration
if
rate
>
0
{
remaining
=
time
.
Second
*
time
.
Duration
((
float64
(
b
.
maxValue
-
b
.
currentValue
))
/
(
float64
(
rate
)))
}
else
{
remaining
=
time
.
Duration
(
math
.
MaxInt64
)
}
}
}
b
.
stats
=
Stats
{
if
denominator
!=
0
{
value
:
b
.
currentValue
,
return
numerator
/
denominator
rate
:
rate
,
remaining
:
remaining
,
}
}
}
b
.
statted
=
time
.
Now
()
return
0
}
func
repeat
(
s
string
,
n
int
)
string
{
if
n
>
0
{
return
strings
.
Repeat
(
s
,
n
)
}
return
b
.
stats
return
""
}
}
progress/spinner.go
View file @
b173cfc5
...
@@ -40,8 +40,8 @@ func (s *Spinner) String() string {
...
@@ -40,8 +40,8 @@ func (s *Spinner) String() string {
}
}
fmt
.
Fprintf
(
&
sb
,
"%s"
,
message
)
fmt
.
Fprintf
(
&
sb
,
"%s"
,
message
)
if
s
.
messageWidth
-
sb
.
Len
()
>
=
0
{
if
padding
:=
s
.
messageWidth
-
sb
.
Len
()
;
padding
>
0
{
sb
.
WriteString
(
strings
.
Repeat
(
" "
,
s
.
messageWidth
-
sb
.
Len
()
))
sb
.
WriteString
(
strings
.
Repeat
(
" "
,
padding
))
}
}
sb
.
WriteString
(
" "
)
sb
.
WriteString
(
" "
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment