mirror of https://github.com/schollz/croc.git
Update progressbar
This commit is contained in:
parent
4574e4816f
commit
bab9ceb1bb
|
@ -28,8 +28,8 @@
|
|||
[[projects]]
|
||||
name = "github.com/schollz/progressbar"
|
||||
packages = ["."]
|
||||
revision = "b8e001516e13bd132ed253bf1b175dbfb5cdf308"
|
||||
version = "v0.2.0"
|
||||
revision = "98649a7575ca56014cb102885f09f05bb2cccb49"
|
||||
version = "v0.3.1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/sirupsen/logrus"
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
<p align="center">
|
||||
<img
|
||||
src="logo.png"
|
||||
width="100%" border="0" alt="progressbar">
|
||||
<br>
|
||||
<a href="https://travis-ci.org/schollz/progressbar"><img src="https://travis-ci.org/schollz/progressbar.svg?branch=master" alt="Build Status"></a>
|
||||
<img src="https://img.shields.io/badge/coverage-94%25-brightgreen.svg" alt="Code Coverage">
|
||||
<a href="https://goreportcard.com/report/github.com/schollz/progressbar"><img src="https://goreportcard.com/badge/github.com/schollz/progressbar" alt="Go Report Card"></a>
|
||||
<a href="https://godoc.org/github.com/schollz/progressbar"><img src="https://godoc.org/github.com/schollz/progressbar?status.svg" alt="GoDoc"></a>
|
||||
</p>
|
||||
# progressbar
|
||||
|
||||
<p align="center">A very simple progress bar.</p>
|
||||
[![travis](https://travis-ci.org/schollz/progressbar.svg?branch=master)](https://travis-ci.org/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-94%25-brightgreen.svg)
|
||||
[![godocs](https://godoc.org/github.com/schollz/progressbar?status.svg)](https://godoc.org/github.com/schollz/progressbar)
|
||||
|
||||
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.
|
||||
|
||||
![Example of progress bar](https://user-images.githubusercontent.com/6550035/32120326-5f420d42-bb15-11e7-89d4-c502864e78eb.gif)
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
go get -u github.com/schollz/progressbar
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
**Basic usage:**
|
||||
|
||||
|
@ -26,3 +32,18 @@ which looks like:
|
|||
```bash
|
||||
100% |████████████████████████████████████████| [1s:0s]
|
||||
```
|
||||
|
||||
The times at the end show the elapsed time and the remaining time, respectively.
|
||||
|
||||
## Contributing
|
||||
|
||||
Pull requests are welcome. Feel free to...
|
||||
|
||||
- Revise documentation
|
||||
- Add new features
|
||||
- Fix bugs
|
||||
- Suggest improvements
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
bar := progressbar.New(100)
|
||||
bar := progressbar.New(1000)
|
||||
bar.Reset()
|
||||
for i := 0; i < 100; i++ {
|
||||
for i := 0; i < 1000; i++ {
|
||||
bar.Add(1)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
|
|
|
@ -35,19 +35,17 @@ type ProgressBar struct {
|
|||
// New returns a new ProgressBar
|
||||
// with the specified maximum
|
||||
func New(max int) *ProgressBar {
|
||||
p := new(ProgressBar)
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
p.max = max
|
||||
p.size = 40
|
||||
p.symbolFinished = "█"
|
||||
p.symbolLeft = " "
|
||||
p.leftBookend = "|"
|
||||
p.rightBookend = "|"
|
||||
p.w = os.Stdout
|
||||
p.lastShown = time.Now()
|
||||
p.startTime = time.Now()
|
||||
return p
|
||||
return &ProgressBar{
|
||||
max: max,
|
||||
size: 40,
|
||||
symbolFinished: "█",
|
||||
symbolLeft: " ",
|
||||
leftBookend: "|",
|
||||
rightBookend: "|",
|
||||
w: os.Stdout,
|
||||
lastShown: time.Now(),
|
||||
startTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// Reset will reset the clock that is used
|
||||
|
@ -85,6 +83,10 @@ func (p *ProgressBar) Add(num int) error {
|
|||
// Set will change the current count on the progress bar
|
||||
func (p *ProgressBar) Set(num int) error {
|
||||
p.Lock()
|
||||
if p.max == 0 {
|
||||
p.Unlock()
|
||||
return errors.New("max must be greater than 0")
|
||||
}
|
||||
p.currentNum = num
|
||||
percent := float64(p.currentNum) / float64(p.max)
|
||||
p.currentSaucerSize = int(percent * float64(p.size))
|
||||
|
@ -112,7 +114,7 @@ func (p *ProgressBar) Show() error {
|
|||
strings.Repeat(p.symbolFinished, p.currentSaucerSize),
|
||||
strings.Repeat(p.symbolLeft, p.size-p.currentSaucerSize),
|
||||
p.rightBookend,
|
||||
time.Since(p.startTime).Round(time.Second).String(),
|
||||
(time.Duration(time.Since(p.startTime).Seconds()) * time.Second).String(),
|
||||
(time.Duration(secondsLeft) * time.Second).String(),
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package progressbar
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ExampleBar() {
|
||||
bar := New(10)
|
||||
|
@ -12,3 +15,14 @@ func ExampleBar() {
|
|||
// Output:
|
||||
// 10% |█ | [1s:9s]
|
||||
}
|
||||
|
||||
func TestBar(t *testing.T) {
|
||||
bar := New(0)
|
||||
if err := bar.Add(1); err == nil {
|
||||
t.Error("should have an error for 0 bar")
|
||||
}
|
||||
bar = New(10)
|
||||
if err := bar.Add(11); err == nil {
|
||||
t.Error("should have an error for adding > bar")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue