Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bar ¶
type Bar struct {
Label string
Total uint16
DoneStr string
OngoingStr string
// contains filtered or unexported fields
}
Bar is a single named progress bar owned by a MultiBar. Obtain one via MultiBar.NewBar(); call Set or Inc from any goroutine.
type MultiBar ¶
type MultiBar struct {
// NonTTYStep controls non-TTY progress output. A line "label: count/total"
// is written to stderr each time a bar's percentage crosses a multiple of
// this value. Set to 0 to disable. Default: 10.
NonTTYStep int
// contains filtered or unexported fields
}
MultiBar manages a set of concurrent progress bars rendered at the bottom of the terminal. Log lines printed via Println appear above the bars and scroll the bar area downward naturally.
Typical usage with sync.WaitGroup:
mb := cmd.NewMultiBar()
mb.SignalHandler()
defer mb.CleanUp()
var wg sync.WaitGroup
for _, task := range tasks {
wg.Add(1)
bar := mb.NewBar(task.Name, task.Total)
go func(b *cmd.Bar) {
defer wg.Done()
for i := 1; i <= int(b.Total); i++ {
b.Set(i)
}
}(bar)
}
wg.Wait()
func NewMultiBar ¶
func NewMultiBar() *MultiBar
NewMultiBar creates a MultiBar that writes to stderr.
func (*MultiBar) CleanUp ¶
func (mb *MultiBar) CleanUp()
CleanUp erases all bar lines and stops signal handling. Recommended: defer mb.CleanUp().
func (*MultiBar) Close ¶
func (mb *MultiBar) Close()
Close stops signal delivery. Safe to call multiple times.
func (*MultiBar) NewBar ¶
NewBar creates a new Bar with the given label and total step count. It can be called before or after SignalHandler.
func (*MultiBar) Println ¶
Println prints a log line above the bar area without corrupting the bars. Safe to call from any goroutine.
func (*MultiBar) SignalHandler ¶
func (mb *MultiBar) SignalHandler()
SignalHandler starts a goroutine that handles SIGWINCH (redraw) and SIGTERM/SIGINT (cleanup + exit).
type PBar ¶
type PBar struct {
Total uint16 // Total number of iterations to sum 100%
DoneStr string // progress bar "done" character(s)
OngoingStr string // progress bar "remaining" character(s)
// NonTTYStep controls non-TTY progress output. A line "count/total" is
// written to stderr each time the percentage crosses a multiple of this
// value. Set to 0 to disable non-TTY output entirely. Default: 10.
NonTTYStep int
// contains filtered or unexported fields
}
PBar is a simple terminal progress bar for non-Windows platforms.
Design goals:
- Terminal-agnostic: uses only widely supported ANSI sequences:
- "\r" (carriage return) to go to start of line
- "\x1B[0K" (EL0) to clear from cursor to end of line (better on "grow wider")
- Safe with "real program" output:
- The progress bar writes to stderr (by default)
- Your program can print to stdout (numbers/logs) without corrupting the bar
- Resizes safely:
- Horizontal resize: recompute width from TTY columns and redraw
- Vertical resize: we do NOT pin to the bottom row or use scroll regions; instead we ensure we never leave leftover characters by erasing the tail from the previous render (lastRenderWidth).
func NewPBar ¶
func NewPBar() *PBar
NewPBar creates a new progress bar instance.
After calling NewPBar():
- set pb.Total
- call pb.SignalHandler() once
- call pb.RenderPBar(i) during work
- defer pb.CleanUp() to clear the line on exit
func (*PBar) CleanUp ¶
func (pb *PBar) CleanUp()
CleanUp clears the progress bar line and stops signal handling.
Call this at the end of your program (recommended: defer pb.CleanUp()).
func (*PBar) Close ¶
func (pb *PBar) Close()
Close stops signal delivery and closes the signal channels. Safe to call multiple times.
func (*PBar) Println ¶
Println prints a normal line without corrupting the progress bar.
When attached to a TTY, we print the message to pb.out (same fd as the bar) to avoid stdout/stderr interleaving that can glue/duplicate progress renders. When NOT a TTY (pipes/CI), we print to stdout as usual.
func (*PBar) RenderPBar ¶
RenderPBar renders the progress bar for the given count.
count is clamped to [0..Total].
func (*PBar) SignalHandler ¶
func (pb *PBar) SignalHandler()
SignalHandler starts a goroutine that listens to SIGWINCH/SIGTERM.
- SIGWINCH: recompute terminal columns and redraw the bar. - SIGTERM/SIGINT: cleanup and exit(1).