progress

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 13 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

Overview

Package progress draws a single-line progress bar on a terminal.

The bar fills with a color gradient and carries a label: the step being worked on. Its label text stays readable on every color of a gradient that keeps to dark colors, as the default one does, or to light ones; see Palette. Between steps the fill keeps creeping toward the next step, so a slow step does not look stuck. When an operation inside a step waits longer than a moment, typically a network request, the bar counts down the time left until that operation's timeout.

A Bar draws only when term.CanAnimate reports that its writer may be redrawn in place, so the same code runs unchanged with its output piped to a file. It is as wide as the terminal and stays so when the window changes its width, and it paints 24-bit colors only where the terminal announces them, the nearest of the 256 colors otherwise. All methods are safe for concurrent use. Log lines written while the bar is shown go through Bar.WithCleared or Bar.Writer, which take the bar off the line, write, and draw it again below.

The bar hides the cursor while it is shown, through a live.Cursor of its own. The cursors of one terminal count together, so a bar that finishes does not show a cursor the application around it keeps hidden, and live.InstallSignalRestore shows it again when the process is interrupted.

Redrawing after the window was made narrower counts on a terminal that wraps its lines again for the new width, as most do. On one that cuts its lines off instead (xterm, GNU screen, the Linux console) the bar erases the row above it when a window narrower than the bar is drawn in for the first time.

Example
package main

import (
	"log"
	"os"
	"time"

	"github.com/mittwald/cli-kit/progress"
)

func main() {
	bar := progress.New(os.Stderr, progress.Options{})
	logger := log.New(bar.Writer(os.Stderr), "", 0)

	steps := []string{"Resolving dependencies", "Downloading", "Installing"}
	bar.Start(len(steps), 30*time.Second)
	for _, step := range steps {
		bar.Advance(step)
		done := bar.BeginWait() // for example around an HTTP request
		logger.Printf("%s: ok", step)
		done()
	}
	bar.Finish(true)
	// Erase the bar right before writing the result.
	bar.Clear()
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPalette = Palette{
	Gradient: []Color{
		{R: 54, G: 86, B: 140},
		{R: 82, G: 92, B: 173},
		{R: 115, G: 77, B: 183},
		{R: 153, G: 69, B: 140},
		{R: 44, G: 110, B: 145},
		{R: 43, G: 119, B: 101},
	},
	Track:     Color{R: 52, G: 58, B: 69},
	TrackText: Color{R: 160, G: 167, B: 177},
}

DefaultPalette is a cool gradient from steel blue through violet to teal, on a dark slate track. Every gradient color stays dark enough to carry a light tint of its own hue at the default text contrast.

Functions

This section is empty.

Types

type Bar

type Bar struct {
	// contains filtered or unexported fields
}

Bar is a progress bar drawn on one terminal line. Create it with New, then call Bar.Start once the number of steps is known. All methods are safe for concurrent use, and all of them do nothing when the bar is not Bar.Enabled, except that Bar.WithCleared and the writers of Bar.Writer still write what they are given.

func New

func New(w io.Writer, options Options) *Bar

New returns a bar drawing to w. It draws nothing until Bar.Start.

The bar paints 24-bit colors where the COLORTERM environment variable announces them, as "truecolor" or "24bit", and the nearest of the 256 colors everywhere else, where a 24-bit color could come out as anything.

func (*Bar) Advance

func (b *Bar) Advance(label string)

Advance counts one step as done and shows label as the step now being worked on. The label is shown as term.PlainLine leaves it, on the bar's one line and without escape sequences, so a label taken from untrusted input cannot move the cursor, change the window title or break the bar onto a second line.

func (*Bar) BeginWait

func (b *Bar) BeginWait() (done func())

BeginWait marks the start of an operation that may stall, such as a network request, and returns the function that marks its end. Once the operation has run for the stall delay, the bar counts down the time left until the timeout given to Bar.Start. Operations may overlap: the countdown is that of the oldest one still under way, the one whose timeout comes first. The returned function is safe to call more than once, and it ends no wait but its own.

func (*Bar) Clear

func (b *Bar) Clear()

Clear erases the bar, running or finished, and shows the cursor again. A partial line that a writer of Bar.Writer holds back is written where the bar was, as it is. Clearing a cleared bar writes nothing.

func (*Bar) Enabled

func (b *Bar) Enabled() bool

Enabled reports whether the bar draws at all.

func (*Bar) Finish

func (b *Bar) Finish(success bool)

Finish stops the bar and shows the cursor again. On success the bar is drawn full; otherwise it stays as it is, with the countdown it showed. Either way the frame stays on screen, unchanged by whatever is written through Bar.WithCleared afterwards, until Bar.Clear, so call Clear right before writing the result. A partial line that a writer of Bar.Writer holds back is written above the bar as a line of its own.

func (*Bar) SetLabel

func (b *Bar) SetLabel(label string)

SetLabel shows label without counting a step, made plain as Bar.Advance makes it.

func (*Bar) Start

func (b *Bar) Start(total int, timeout time.Duration)

Start shows the bar for total steps, replacing a bar already shown. The timeout is the time an operation started with Bar.BeginWait may take; the bar counts it down once the operation stalls. A timeout of 0 never shows a countdown. Start hides the cursor until Bar.Finish or Bar.Clear.

func (*Bar) WithCleared

func (b *Bar) WithCleared(write func())

WithCleared takes the bar off its line, calls write, and draws the bar again below whatever write wrote. Use it for every line written to the bar's terminal while the bar is shown. Without a bar shown it just calls write.

The bar is not drawn while write runs, and none of its locks are held: write may call the bar's methods and may write through a writer of Bar.Writer, as a logger set up with one does. Calls from several goroutines run write at the same time; what they write is theirs to keep in order.

func (*Bar) Writer

func (b *Bar) Writer(w io.Writer) io.Writer

Writer returns a writer that writes to w through Bar.WithCleared, for use as a logger's output or as the output of a command that is run.

The bar is drawn below whole lines only, so what follows the last newline of a write is held back until the rest of that line arrives: drawn right away, the bar would cover it. The line is put together from every writer the bar returned for w, where w is a pointer such as an *os.File. A line still held back is written by Bar.Finish above the finished bar, ended with a newline, by Bar.Clear as it is, and by the writer itself once it has grown to 64 KiB. Write reports the bytes it was given as written, held back or not. Nothing is held back while no bar is on screen or when the bar is not Bar.Enabled.

type Color

type Color struct{ R, G, B uint8 }

Color is a 24-bit RGB color.

type Options

type Options struct {
	// Width is the most cells the bar takes up. The bar is never wider than
	// the terminal and follows its width when the window changes, so a
	// Width beyond the terminal's, like zero, uses the full width of the
	// terminal. Where the terminal's width is unknown the bar is Width
	// cells wide, or 80 when Width is zero.
	Width int
	// Palette holds the bar's colors. Nil uses [DefaultPalette].
	Palette *Palette
	// TextContrast is the WCAG contrast ratio the label gets against the
	// gradient behind it, where the gradient allows it: see
	// [Palette.Gradient]. Zero uses 4.5, the WCAG AA level for text.
	TextContrast float64
	// StallDelay is how long an operation started with [Bar.BeginWait] may
	// run before the bar counts down its timeout. Zero uses three seconds.
	StallDelay time.Duration
	// Countdown formats the time left until the timeout. Nil formats it as
	// "(27s)", rounding up so the last second shown is the one being waited
	// out. Its result is shown as term.PlainLine leaves it. Countdown is
	// called while the bar is being drawn and must not call the bar's
	// methods.
	Countdown func(remaining time.Duration) string
	// Force draws the bar even when term.CanAnimate reports that its writer
	// may not be redrawn in place, for demos and tests.
	Force bool
}

Options configures a Bar. The zero value is ready to use.

type Palette

type Palette struct {
	// Gradient holds the colors the filled part of the bar passes through
	// from left to right. A single color fills evenly; an empty Gradient
	// uses the default one.
	//
	// The label on the gradient is a tint of the color behind it: lighter
	// when every color of the gradient is dark enough for light text to
	// reach Options.TextContrast, darker when every color is light enough
	// for dark text to reach it. The whole label goes the same way, so it
	// does not flip in the middle of a word. A gradient that runs from dark
	// colors to light ones leaves no way that reaches the contrast on all
	// of it: the label then goes the way that comes closest, and is hard to
	// read on a part of the bar. Keep a gradient on one side.
	Gradient []Color
	// Track is the background of the part not yet filled.
	Track Color
	// TrackText is the label color on the part not yet filled.
	TrackText Color
}

Palette holds the colors a Bar is drawn in.

Jump to

Keyboard shortcuts

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