-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ux): optimize progress bar (#1349)
Signed-off-by: Billy Zha <[email protected]>
- Loading branch information
Showing
7 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package progress | ||
|
||
import "time" | ||
|
||
type speedPoint struct { | ||
time time.Time | ||
offset int64 | ||
} | ||
|
||
type speedWindow struct { | ||
point []speedPoint | ||
next int | ||
size int | ||
} | ||
|
||
// newSpeedWindow creates a new speed window with a given capacity. | ||
func newSpeedWindow(capacity int) *speedWindow { | ||
return &speedWindow{ | ||
point: make([]speedPoint, capacity), | ||
} | ||
} | ||
|
||
// Add adds a done workload to the window. | ||
func (w *speedWindow) Add(time time.Time, offset int64) { | ||
if w.size != len(w.point) { | ||
w.size++ | ||
} | ||
w.point[w.next] = speedPoint{ | ||
time: time, | ||
offset: offset, | ||
} | ||
w.next = (w.next + 1) % len(w.point) | ||
} | ||
|
||
// Mean returns the mean speed of the window with unit of byte per second. | ||
func (w *speedWindow) Mean() float64 { | ||
if w.size < 2 { | ||
// no speed diplayed for first read | ||
return 0 | ||
} | ||
|
||
begin := (w.next - w.size + len(w.point)) % len(w.point) | ||
end := (begin - 1 + w.size) % w.size | ||
|
||
return float64(w.point[end].offset-w.point[begin].offset) / w.point[end].time.Sub(w.point[begin].time).Seconds() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package progress | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_speedWindow(t *testing.T) { | ||
w := newSpeedWindow(3) | ||
if s := w.Mean(); s != 0 { | ||
t.Errorf("expected 0, got %f", s) | ||
} | ||
|
||
now := time.Now() | ||
w.Add(now, 100) | ||
if s := w.Mean(); s != 0 { | ||
t.Errorf("expected 0, got %f", s) | ||
} | ||
|
||
w.Add(now.Add(1*time.Second), 200) | ||
if s := w.Mean(); s != 100 { | ||
t.Errorf("expected 100, got %f", s) | ||
} | ||
|
||
w.Add(now.Add(4*time.Second), 900) | ||
if s := w.Mean(); s != 200 { | ||
t.Errorf("expected 200, got %f", s) | ||
} | ||
|
||
w.Add(now.Add(5*time.Second), 1400) | ||
if s := w.Mean(); s != 300 { | ||
t.Errorf("expected 300, got %f", s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters