This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add progress display handler and api (#1116)
## Description Adds a new package- Observe- for owning user- oriented displays like progress bars. This PR adds an initial progress bar to onedrive backups as a proof-of-concept. The API is more important than the specific progress bar package at this time. Future changes may opt for a different pkg. Display format currently looks like: ``` 59% [=============> ] (6.9/12 kB, 14 MB/s) | Item_Name.txt ``` Known Issues: * the `progressbar` package does not support multiline output, and [the author is not planning to add support](schollz/progressbar#6). This causes concurrent items to overwrite each other. We will either need to fork the library, or change to a different one. ## Type of change - [x] 🌻 Feature ## Issue(s) * #1112 ## Test Plan - [x] 💪 Manual - [x] ⚡ Unit test
- Loading branch information
1 parent
ec916bc
commit 2cc9b7b
Showing
8 changed files
with
127 additions
and
1 deletion.
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
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
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 @@ | ||
package observe | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/schollz/progressbar/v3" | ||
) | ||
|
||
var writer io.Writer | ||
|
||
// SeedWriter adds default writer to the observe package. | ||
// Uses a noop writer until seeded. | ||
func SeedWriter(w io.Writer) { | ||
writer = w | ||
} | ||
|
||
// ItemProgress tracks the display of an item by counting the bytes | ||
// read through the provided readcloser, up until the byte count matches | ||
// the totalBytes. | ||
func ItemProgress(rc io.ReadCloser, iname string, totalBytes int64) io.ReadCloser { | ||
if writer == nil { | ||
return rc | ||
} | ||
|
||
opts := progressbar.NewOptions( | ||
int(totalBytes), | ||
progressbar.OptionSetWriter(writer), | ||
progressbar.OptionClearOnFinish(), | ||
progressbar.OptionSetDescription(" | "+iname), | ||
progressbar.OptionShowDescriptionAtLineEnd(), | ||
progressbar.OptionSetRenderBlankState(true), | ||
progressbar.OptionShowCount(), | ||
progressbar.OptionSetPredictTime(false), | ||
progressbar.OptionEnableColorCodes(true), | ||
progressbar.OptionShowBytes(true), | ||
progressbar.OptionSetWidth(20), | ||
progressbar.OptionSetTheme(progressbar.Theme{ | ||
Saucer: "[green]=[reset]", | ||
SaucerHead: "[green]>[reset]", | ||
SaucerPadding: " ", | ||
BarStart: "[", | ||
BarEnd: "]", | ||
}), | ||
) | ||
|
||
pbr := progressbar.NewReader(rc, opts) | ||
|
||
return &pbr | ||
} |
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,53 @@ | ||
package observe_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alcionai/corso/src/internal/observe" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ObserveProgressUnitSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestObserveProgressUnitSuite(t *testing.T) { | ||
suite.Run(t, new(ObserveProgressUnitSuite)) | ||
} | ||
|
||
func (suite *ObserveProgressUnitSuite) TestDoesThings() { | ||
t := suite.T() | ||
|
||
recorder := strings.Builder{} | ||
observe.SeedWriter(&recorder) | ||
|
||
from := make([]byte, 100) | ||
prog := observe.ItemProgress( | ||
io.NopCloser(bytes.NewReader(from)), | ||
"test", | ||
100) | ||
require.NotNil(t, prog) | ||
|
||
for { | ||
to := make([]byte, 25) | ||
n, err := prog.Read(to) | ||
|
||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Less(t, 0, n) | ||
} | ||
|
||
recorded := recorder.String() | ||
assert.Contains(t, recorded, "25%") | ||
assert.Contains(t, recorded, "50%") | ||
assert.Contains(t, recorded, "75%") | ||
} |