gospin

package module
v0.0.0-...-bfe06bc Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 3 Imported by: 0

README

Progress Spinner for Go

Build Status Coverage Status Maintainability

gospin provides a console spinner to indicate activity with a running program. By default the spinner is animated on every tick, but a porcelain mode for log output is also provided.

Installation

go get github.com/domdavis/gospin

Basic Usage

package main

import (
    "fmt"
    "github.com/domdavis/gospin"
    "time"
)

func main() {
	fmt.Print("Working ")
	s := gospin.New()
	
	for i := 0; i < 50; i++ {
		s.Advance()
	}
	
	s.Done()
	fmt.Println("... done")
}

Advanced Usage

Spinners can be constructed by providing a set of frames, specifying the width of the frame if non-standard characters are used, and the output destination.

s := gospin.New("☱", "☲", "☴")
s.Width(1)
s.Writer(os.Stderr)

// Start drawing the spinner
s.Advance()

// ...do work, calling Advance() as the work progresses.

// Finally remove the spinner.
s.Done()

See the examples for a full list of predefined spinners.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Spinner

type Spinner interface {
	// Advance the spinner forward one frame. The previous frame will be
	// overwritten by the new frame. On the first call to Advance the cursor
	// will also be hidden.
	Advance()

	// Done will remove the spinner, show the cursor and reset the spinner
	// ready for reuse.
	Done()

	// Width overrides the width of the spinner. By default the number of
	// characters in the first frame are used to determine the width of the
	// spinner, however this doesn't take into account things like control
	// characters.
	Width(w int)

	// Porcelain stops the spinner from producing any output and calls to
	// Advance and Done will do nothing.
	Porcelain()

	// Writer sets the writer to use when producing the spinner. By default the
	// writer is stdout. Setting the writer sets porcelain mode to false. If a
	// custom writer is used then setting porcelain needs to happen outside of
	// the Spinner and after the writer is set.
	Writer(writer Writer)
}

A Spinner outputs a spinner in place to stdout, allowing activity to be indicated. Run in porcelain mode a Spinner does nothing. A spinner will try to detect if it's not running in a terminal and set porcelain mode if it is.

func Basic

func Basic() Spinner

Basic returns a basic Spinner.

func Dot

func Dot() Spinner

Dot returns a spinner that uses a single dot moving in a circle.

func Dots

func Dots() Spinner

Dots returns a Spinner that uses multiple dots moving in a circle.

func Ellipses

func Ellipses() Spinner

Ellipses returns a Spinner that repeatedly draws ellipses.

func New

func New(frames ...string) Spinner

New returns a new Spinner that uses the given frames for the spinner animation. The Spinner will attempt to determine if it's running in the console, engaging porcelain mode if it isn't. The first frame of the animation is used to determine the width of the spinner. If no frames are provided then the basic frames ("|", "/", "-", and "\") are used.

Example
package main

import (
	"github.com/domdavis/gospin"
)

func main() {
	// This is the same as gospin.Basic()
	gospin.New()

	// New spinner with custom frames
	gospin.New("-", "+", "*", "+", "-", " ")

	// Basic ascii spinner using |,/,-, and \
	gospin.Basic()

	// Spinner with a single dot that moves in a circle
	gospin.Dot()

	// Spinner with multiple dots moving in a circle
	gospin.Dots()

	// Spinner that uses the frames ".", "..", "...", ""
	gospin.Ellipses()

	// Spinner that uses scrolling ellipses.
	gospin.Scrolling()

}
Output:

func Scrolling

func Scrolling() Spinner

Scrolling returns a Spinner that produces scrolling ellipses.

type Writer

type Writer interface {

	// Write len(b) bytes to the output or output buffer. It returns the number
	// of bytes written and an error, if any. Write returns a non-nil error when
	// n != len(b). The contents of Write may not appear on the output until
	// Sync is called.
	Write(b []byte) (int, error)

	// Sync flushes the writer to the output it is tied to.
	Sync() error
}

A Writer allows strings to be written to an output. Output may not appear until Sync is called.

Jump to

Keyboard shortcuts

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