progress

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 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 the gradient passes through. 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. 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.

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, so the label never flips to dark text in the middle of a word.

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.

func New

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

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

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.

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. The returned function is safe to call more than once, and a late call does not end a newer wait.

func (*Bar) Clear

func (b *Bar) Clear()

Clear erases the bar, running or finished, and shows the cursor again. 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. Either way the frame stays on screen until Bar.Clear, so call Clear right before writing the result.

func (*Bar) SetLabel

func (b *Bar) SetLabel(label string)

SetLabel shows label without counting a step.

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. write must not call methods of b.

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. Each write should be one or more whole lines.

type Color

type Color struct{ R, G, B uint8 }

Color is a 24-bit RGB color.

type Options

type Options struct {
	// Width is the bar width in cells. Zero uses the full width of the
	// terminal, or 80 cells when that is unknown.
	Width int
	// Palette holds the bar's colors. Nil uses [DefaultPalette].
	Palette *Palette
	// TextContrast is the minimum WCAG contrast ratio between the label and
	// the gradient behind it. 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.
	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.
	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