spinner

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2019 License: MIT Imports: 3 Imported by: 1

README

Package spinner implements simple "progress spinner" for terminal output.

See https://pkg.go.dev/github.com/artyom/spinner for details.

Documentation

Overview

Package spinner implements simple "progress spinner" for terminal output.

Spinner expects to have exclusive access to underlying *os.File, that nothing else is writing there while spinner is in use, otherwise output would be broken.

If provided *os.File (usually os.Stdout or os.Sterr) is not attached to a terminal, spinner outputs nothing, that makes it safe to redirect program output to files, pipes, etc.

Spinner can either be used manually, by first creating it with New function, then periodically calling Spin() method on it to refresh output and finally finishing with Clear() method call to clean output; or package-level Spin shortcut function can be used to launch background goroutine that handles output refresh.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Spin

func Spin(f *os.File, text string) (stop func())

Spin is a shortcut function which creates new Spinner, launches background goroutine that periodically calls spinner's Spin method, and returns function that stops that background goroutine and clears spinner output.

Can be used like this:

func work() {
    defer spinner.Spin(os.Stderr, "working...")()
    // logic here
}

It is expected that nothing else is writing to underlying *os.File until stop function returns.

Types

type Spinner

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

Spinner implements terminal spinner attached to *os.File which usually either stdout or stderr. Both zero and nil values are valid and are no-op. If spinner created on an *os.File that is not attached to the terminal, spinner's methods do nothing.

Its methods are NOT thread safe, and it expects to have exclusive access to underlying *os.File — that nothing is writing to it while Spinner's methods are in use.

func New

func New(f *os.File, text string) *Spinner

New returns new Spinner attached to f which usually either os.Stdout or os.Stderr. If f is attached to a terminal, retrurned spinner would output text followed by space and "spinning" character on each Spin call.

*os.File provided must not be nil.

func (*Spinner) Clear

func (s *Spinner) Clear()

Clear redraws output with spaces, clearing previous output if underlying *os.File is attached to a terminal.

func (*Spinner) Spin

func (s *Spinner) Spin()

Spin redraws output if underlying *os.File is attached to a terminal.

Directories

Path Synopsis
Example program demonstrating two ways to use spinner package
Example program demonstrating two ways to use spinner package

Jump to

Keyboard shortcuts

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