|
1 |
| -/* |
2 |
| -Copyright The ORAS Authors. |
3 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -you may not use this file except in compliance with the License. |
5 |
| -You may obtain a copy of the License at |
6 |
| -
|
7 |
| -http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -
|
9 |
| -Unless required by applicable law or agreed to in writing, software |
10 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -See the License for the specific language governing permissions and |
13 |
| -limitations under the License. |
14 |
| -*/ |
15 |
| - |
16 |
| -package progress_test |
17 |
| - |
18 |
| -import ( |
19 |
| - "crypto/rand" |
20 |
| - "fmt" |
21 |
| - "io" |
22 |
| - |
23 |
| - "oras.land/oras-go/v2/progress" |
24 |
| -) |
25 |
| - |
26 |
| -// ExampleTrackReader demonstrates how to track the transmission progress of a |
27 |
| -// reader. |
28 |
| -func ExampleTrackReader() { |
29 |
| - // Set up a progress tracker. |
30 |
| - total := int64(11) |
31 |
| - tracker := progress.TrackerFunc(func(status progress.Status, err error) error { |
32 |
| - if err != nil { |
33 |
| - fmt.Printf("Error: %v\n", err) |
34 |
| - return nil |
35 |
| - } |
36 |
| - switch status.State { |
37 |
| - case progress.StateInitialized: |
38 |
| - fmt.Println("Start reading content") |
39 |
| - case progress.StateTransmitting: |
40 |
| - fmt.Printf("Progress: %d/%d bytes\n", status.Offset, total) |
41 |
| - case progress.StateTransmitted: |
42 |
| - fmt.Println("Finish reading content") |
43 |
| - default: |
44 |
| - // Ignore other states. |
45 |
| - } |
46 |
| - return nil |
47 |
| - }) |
48 |
| - // Close takes no effect for TrackerFunc but should be called for general |
49 |
| - // Tracker implementations. |
50 |
| - defer tracker.Close() |
51 |
| - |
52 |
| - // Wrap a reader of a random content generator with the progress tracker. |
53 |
| - r := io.LimitReader(rand.Reader, total) |
54 |
| - rc := progress.TrackReader(tracker, r) |
55 |
| - |
56 |
| - // Start tracking the transmission. |
57 |
| - if err := progress.Start(tracker); err != nil { |
58 |
| - panic(err) |
59 |
| - } |
60 |
| - |
61 |
| - // Read from the random content generator and discard the content, while |
62 |
| - // tracking the progress. |
63 |
| - // Note: io.Discard is wrapped with a io.MultiWriter for dropping |
64 |
| - // the io.ReadFrom interface for demonstration purposes. |
65 |
| - buf := make([]byte, 3) |
66 |
| - w := io.MultiWriter(io.Discard) |
67 |
| - if _, err := io.CopyBuffer(w, rc, buf); err != nil { |
68 |
| - panic(err) |
69 |
| - } |
70 |
| - |
71 |
| - // Finish tracking the transmission. |
72 |
| - if err := progress.Done(tracker); err != nil { |
73 |
| - panic(err) |
74 |
| - } |
75 |
| - |
76 |
| - // Output: |
77 |
| - // Start reading content |
78 |
| - // Progress: 3/11 bytes |
79 |
| - // Progress: 6/11 bytes |
80 |
| - // Progress: 9/11 bytes |
81 |
| - // Progress: 11/11 bytes |
82 |
| - // Finish reading content |
83 |
| -} |
| 1 | +/* |
| 2 | +Copyright The ORAS Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package progress_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto/rand" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + "oras.land/oras-go/v2/progress" |
| 24 | +) |
| 25 | + |
| 26 | +// ExampleTrackReader demonstrates how to track the transmission progress of a |
| 27 | +// reader. |
| 28 | +func ExampleTrackReader() { |
| 29 | + // Set up a progress tracker. |
| 30 | + total := int64(11) |
| 31 | + tracker := progress.TrackerFunc(func(status progress.Status, err error) error { |
| 32 | + if err != nil { |
| 33 | + fmt.Printf("Error: %v\n", err) |
| 34 | + return nil |
| 35 | + } |
| 36 | + switch status.State { |
| 37 | + case progress.StateInitialized: |
| 38 | + fmt.Println("Start reading content") |
| 39 | + case progress.StateTransmitting: |
| 40 | + fmt.Printf("Progress: %d/%d bytes\n", status.Offset, total) |
| 41 | + case progress.StateTransmitted: |
| 42 | + fmt.Println("Finish reading content") |
| 43 | + default: |
| 44 | + // Ignore other states. |
| 45 | + } |
| 46 | + return nil |
| 47 | + }) |
| 48 | + // Close takes no effect for TrackerFunc but should be called for general |
| 49 | + // Tracker implementations. |
| 50 | + defer tracker.Close() |
| 51 | + |
| 52 | + // Wrap a reader of a random content generator with the progress tracker. |
| 53 | + r := io.LimitReader(rand.Reader, total) |
| 54 | + rc := progress.TrackReader(tracker, r) |
| 55 | + |
| 56 | + // Start tracking the transmission. |
| 57 | + if err := progress.Start(tracker); err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + |
| 61 | + // Read from the random content generator and discard the content, while |
| 62 | + // tracking the progress. |
| 63 | + // Note: io.Discard is wrapped with a io.MultiWriter for dropping |
| 64 | + // the io.ReadFrom interface for demonstration purposes. |
| 65 | + buf := make([]byte, 3) |
| 66 | + w := io.MultiWriter(io.Discard) |
| 67 | + if _, err := io.CopyBuffer(w, rc, buf); err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + |
| 71 | + // Finish tracking the transmission. |
| 72 | + if err := progress.Done(tracker); err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + // Output: |
| 77 | + // Start reading content |
| 78 | + // Progress: 3/11 bytes |
| 79 | + // Progress: 6/11 bytes |
| 80 | + // Progress: 9/11 bytes |
| 81 | + // Progress: 11/11 bytes |
| 82 | + // Finish reading content |
| 83 | +} |
0 commit comments