Terminal Progress Renderer (progress/progress.go)

ollama · progress/progress.go · go · MIT
ui, cli, lang:go, tag:concurrency

Renders and manages animated terminal progress states (e.g., spinners) in a thread-safe loop, supporting start/stop and clearing of output.

// Source: https://github.com/ollama/ollama | License: MIT | Copyright Ollama
// Provided via justclaw.it.com; keep this notice with the code.
package progress

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sync"
	"time"

	"golang.org/x/term"
)

const (
	defaultTermWidth  = 80
	defaultTermHeight = 24
)

type State interface {
	String() string
}

type Progress struct {
	mu sync.Mutex
	// buffer output to minimize flickering on all terminals
	w *bufio.Writer

	pos int

	states []State

	stopOnce sync.Once
	// done is closed to tell the render loop to exit.
	done chan struct{}
	// loopDone is closed by the render loop when it exits; stop waits on it so
	// no in-flight render can race the writes in Stop and StopAndClear.
	loopDone chan struct{}
}

func NewProgress(w io.Writer) *Progress {
	p := &Pro

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key