progressbar

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

func (*Bar) Inc

func (b *Bar) Inc()

Inc increments the bar by 1 and triggers a redraw of all bars.

func (*Bar) Set

func (b *Bar) Set(count int)

Set updates the bar to count and triggers a redraw of all bars.

type MultiBar

type MultiBar struct {
	// 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

func (mb *MultiBar) NewBar(label string, total uint16) *Bar

NewBar creates a new Bar with the given label and total step count. It can be called before or after SignalHandler.

func (*MultiBar) Println

func (mb *MultiBar) Println(a ...any)

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)
	// 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

func (pb *PBar) Println(a ...any)

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

func (pb *PBar) RenderPBar(count int)

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL