spin

package module
v0.0.0-...-5aad845 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 16 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 Spinner

type Spinner struct {
	Writer     io.Writer
	WriterFile *os.File

	PreUpdate  func(s *Spinner)
	PostUpdate func(s *Spinner)
	FinalMsg   string
	Prefix     string
	Suffix     string

	CancelKeys []keys.KeyCode

	Color color.Color
	// contains filtered or unexported fields
}

Spinner represents a thread-safe spinner (s *Spinner) Set customizable s such as character sets, prefix, suffix, and color.

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) SetCancelKeys

func (s *Spinner) SetCancelKeys(keys []keys.KeyCode)

SetCancelKeys returns an function that sets the cancelation keys for the Spinner.

func (*Spinner) SetColor

func (s *Spinner) SetColor(c color.Color)

SetColor sets the color of the spinner.

func (*Spinner) SetFinalMsg

func (s *Spinner) SetFinalMsg(fm string)

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

func (*Spinner) SetPrefix

func (s *Spinner) SetPrefix(p string)

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

func (*Spinner) SetSuffix

func (s *Spinner) SetSuffix(sf string)

SetSuffix returns an function that sets the suffix of a Spinner.

func (*Spinner) SetWriter

func (s *Spinner) SetWriter(w io.Writer)

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

func (*Spinner) SetWriterFile

func (s *Spinner) SetWriterFile(f *os.File)

SetWriterFile adds the given writer to the spinner.

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 SpinnerBuilder

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

SpinnerBuilder is a builder for the spinner.

func New

func New() *SpinnerBuilder

New returns a new spinner builder.

func (*SpinnerBuilder) Build

func (s *SpinnerBuilder) Build() *Spinner

Build builds a new spinner with the given options.

func (*SpinnerBuilder) WithCancelKeys

func (s *SpinnerBuilder) WithCancelKeys(cancelKeys []keys.KeyCode) *SpinnerBuilder

WithCancelKeys sets the keys that can cancel the spinner.

func (*SpinnerBuilder) WithColor

func (s *SpinnerBuilder) WithColor(color color.Color) *SpinnerBuilder

WithColor sets the color of the spinner.

func (*SpinnerBuilder) WithFinalMsg

func (s *SpinnerBuilder) WithFinalMsg(finalMsg string) *SpinnerBuilder

WithFinalMsg sets the final message of the spinner.

func (*SpinnerBuilder) WithPostUpdate

func (s *SpinnerBuilder) WithPostUpdate(postUpdate func(s *Spinner)) *SpinnerBuilder

WithPostUpdate sets the post update function of the spinner.

func (*SpinnerBuilder) WithPreUpdate

func (s *SpinnerBuilder) WithPreUpdate(preUpdate func(s *Spinner)) *SpinnerBuilder

WithPreUpdate sets the pre update function of the spinner.

func (*SpinnerBuilder) WithPrefix

func (s *SpinnerBuilder) WithPrefix(prefix string) *SpinnerBuilder

WithPrefix sets the prefix of the spinner.

func (*SpinnerBuilder) WithSuffix

func (s *SpinnerBuilder) WithSuffix(suffix string) *SpinnerBuilder

WithSuffix sets the suffix of the spinner.

func (*SpinnerBuilder) WithVariant

func (s *SpinnerBuilder) WithVariant(variant SpinnerVariant) *SpinnerBuilder

WithVariant sets the variant of the spinner.

func (*SpinnerBuilder) WithWriter

func (s *SpinnerBuilder) WithWriter(writer io.Writer) *SpinnerBuilder

WithWriter sets the writer of the spinner.

func (*SpinnerBuilder) WithWriterFile

func (s *SpinnerBuilder) WithWriterFile(writerFile *os.File) *SpinnerBuilder

WithWriterFile sets the writer file of the spinner.

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.

func (*SpinnerVariant) All

func (v *SpinnerVariant) All() iter.Seq[string]

Jump to

Keyboard shortcuts

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