Implementation:Ollama Ollama Progress Manager
| Knowledge Sources | |
|---|---|
| Domains | CLI, Progress |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Progress display manager that coordinates multiple concurrent progress indicators (bars and spinners) with periodic terminal rendering using ANSI escape codes.
Description
The Progress struct holds a mutex-protected list of State objects (anything implementing String() string) and renders them every 100ms via a ticker goroutine. NewProgress accepts an io.Writer (typically os.Stderr), wraps it in a bufio.Writer for buffered output, and starts the rendering goroutine. The render method uses ANSI escape codes for cursor movement (\\033[A to move up), line clearing (\\033[2K\\033[1G), synchronized output (\\033[?2026h/l to reduce flicker), and cursor hiding (\\033[?25l/h). Rendering respects terminal height to avoid overflow. Add appends new states, Stop halts rendering and flushes with a newline, and StopAndClear erases all progress lines by moving up and clearing each line.
Usage
Created by the CLI during model pull/push operations to manage multiple concurrent progress bars (one per model layer) and spinners (for processing states) in a clean terminal display.
Code Reference
Source Location
- Repository: Ollama
- File: progress/progress.go
- Lines: 1-134
Signature
type State interface {
String() string
}
type Progress struct {
mu sync.Mutex
w *bufio.Writer
pos int
ticker *time.Ticker
states []State
}
func NewProgress(w io.Writer) *Progress
func (p *Progress) Add(key string, state State)
func (p *Progress) Stop() bool
func (p *Progress) StopAndClear() bool
Import
import "github.com/ollama/ollama/progress"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| w | io.Writer | Yes | Output writer (typically os.Stderr) for rendering |
| key | string | Yes | Identifier for the progress state being added |
| state | State | Yes | Progress display object (Bar, Spinner, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| stopped | bool | Whether the progress display was successfully stopped |
Usage Examples
import (
"os"
"github.com/ollama/ollama/progress"
)
// Create a progress manager
p := progress.NewProgress(os.Stderr)
// Add multiple progress bars for concurrent layer downloads
bar1 := progress.NewBar("layer 1", 500000000, 0)
bar2 := progress.NewBar("layer 2", 300000000, 0)
p.Add("layer1", bar1)
p.Add("layer2", bar2)
// Update bars as downloads progress
bar1.Set(250000000)
bar2.Set(100000000)
// Stop and clear when complete
p.StopAndClear()