-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: line break problem in progress bar
I explicitly specified the options to minimize the issue of the progress bar breaking into new lines to the greatest extent possible. refs #7
- Loading branch information
Showing
5 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package progress | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/k0kubun/go-ansi" | ||
"github.com/schollz/progressbar/v3" | ||
"golang.org/x/term" | ||
) | ||
|
||
type Progress struct { | ||
Bar *progressbar.ProgressBar | ||
} | ||
|
||
func NewProgress(max int64, desc string) *Progress { | ||
width, _, err := term.GetSize(int(os.Stdout.Fd())) | ||
if err != nil { | ||
// If the terminal size cannot be taken, it shall be roughly 35. | ||
width = 35 | ||
} else { | ||
// adjust the width roughly... | ||
width = width / 3 | ||
} | ||
|
||
return &Progress{ | ||
Bar: progressbar.NewOptions64( | ||
max, | ||
progressbar.OptionSetWriter(ansi.NewAnsiStdout()), | ||
progressbar.OptionEnableColorCodes(true), | ||
progressbar.OptionSetWidth(width), | ||
progressbar.OptionSetDescription(fmt.Sprintf("🐟 %s", desc)), | ||
progressbar.OptionSetTheme(progressbar.Theme{ | ||
Saucer: "[blue]=[reset]", | ||
SaucerHead: "[blue]>[reset]", | ||
SaucerPadding: " ", | ||
BarStart: "[", | ||
BarEnd: "]", | ||
}), | ||
), | ||
} | ||
} | ||
|
||
func (p *Progress) Increment() { | ||
_ = p.Bar.Add(1) | ||
} |