Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use generics for new progressbar creation #132

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
@@ -1,20 +1,23 @@
# progressbar

[![CI](https://github.com/schollz/progressbar/actions/workflows/ci.yml/badge.svg?branch=master&event=push)](https://github.com/schollz/progressbar/actions/workflows/ci.yml)
[![go report card](https://goreportcard.com/badge/github.com/schollz/progressbar)](https://goreportcard.com/report/github.com/schollz/progressbar)
[![go report card](https://goreportcard.com/badge/github.com/schollz/progressbar)](https://goreportcard.com/report/github.com/schollz/progressbar)
[![coverage](https://img.shields.io/badge/coverage-84%25-brightgreen.svg)](https://gocover.io/github.com/schollz/progressbar)
[![godocs](https://godoc.org/github.com/schollz/progressbar?status.svg)](https://godoc.org/github.com/schollz/progressbar/v3)
[![godocs](https://godoc.org/github.com/schollz/progressbar?status.svg)](https://godoc.org/github.com/schollz/progressbar/v3)

A very simple thread-safe progress bar which should work on every OS without problems. I needed a progressbar for [croc](https://github.com/schollz/croc) and everything I tried had problems, so I made another one. In order to be OS agnostic I do not plan to support [multi-line outputs](https://github.com/schollz/progressbar/issues/6).

## Requires

Progressbar V4 requires Go version v1.18 or later.

## Install

```
go get -u github.com/schollz/progressbar/v3
```

## Usage
## Usage

### Basic usage

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"time"

"github.com/schollz/progressbar/v3"
"github.com/schollz/progressbar/v4"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/customization/main.go
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"github.com/schollz/progressbar/v4"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/download-unknown/main.go
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/schollz/progressbar/v3"
"github.com/schollz/progressbar/v4"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/download/main.go
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/schollz/progressbar/v3"
"github.com/schollz/progressbar/v4"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/pacman/main.go
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"github.com/schollz/progressbar/v4"
)

func main() {
Expand Down
18 changes: 11 additions & 7 deletions go.mod
@@ -1,15 +1,19 @@
module github.com/schollz/progressbar/v3
module github.com/schollz/progressbar/v4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.13
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/rivo/uniseg v0.3.4 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
)

go 1.13
go 1.18
18 changes: 8 additions & 10 deletions go.sum
Expand Up @@ -3,23 +3,21 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.3.4 h1:3Z3Eu6FGHZWSfNKJTOUiPatWwfc7DzJRU04jFUqJODw=
github.com/rivo/uniseg v0.3.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
33 changes: 11 additions & 22 deletions progressbar.go
Expand Up @@ -269,20 +269,15 @@ func OptionUseANSICodes(val bool) Option {
var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}

// NewOptions constructs a new instance of ProgressBar, with any options you specify
func NewOptions(max int, options ...Option) *ProgressBar {
return NewOptions64(int64(max), options...)
}

// NewOptions64 constructs a new instance of ProgressBar, with any options you specify
func NewOptions64(max int64, options ...Option) *ProgressBar {
func NewOptions[V int | int64](max V, options ...Option) *ProgressBar {
b := ProgressBar{
state: getBasicState(),
config: config{
writer: os.Stdout,
theme: defaultTheme,
iterationString: "it",
width: 40,
max: max,
max: int64(max),
throttleDuration: 0 * time.Nanosecond,
elapsedTime: true,
predictTime: true,
Expand Down Expand Up @@ -326,19 +321,19 @@ func getBasicState() state {

// New returns a new ProgressBar
// with the specified maximum
func New(max int) *ProgressBar {
func New[V int | int64](max V) *ProgressBar {
return NewOptions(max)
}

// DefaultBytes provides a progressbar to measure byte
// throughput with recommended defaults.
// Set maxBytes to -1 to use as a spinner.
func DefaultBytes(maxBytes int64, description ...string) *ProgressBar {
func DefaultBytes[V int | int64](maxBytes V, description ...string) *ProgressBar {
desc := ""
if len(description) > 0 {
desc = description[0]
}
return NewOptions64(
return NewOptions(
maxBytes,
OptionSetDescription(desc),
OptionSetWriter(os.Stderr),
Expand All @@ -357,14 +352,14 @@ func DefaultBytes(maxBytes int64, description ...string) *ProgressBar {

// DefaultBytesSilent is the same as DefaultBytes, but does not output anywhere.
// String() can be used to get the output instead.
func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar {
func DefaultBytesSilent[V int | int64](maxBytes V, description ...string) *ProgressBar {
// Mostly the same bar as DefaultBytes

desc := ""
if len(description) > 0 {
desc = description[0]
}
return NewOptions64(
return NewOptions(
maxBytes,
OptionSetDescription(desc),
OptionSetWriter(ioutil.Discard),
Expand All @@ -379,12 +374,12 @@ func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar {

// Default provides a progressbar with recommended defaults.
// Set max to -1 to use as a spinner.
func Default(max int64, description ...string) *ProgressBar {
func Default[V int | int64](max V, description ...string) *ProgressBar {
desc := ""
if len(description) > 0 {
desc = description[0]
}
return NewOptions64(
return NewOptions(
max,
OptionSetDescription(desc),
OptionSetWriter(os.Stderr),
Expand All @@ -403,14 +398,14 @@ func Default(max int64, description ...string) *ProgressBar {

// DefaultSilent is the same as Default, but does not output anywhere.
// String() can be used to get the output instead.
func DefaultSilent(max int64, description ...string) *ProgressBar {
func DefaultSilent[V int | int64](max V, description ...string) *ProgressBar {
// Mostly the same bar as Default

desc := ""
if len(description) > 0 {
desc = description[0]
}
return NewOptions64(
return NewOptions(
max,
OptionSetDescription(desc),
OptionSetWriter(ioutil.Discard),
Expand Down Expand Up @@ -541,12 +536,6 @@ func (p *ProgressBar) Describe(description string) {
p.render()
}

// New64 returns a new ProgressBar
// with the specified maximum
func New64(max int64) *ProgressBar {
return NewOptions64(max)
}

// GetMax returns the max of a bar
func (p *ProgressBar) GetMax() int {
return int(p.config.max)
Expand Down
6 changes: 3 additions & 3 deletions progressbar_test.go
Expand Up @@ -19,7 +19,7 @@ import (
)

func BenchmarkRender(b *testing.B) {
bar := NewOptions64(100000000,
bar := NewOptions(100000000,
OptionSetWriter(os.Stderr),
OptionShowIts(),
)
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestBarSlowAdd(t *testing.T) {

func TestBarSmallBytes(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions64(100000000, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
bar := NewOptions(100000000, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
for i := 1; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
bar.Add(1000)
Expand All @@ -275,7 +275,7 @@ func TestBarSmallBytes(t *testing.T) {

func TestBarFastBytes(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions64(1e8, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
bar := NewOptions(int(1e8), OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
time.Sleep(time.Millisecond)
bar.Add(1e7)
if !strings.Contains(buf.String(), " GB/s)") {
Expand Down