ui

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package ui defines unified user interface to decouple the information source from the presentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, task *Task) context.Context

NewContext returns a new Context, derived from ctx, which carries the provided task.

Types

type CSVType

type CSVType int

CSVType is an enumeration of the supported CSV file types.

const (
	// ProgressCSV suggests an *os.File is a csv for progress updates.
	ProgressCSV CSVType = iota
	// CounterCSV suggests an *os.File is a csv for task counting updates.
	CounterCSV
)

type Progress

type Progress struct {
	Task
	AggregateTo *Progress
}

Progress simply sends progress updates along the provided channel. It also logs to the provides `logging.LogFrontend`. All methods are thread-safe.

func (*Progress) SubTaskWithProgress

func (p *Progress) SubTaskWithProgress(name string) *Progress

SubTaskWithProgress creates a task supporting progress. You must call Complete() on the progress when finished.

func (*Progress) Update

func (p *Progress) Update(deltaComplete, deltaTotal int64)

Update provides a relative progress update.

func (*Progress) Write

func (p *Progress) Write(data []byte) (int, error)

Write implements the io.Writer interface.

type Task

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

Task simply sends task updates along the provided channel. It also logs everything to the provided log. All methods are thread-safe.

func FromContextOrNoop

func FromContextOrNoop(ctx context.Context) *Task

FromContextOrNoop returns the current Task.

func (*Task) Complete

func (tsk *Task) Complete()

Complete the task. Must be called exactly once.

func (*Task) Info

func (tsk *Task) Info(a ...any)

Info suggests an informational message to be displayed. This is a transient informational message.

func (*Task) Infof

func (tsk *Task) Infof(format string, args ...any)

Infof suggests an informational message to be displayed. The message uses Printf style formatting. This is a transient informational message.

func (*Task) SubTask

func (tsk *Task) SubTask(name string) *Task

SubTask returns a new nested Task where everything send is related to the parent and this child task with name. This is a "prefix" for all information in this task. You must call Complete() when done with the work in this Task.

func (*Task) SubTaskWithProgress

func (tsk *Task) SubTaskWithProgress(name string) *Progress

SubTaskWithProgress creates a task supporting progress. You must call Complete() on the progress when finished.

type UI

type UI interface {
	// Root task
	Root(ctx context.Context) *Task

	// Run will block until the context is exceeded or Close() is called. This runs the actual UI updating the screen.
	Run(ctx context.Context) error

	// Shutdown shuts down the UI.  It returns immediately but has the side-effect that Run() should return shortly there-after.
	Shutdown()
}

UI is a simple user interface abstraction.

func NewComplexUI

func NewComplexUI(out *os.File) UI

NewComplexUI returns a new fancy UI that outputs messages to "out". Task names are prefixed to messages to provide the necessary context. Progress is displayed as a progress bar

out must be a terminal.

func NewDebugUI

func NewDebugUI(out *os.File) UI

NewDebugUI returns a debug UI. Output is expected to be a log file.

func NewSilentUI

func NewSilentUI() UI

NewSilentUI returns a UI that outputs nothing (quiet mode).

func NewSimpleUI

func NewSimpleUI(out io.Writer) UI

NewSimpleUI returns a new simple UI that simply outputs messages to "out". Task names are prefixed to messages to provide the necessary context. Progress is updated regularly.

out need not be a terminal for this UI.

Example
var ui = NewSimpleUI(os.Stdout)

rootUI := ui.Root(context.Background())
defer rootUI.Complete()

gX := rootUI.SubTask("Processing X")
defer gX.Complete()
gX.Infof("Doing something now")

gY := rootUI.SubTask("Processing Y")
defer gY.Complete()
gY.Infof("Doing other work")

// some time has elapsed...

p := gX.SubTaskWithProgress("Transferring")
p.Update(4, 100)
p.Infof("Finished X")
p.Update(99, 100)
p.Complete()

Directories

Path Synopsis
Package tracker provides progress tracking
Package tracker provides progress tracking

Jump to

Keyboard shortcuts

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