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()
}
Output:
Index ¶
- Variables
- type Bar
- func (b *Bar) Advance(label string)
- func (b *Bar) BeginWait() (done func())
- func (b *Bar) Clear()
- func (b *Bar) Enabled() bool
- func (b *Bar) Finish(success bool)
- func (b *Bar) SetLabel(label string)
- func (b *Bar) Start(total int, timeout time.Duration)
- func (b *Bar) WithCleared(write func())
- func (b *Bar) Writer(w io.Writer) io.Writer
- type Color
- type Options
- type Palette
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 (*Bar) Advance ¶
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) Finish ¶
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) Start ¶
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.
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.