Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ progressR := &ioprogress.Reader{
// customizable.
io.Copy(f, progressR)
```

See [example.go](./example.go)
73 changes: 73 additions & 0 deletions example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
"time"

"github.com/mitchellh/ioprogress"
)

type FakeReader struct {
Length int
Step int
hasRead int
}

func (f *FakeReader) Read(p []byte) (int, error) {
if f.hasRead >= f.Length {
return 0, io.EOF
}
// Sleep for a while so we can see the animation.
<-time.After(time.Second)
f.hasRead += f.Step
return f.Step, nil
}

func (f *FakeReader) Size() int {
return f.Length
}

func drawDefault() {
r := &FakeReader{Length: 100, Step: 10}
progressR := &ioprogress.Reader{
Reader: r,
Size: int64(r.Size()),
}
var buff bytes.Buffer
io.Copy(&buff, progressR)
}

func drawBar() {
r := &FakeReader{Length: 100, Step: 10}
bar := ioprogress.DrawTextFormatBar(20)
progressR := &ioprogress.Reader{
Reader: r,
Size: int64(r.Size()),
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, bar),
}
var buff bytes.Buffer
io.Copy(&buff, progressR)
}

func drawBytes() {
r := &FakeReader{Length: 100, Step: 10}
progressR := &ioprogress.Reader{
Reader: r,
Size: int64(r.Size()),
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, ioprogress.DrawTextFormatBytes),
}
var buff bytes.Buffer
io.Copy(&buff, progressR)
}

func main() {
fmt.Println("default draw...")
drawDefault()
fmt.Println("draw bar...")
drawBar()
fmt.Println("draw bytes...")
drawBytes()
}