spin

package module
v0.0.0-...-581d9f9 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 14 Imported by: 0

README

Spin Module

The Spin module provides a thread-safe spinner with various available character sets, prefix, suffix, and color. The spinner can be controlled using an Options pattern.

Install

import "github.com/stelmanjones/termtools/spin"

Usage

s := spin.New(spin.BouncingBar, time.Millisecond * 10,
    spin.WithPrefix("SPINNING "),
    spin.WithSuffix("AFTER"),
    spin.WithColor(color.FgGreen),
    spin.WithFinalMsg("BYE!"))

s.Start()
time.Sleep(time.Second * 3)
s.Stop()

Documentation

Overview

Package spin provides a thread safe Spinner with various available character sets, prefix, suffix and color. The Spinner can be controlled using an Options pattern. Example:

s := spin.New(spin.BouncingBar, time.Millisecond * 10,
    spin.WithPrefix("SPINNING "),
    spin.WithSuffix("AFTER"),
    spin.WithColor(color.FgGreen),
    spin.WithFinalMsg("BYE!"))

s.Start()
time.Sleep(time.Second * 3)
s.Stop()

Index

Constants

This section is empty.

Variables

View Source
var (
	// GrowVertical is a spinner variant that grows the spinner vertically.
	GrowVertical = NewSpinnerVariant(CharSets[0], 80)
	// Bounce is a spinner variant that bounces the spinner.
	Bounce = NewSpinnerVariant(CharSets[1], 120)
	// Dots1 is a spinner variant that shows three dots.
	Dots1 = NewSpinnerVariant(CharSets[2], 80)
	// Dots2 is a spinner variant that shows three dots.
	Dots2 = NewSpinnerVariant(CharSets[3], 80)
	// Dots3 is a spinner variant that shows three dots.
	Dots3 = NewSpinnerVariant(CharSets[4], 80)
	// Letters is a spinner variant that shows the letters a-z.
	Letters = NewSpinnerVariant(CharSets[5], 120)
	// GrowHorizontal is a spinner variant that grows the spinner horizontally.
	GrowHorizontal = NewSpinnerVariant(CharSets[6], 80)
	// Simple is a spinner variant that shows a simple spinner.
	Simple = NewSpinnerVariant(CharSets[7], 120)
	// GrowHV is a spinner variant that grows the spinner horizontally and vertically.
	GrowHV = NewSpinnerVariant(CharSets[8], 80)
	// Arc is a spinner variant that shows an arc.
	Arc = NewSpinnerVariant(CharSets[9], 80)
	// BouncingBar is a spinner variant that shows a bouncing bar.
	BouncingBar = NewSpinnerVariant(CharSets[10], 80)
	// BouncingSimple is a spinner variant that shows a bouncing simple spinner.
	BouncingSimple = NewSpinnerVariant(CharSets[11], 80)
	// MovingDots is a spinner variant that shows moving dots.
	MovingDots = NewSpinnerVariant(CharSets[12], 80)
)
View Source
var CharSets = map[int][]string{
	0: {"▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▁"},
	1: {"▖", "▘", "▝", "▗"},
	2: {"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
	3: {"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
	4: {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
	5: {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"},
	6: {"▉", "▊", "▋", "▌", "▍", "▎", "▏", "▎", "▍", "▌", "▋", "▊", "▉"},

	7:  {".  ", ".. ", "..."},
	8:  {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "▉", "▊", "▋", "▌", "▍", "▎", "▏", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█", "▇", "▆", "▅", "▄", "▃", "▂", "▁"},
	9:  {"◜", "◝", "◞", "◟"},
	10: {"(●    )", "( ●    )", "(  ●   )", "(   ●  )", "(    ● )", "(     ●)", "(    ● )", "(   ●  )", "(  ●   )", "( ●    )"},
	11: {".  ", ".. ", "...", " ..", "  .", "   "},
	12: {"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"},
}

CharSets contains the available character sets

Functions

This section is empty.

Types

type CharSet

type CharSet = []string

CharSet is a type alias for a slice of strings.

type Option

type Option func(s *Spinner)

Option is a function that modifies a Spinner.

func WithCancelKeys

func WithCancelKeys(keys []keys.KeyCode) Option

WithCancelKeys returns an Option function that sets the cancelation keys for the Spinner.

func WithColor

func WithColor(c color.Color) Option

WithColor returns an Option function that sets the color of the spinner.

func WithFinalMsg

func WithFinalMsg(fm string) Option

WithFinalMsg returns an Option function that sets the final message of a Spinner.

func WithPrefix

func WithPrefix(p string) Option

WithPrefix returns an Option function that sets the Prefix field of a Spinner.

func WithSuffix

func WithSuffix(sf string) Option

WithSuffix returns an Option function that sets the suffix of a Spinner.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter takes an io.Writer and sets the spinner output.

func WithWriterFile

func WithWriterFile(f *os.File) Option

WithWriterFile adds the given writer to the spinner.

type Spinner

type Spinner struct {
	CancelKeys []keys.KeyCode
	Writer     io.Writer
	WriterFile *os.File
	Color      color.Color
	FinalMsg   string
	Prefix     string
	Suffix     string
	PreUpdate  func(s *Spinner)
	PostUpdate func(s *Spinner)
	// contains filtered or unexported fields
}

Spinner represents a thread-safe spinner with customizable options such as character sets, prefix, suffix, and color.

func New

func New(variant SpinnerVariant, options ...Option) *Spinner

New creates a new Spinner with the provided CharSet, delay, and options.

func (*Spinner) HideCursor

func (s *Spinner) HideCursor()

HideCursor hides the cursor.

func (*Spinner) Restart

func (s *Spinner) Restart()

Restart stops the spinner and starts it again.

func (*Spinner) ShowCursor

func (s *Spinner) ShowCursor()

ShowCursor shows the cursor.

func (*Spinner) Start

func (s *Spinner) Start()

Start starts the spinner.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop stops the spinner, prints the final message if set, and signals the stop channel.

type SpinnerVariant

type SpinnerVariant struct {
	Interval int
	// contains filtered or unexported fields
}

SpinnerVariant represents a variant of a spinner with a specific character set and interval.

func NewSpinnerVariant

func NewSpinnerVariant(charSet CharSet, interval int) SpinnerVariant

NewSpinnerVariant creates a new SpinnerVariant with the given character set and interval.

Jump to

Keyboard shortcuts

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