https://github.com/ermanimer/progress_bar
Go Progress Bar
https://github.com/ermanimer/progress_bar
progress-bar
Last synced: 5 months ago
JSON representation
Go Progress Bar
- Host: GitHub
- URL: https://github.com/ermanimer/progress_bar
- Owner: ermanimer
- License: mit
- Created: 2020-12-25T07:45:58.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-08-12T16:01:10.000Z (almost 4 years ago)
- Last Synced: 2024-06-18T20:20:37.980Z (about 2 years ago)
- Topics: progress-bar
- Language: Go
- Homepage:
- Size: 252 KB
- Stars: 127
- Watchers: 7
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# progress_bar

[](https://goreportcard.com/report/github.com/ermanimer/progress_bar)
Go Progress Bar
## Features
progress_bar creates a single customizable progress bar for Unix terminal.
## Installation
```bash
go get -u github.com/ermanimer/progress_bar
```
## Functions
#### DefaultProgressBar(totalValue float64) *ProgressBar
Creates a progress bar with default parameters and given total value.
|Parameter |Description |
|:---------|:---------------------------|
|totalValue|Total value of progress bar|
Default parameters:
|Default Parameter |Value |
|:-----------------------|:--------------------------------------------------------------------------------|
|Default Schema |[{bar}][{percent}][{current}/{total}][Elapsed: {elapsed}s Remaining:{remaining}s]|
|Default Filled Character|# |
|Default Blank Character |. |
|Default Length |50 |
#### NewProgressBar(output io.Writer, schema string, filledCharacter string, blankCharacter string, length float64, totalValue float64) *ProgressBar
Creates a progress bar with default parameters and given total value.
|Parameter |Description |
|:--------------|:-------------------------------|
|output |Output of progress bar |
|schema |Schema of progress bar |
|filledCharacter|Filled character of progress bar|
|blankCharacter |Blank character of progress bar |
|length |Length of progress bar |
|totalValue |Total value of progress bar |
Schema Variables:
|Schema Variable|Value |
|:--------------|:----------------------------|
|{bar} |Bar of progress bar |
|{percent} |Percentage of progress bar |
|{current} |Current value of progress bar|
|{total} |Total value of progress bar |
|{elapsed} |Elapsed duration |
|{remaining} |Estimated remaining duration |
## Methods
#### Start() error
Starts progress bar.
#### Stop() error
Stops progress bar.
#### Update(value float64) error
Updates progress bar with given value and stops progress bar is total value is reached.
|Parameter|Description |
|:--------|:----------------------------|
|value |Current value of progress bar|
## Usage
#### Default Progress Bar:
```go
package main
import (
"fmt"
"time"
"github.com/ermanimer/progress_bar"
)
func main() {
//create new progress bar
pb := progress_bar.DefaultProgressBar(100)
//start
err := pb.Start()
if err != nil {
fmt.Println(err.Error())
return
}
//update
for value := 1; value <= 100; value++ {
time.Sleep(20 * time.Millisecond)
err := pb.Update(float64(value))
if err != nil {
fmt.Println(err.Error())
break
}
}
}
```
Terminal Output:

#### New Progress Bar:
```go
package main
import (
"fmt"
"os"
"time"
"github.com/ermanimer/progress_bar"
)
func main() {
//create parameters
output := os.Stdout
schema := "({bar}) ({percent}) ({current} of {total} completed)"
filledCharacter := "="
blankCharacter := "-"
var length float64 = 60
var totalValue float64 = 80
//create new progress bar
pb := progress_bar.NewProgressBar(output, schema, filledCharacter, blankCharacter, length, totalValue)
//start
err := pb.Start()
if err != nil {
fmt.Println(err.Error())
return
}
//update
for value := 1; value <= 80; value++ {
time.Sleep(20 * time.Millisecond)
err := pb.Update(float64(value))
if err != nil {
fmt.Println(err.Error())
break
}
}
}
```
Terminal Output:

## References
- [ANSI Escape Codes, Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code)
- [Build Your Own Command Line With ANSI Escape Codes, Haoyi's Programming Blog](https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html)