schollz / progressbar Goto Github PK
View Code? Open in Web Editor NEWA really basic thread-safe progress bar for Golang applications
Home Page: https://pkg.go.dev/github.com/schollz/progressbar/v3?tab=doc
License: MIT License
A really basic thread-safe progress bar for Golang applications
Home Page: https://pkg.go.dev/github.com/schollz/progressbar/v3?tab=doc
License: MIT License
Better defaults for bytes / reg bar
New
Trying to compile using the import "github.com/schollz/progressbar/v3" results in
main.go:4:2: cannot find package "github.com/schollz/progressbar/v3" in any of:
/usr/lib/go/src/github.com/schollz/progressbar/v3 (from $GOROOT)
/root/go/src/github.com/schollz/progressbar/v3 (from $GOPATH)
But it works when removing the v3 in the import: "github.com/schollz/progressbar"
It's cheaper in computation and memory use. You only need memory for the average value, not the last n values.
And it's more flexible. You can specify an arbitrary large rolling window, not only the last 10 values as it is now.
I use this package for my own progressbar (not public):
https://github.com/VividCortex/ewma
The README.me nicely explains the algorithm.
Want progress-bar to display the status of for loop.
for i:=0; i<100; i++ {
// Time consuming work
progressBar(i,100)
}
Hey,
this is a nice little library :)
I am trying to implement my own io.Writer to output to. Eventually I want to output the progress bar to a logging area I have put together in a terminal UI using this library however as a test case I have put together this:
import "github.com/schollz/progressbar"
type tmpStruct struct {
}
func (t *tmpStruct) Write(p []byte) (n int, err error) {
fmt.Fprintf(os.Stdout, string(p[:]))
return len(p), nil
}
func demoLoadingBarCount(maximumInt int) {
var buf tmpStruct
if bufWriter, ok := interface{}(buf).(io.Writer); ok {
bar := progressbar.NewOptions(
maximumInt,
progressbar.OptionSetTheme(progressbar.Theme{Saucer: "#", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
progressbar.OptionSetWidth(10),
progressbar.OptionSetWriter(bufWriter),
)
for i := 0; i < maximumInt; i++ {
bar.Add(1)
time.Sleep(10 * time.Millisecond)
}
}
}
The above seems to compile fine, but I don't see the progress bar on the terminal. Eventually I will change the os.Stdout to the writer of the terminal UI but for now I want to understand why the above isn't showing the terminal. Any help greatly appreciated.
Thanks
It would be nice if the progress bar would show on os.StdErr.
My app has a lot of output which I want to redirect to a file, but still see the state of the progress bar.
Currently the progress bar renders itself once progression is added. However for lengty processes, it can take a while (in my case minutes) before anything shows. This isn't very user friendly and instead I would like the progress bar to render with a 0% state instead, giving some feedback that "it works".
I'm using instead: progressbar.NewOptions64(itemCount)
.
I know that with #11 support for specifying the output stream was added, and thus it is technically possible to use os.StdErr
.
However given that such a library is most useful in CLI tools and filters (like sed
, grep
, etc.) that are part of a pipeline, having status printed on os.StdOut
by default would take many (at least in the *NIX) world by surprise.
For example today I've tried integrating this library without thinking for a second it would clobber the standard output...
Therefore I would suggest making os.Stderr
the default. (At least in a future version like v4
if backward compatibility is a major issue.)
go: finding github.com/schollz/progressbar v0.0.0-20190307141100-7e454dc2feed
go: github.com/schollz/[email protected]: unexpected status (https://goproxy.cn/github.com/schollz/progressbar/@v/v0.0.0-20190307141100-7e454dc2feed.info): 500 Internal Server Error
go: error loading module requirements
Now it tries to import
"github.com/schollz/progressbar/v2"
which fails, is suppose the '/v2' part should simply be removed
Consider adding this to https://github.com/avelino/awesome-go (under "Advanced Console UIs")
After running the "make install" it's showing the following error:-
go generate ./templates
go: creating new go.mod: module github.com/blocklayerhq/chainkit
go: copying requirements from Gopkg.lock
go: github.com/schollz/[email protected]: go.mod has post-v0 module path "github.com/schollz/progressbar/v2" at revision 174fa38
go: error loading module requirements
make: *** [Makefile:13: generate] Error 1
what should I do, please help?
For my use case, I have a single progress bar that moves, but the description changes a lot more frequently, and putting that description before the bar would result in a lot of unwanted movement for the bar itself. Is it possible to tell progressbar to render the description after the bar?
most IO progress implementations seem to only allow for a known fixed length denominator. in the case where the operation is for instance an HTTP POST, the total length/duration is not known and there doesn't seem to be a way to easily tie into the network layer to do a 'better' job. to satisfy this use case it would be great to have the progress bar simply ping back and forth, ping pong style, until progress bar Finish or Reset is called. Passing -1 perhaps to OptionSetWidth could be an approach to indicate unknown length.
Will this do multi line outputs?
If so you may want to add that to the docs.
Or would this unfortunately fall under #6 and multi-line in-compatibility?
Related to #57
I testing spinners on windows:
AWS => Microsoft Windows Server 2019 Base - ami-0c278895328cddfdd
Same code works fine on macOS (check https://asciinema.org/a/327940?speed=2&autoplay=1&cols=150&rows=35&size=medium&loop=1)
The code I am using is simple:
This error happens every 9 out of 10 times, all renders error out with: golang.org/x/sys/windows.ERROR_INVALID_HANDLE (6)
. I am using the default GoLand configuration, so it's a Command Prompt terminal.
Ref: #79
The spinner is not working for me. I try to use it when reading from os.Stdin
:
r = progressbar.NewReader(os.Stdin, progressbar.DefaultBytes(-1, "Test"))
The resulting progress bar doesn't "spin", there is just a static character. Also the \n
is not appended after completion.
I'm running this in zsh on macOS.
If you create a new default bar and immediately try to RenderBlank()
, nothing will render. To work around this:
func NewBar(max int, description ...string) *progressbar.ProgressBar {
bar := progressbar.Default(int64(max), description...)
// wait for throttle duration so initial render doesn't skip
time.Sleep(65 * time.Millisecond)
bar.RenderBlank()
return bar
}
Haven't tested, but I think the problem is the p.state.lastShown
is set to time.Now()
along with p.config.throttleDuration
in:
func getBasicState() state {
now := time.Now()
return state{
startTime: now,
lastShown: now,
counterTime: now,
}
}
Maybe set lastShown
to 0?
Sometimes progress bar isn't the last thing in application output (and when it comes to 100% it might be good idea to replace the progress bar with something like "Operation finished, moving to next step". For this "replacement" a function that will erase progress bar from stdout may be useful.
This is how I am trying to import it into my code :
import (
...
...
"github.com/tidwall/gjson"
"github.com/schollz/progressbar/v3"
)
And this is how I am running it
$ GO111MODULE=on go get -u github.com/schollz/progressbar/v3
go: downloading github.com/schollz/progressbar/v3 v3.5.1
go: downloading github.com/schollz/progressbar v1.0.0
go: github.com/schollz/progressbar/v3 upgrade => v3.5.1
go: downloading github.com/mattn/go-runewidth v0.0.9
go: downloading github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
$ export GO111MODULE=on
$ go run main.go
main.go:18:2: cannot find module providing package github.com/schollz/progressbar/v3: working directory is not part of a module
main.go:17:2: cannot find module providing package github.com/tidwall/gjson: working directory is not part of a module
Since there is no way to determine if a terminal (emulator) supports certain ansi escape codes, it would be nice to offer a "option" that would allow to use ANSI escape codes to reduce terminal writes to the required minimum
This would allow for a more flicker-free experience (atleast for windows users)
currently the progressbar flickers when used in the "Windows Terminal", or a conhost environment.
This is due to the call to "clearProgressBar" which happens on every single render call.
func (p *ProgressBar) render() error {
// ...
// first, clear the existing progress bar
err := clearProgressBar(p.config, p.state)
if err != nil {
return err
}
// either returns because finished, or renders current progress
by only calling the clear function if "finished" and otherwise appending the ANSI escape sequence of \033[0K
to the rendered string, we can write a full clean line in just one write call, without a prior clear
.
This however is only supported on terminal(emulators) that support ANSI escape sequences, like virtually every unix terminal and Windows 10 conhost (opt-in 1) and "Windows Terminal"
Before using the ANSI escape code option:
I do not want the progressBar to implement windows specific code, but for it to offer a way to make use of ANSI escape sequences if a specific option is provided. e.g.
func OptionUseANSICodes(val bool) Option {
return func(p *ProgressBar) {
p.config.useANSICodes = val
}
}
1 atleast ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x04)
must be enabled on the terminal output console handle (e.g. os.Stderr.Fd()
) to support escape codes. This is easily achieved using syscall.LazyDll
and calling the GetConsoleMode
+ SetConsoleMode
Kernel32-APIs to enable the flags on demand.
This is code that shows how easy it can be to implement.
I am using two progressbar but it will in the same line and switch each other.
Is it possible to have an option to use 1024 as base for byte sizes?
Currently, if a bar is created with maximum number of items X
and then updated (.Add(i)
) with i
that is significantly lower than 1% of X
(e.g. 0.1%), then the item count shown to the right of the bar ((n/X)
) will only update when it has accumulated to a whole percentage value (e.g. 1%, 2%) and so on. When you have a lot of items in between, it looks really weird.
It is possible to introduce fractional percentage (which i guess will by default fix this behaviour), or somehow re-render the item count text on Add?
Anything against having int64
for maxBytes, currentNum and so on?
If we're processing a file, and we're using a progressbar, it's likely it's a huge file :)
int
will do in 64bit OS, but not in 32bit one (such as raspbian)
I have a case where I want to show a progress but I have no knowledge when the task will finish.
When revert 3e6b9bf
Each time the progress bar is rendered, an empty line is added.
func main() {
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
bar.Add(1)
time.Sleep(40 * time.Millisecond)
}
}
Go vet shows the following errors:
# github.com/schollz/progressbar/v2
./progressbar_test.go:39:1: ExampleProgressBarSet refers to unknown identifier: ProgressBarSet
./progressbar_test.go:46:1: ExampleProgressBarSet64 refers to unknown identifier: ProgressBarSet64
./progressbar_test.go:53:1: ExampleProgressBarBasic refers to unknown identifier: ProgressBarBasic
./progressbar_test.go:62:1: ExampleThrottle refers to unknown identifier: Throttle
./progressbar_test.go:73:1: ExampleChangeMax refers to unknown identifier: ChangeMax
./progressbar_test.go:79:1: ExampleFinish refers to unknown identifier: Finish
./progressbar_test.go:87:1: ExampleFinish2 refers to unknown identifier: Finish2
./progressbar_test.go:94:1: ExampleXOutOfY refers to unknown identifier: XOutOfY
./progressbar_test.go:103:1: ExampleSetBytes refers to unknown identifier: SetBytes
./progressbar_test.go:112:1: ExampleShowCount refers to unknown identifier: ShowCount
./progressbar_test.go:121:1: ExampleSetIts refers to unknown identifier: SetIts
./progressbar_test.go:331:1: ExampleDescribe refers to unknown identifier: Describe
For example ExampleProgressBarSet
should be called ExampleProgressBar_Set
to be detected properly as an example for the Set
method. For more info see https://blog.golang.org/examples.
Additionally I'd suggest using the ioutil.TempFile
function instead of explicitly creating a named file in TestReaderToFile and TestReaderToBuffer. TestReaderToBuffer does not actually to create a file at all.
If you like I can prepare a pull request.
Hi thanks for the package, I've run into a problem with the installation, same error as the others listed in schollz/croc#110.
Stack Trace:
jun ~ go get -u github.com/schollz/progressbar/v2
package github.com/schollz/progressbar/v2: cannot find package "github.com/schollz/progressbar/v2" in any of:
/usr/lib/go/src/github.com/schollz/progressbar/v2 (from $GOROOT)
/home/jun/go/src/github.com/schollz/progressbar/v2 (from $GOPATH)
jun ~ go version
go version go1.13 linux/amd64
However I'm running go 1.13. Is the only current solution using ?env:GO111MODULE=
It looks like you've created a wrapper for the Reader interface, but it's impossible to use, as you can't declare it in external packages as it has the unexported field "bar":
The Reader is exported as:
Is this a mistake, or is the Reader just a leftover experiment?
For my use case I'm opening a file (Reader) and feeding it to another component that expects a Reader. There is no writer involved.
ERROR: Permission to schollz/progressbar.git denied to Kiura.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Sorry I am new to git cli, could you help me out here?
When the progressbar reaches the maximum for the first time, maybe it can emit a new line?
Being able to know when the progessbar is finished would be very helpful. The State()
func is not helpful when the max is set to -1, aka when we don't know when the progress bar will end.
Currrently, ProgressBar.w
is private and there is no ability to switch it from stdout to stderr, for example. I suggest to add a function to change it.
The README have functions, defined in v2
of library, but
go get -u github.com/schollz/progressbar
installs v1, so README examples are broken. The right way to install library is go get -u github.com/schollz/progressbar/v2
.
When I try to install using λ › go get -u github.com/schollz/progressbar/v3
I get this error:
package github.com/schollz/progressbar/v3: cannot find package "github.com/schollz/progressbar/v3" in any of:
/usr/lib/go/src/github.com/schollz/progressbar/v3 (from $GOROOT)
/home/tozkoparan/go/src/github.com/schollz/progressbar/v3 (from $GOPATH)
Not so much a bug as a request to see if someone can assist.
I have the progress bar in my app. If I run my app using go run main.go
it works fine. However, if I run docker-container up --build
it doesn't show the progress bar until it reaches 100%.
Does anyone know how what I should change to make it show the progress bar?
In readme, 0 is specified as value to give Default and DefaultBytes to create a spinner, but this should be -1 (this seems to be correct in the examples)
my progressbar init:
max := 10000
writer := os.Stdout
bar := progressbar.NewOptions(max,
progressbar.OptionSetDescription(""),
progressbar.OptionSetWriter(writer),
progressbar.OptionSetWidth(50),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionOnCompletion(func() {
fmt.Fprint(writer, "\n")
}),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
)
_ = bar.RenderBlank()
for i := 0; i <= max; i++ {
bar.Add(1)
time.Sleep(time.Millisecond * 50)
}
Option to leave progress bar onscreen when finished. Useful for seeing counts and rates after completing a run.
This line:
Line 661 in e234529
Should be something like:
"github.com/mattn/go-runewidth"
...
stringWidth := runewidth.StringWidth(cleanString)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.