spinner

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 5 Imported by: 2

README

# Spinner

Spinner is a flexible and customizable terminal spinner package for Go.

## Features

- Multiple pre-defined spinner styles
- Customizable frames, colors, and intervals
- Support for fixed and dynamic colors
- Adjustable speed and position
- Prefix and suffix text support
- Easy to use API

## Installation

```bash
go get github.com/tmc/spinner
```

## Quick Start

```go
package main

import (
    "time"
    "github.com/tmc/spinner"
)

func main() {
    s := spinner.New()
    s.Start()
    time.Sleep(3 * time.Second)
    s.Stop()
}

Documentation

Overview

Package spinner provides a customizable terminal spinner for displaying progress or activity.

Index

Examples

Constants

View Source
const (
	Black  = "\033[38;5;0m"
	Green  = "\033[38;5;2m"
	Olive  = "\033[38;5;3m"
	Navy   = "\033[38;5;4m"
	Teal   = "\033[38;5;6m"
	Silver = "\033[38;5;7m"
	Grey   = "\033[38;5;8m"
	Red    = "\033[38;5;9m"
	Lime   = "\033[38;5;10m"
	Yellow = "\033[38;5;11m"
	Blue   = "\033[38;5;12m"
	Aqua   = "\033[38;5;14m"
	White  = "\033[38;5;15m"
	Reset  = "\033[0m"
)

Variables

View Source
var (
	Dots1               = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
	Dots2               = []string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"}
	Dots3               = []string{"⠋", "⠙", "⠚", "⠞", "⠖", "⠦", "⠴", "⠲", "⠳", "⠓"}
	Dots4               = []string{"⠄", "⠆", "⠇", "⠋", "⠙", "⠸", "⠰", "⠠", "⠰", "⠸", "⠙", "⠋", "⠇", "⠆"}
	Dots5               = []string{"⠋", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋"}
	Dots6               = []string{"⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠴", "⠲", "⠒", "⠂", "⠂", "⠒", "⠚", "⠙", "⠉", "⠁"}
	Dots7               = []string{"⠈", "⠉", "⠋", "⠓", "⠒", "⠐", "⠐", "⠒", "⠖", "⠦", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈"}
	Dots8               = []string{"⠁", "⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈", "⠈"}
	Dots9               = []string{"⢹", "⢺", "⢼", "⣸", "⣇", "⡧", "⡗", "⡏"}
	Dots10              = []string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"}
	Dots11              = []string{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"}
	Dots12              = []string{"⢀⠀", "⡀⠀", "⠄⠀", "⢂⠀", "⡂⠀", "⠅⠀", "⢃⠀", "⡃⠀", "⠍⠀", "⢋⠀", "⡋⠀", "⠍⠁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⢈⠩", "⡀⢙", "⠄⡙", "⢂⠩", "⡂⢘", "⠅⡘", "⢃⠨", "⡃⢐", "⠍⡐", "⢋⠠", "⡋⢀", "⠍⡁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⠈⠩", "⠀⢙", "⠀⡙", "⠀⠩", "⠀⢘", "⠀⡘", "⠀⠨", "⠀⢐", "⠀⡐", "⠀⠠", "⠀⢀", "⠀⡀"}
	Line                = []string{"-", "\\", "|", "/"}
	Pipe                = []string{"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"}
	SimpleDots          = []string{".  ", ".. ", "...", "   "}
	SimpleDotsScrolling = []string{".  ", ".. ", "...", " ..", "  .", "   "}
	Star                = []string{"✶", "✸", "✹", "✺", "✹", "✷"}
	Flip                = []string{"_", "_", "_", "-", "`", "`", "'", "´", "-", "_", "_", "_"}
	Hamburger           = []string{"☱", "☲", "☴"}
	GrowVertical        = []string{"▁", "▃", "▄", "▅", "▆", "▇", "▆", "▅", "▄", "▃"}
	GrowHorizontal      = []string{"▏", "▎", "▍", "▌", "▋", "▊", "▉", "▊", "▋", "▌", "▍", "▎"}
	Balloon             = []string{" ", ".", "o", "O", "@", "*", " "}
	Noise               = []string{"▓", "▒", "░"}
	Bounce              = []string{"⠁", "⠂", "⠄", "⠂"}
	BoxBounce           = []string{"▖", "▘", "▝", "▗"}
	BoxBounce2          = []string{"▌", "▀", "▐", "▄"}
	Triangle            = []string{"◢", "◣", "◤", "◥"}
	Arc                 = []string{"◜", "◠", "◝", "◞", "◡", "◟"}
	Circle              = []string{"◡", "⊙", "◠"}
	SquareCorners       = []string{"◰", "◳", "◲", "◱"}
	CircleQuarters      = []string{"◴", "◷", "◶", "◵"}
	CircleHalves        = []string{"◐", "◓", "◑", "◒"}
	Moon                = []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"}
	Smiley              = []string{"😄 ", "😝 "}
	Monkey              = []string{"🙈 ", "🙈 ", "🙉 ", "🙊 "}
	Hearts              = []string{"💛 ", "💙 ", "💜 ", "💚 ", "❤️ "}
	Clock               = []string{"🕛 ", "🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "}
	Earth               = []string{"🌍 ", "🌎 ", "🌏 "}
	Material            = []string{"█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "███▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "████▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "██████▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "██████▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "███████▁▁▁▁▁▁▁▁▁▁▁▁▁", "████████▁▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "██████████▁▁▁▁▁▁▁▁▁▁", "███████████▁▁▁▁▁▁▁▁▁", "█████████████▁▁▁▁▁▁▁", "██████████████▁▁▁▁▁▁", "██████████████▁▁▁▁▁▁", "▁██████████████▁▁▁▁▁", "▁██████████████▁▁▁▁▁", "▁██████████████▁▁▁▁▁", "▁▁██████████████▁▁▁▁", "▁▁▁██████████████▁▁▁", "▁▁▁▁█████████████▁▁▁", "▁▁▁▁██████████████▁▁", "▁▁▁▁██████████████▁▁", "▁▁▁▁▁██████████████▁", "▁▁▁▁▁██████████████▁", "▁▁▁▁▁██████████████▁", "▁▁▁▁▁▁██████████████", "▁▁▁▁▁▁██████████████", "▁▁▁▁▁▁▁█████████████", "▁▁▁▁▁▁▁█████████████", "▁▁▁▁▁▁▁▁████████████", "▁▁▁▁▁▁▁▁████████████", "▁▁▁▁▁▁▁▁▁███████████", "▁▁▁▁▁▁▁▁▁███████████", "▁▁▁▁▁▁▁▁▁▁██████████", "▁▁▁▁▁▁▁▁▁▁██████████", "▁▁▁▁▁▁▁▁▁▁▁▁████████", "▁▁▁▁▁▁▁▁▁▁▁▁▁███████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁██████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█████", "█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁████", "██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁███", "██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁███", "███▁▁▁▁▁▁▁▁▁▁▁▁▁▁███", "████▁▁▁▁▁▁▁▁▁▁▁▁▁▁██", "█████▁▁▁▁▁▁▁▁▁▁▁▁▁▁█", "█████▁▁▁▁▁▁▁▁▁▁▁▁▁▁█", "██████▁▁▁▁▁▁▁▁▁▁▁▁▁█", "████████▁▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "█████████▁▁▁▁▁▁▁▁▁▁▁", "███████████▁▁▁▁▁▁▁▁▁", "████████████▁▁▁▁▁▁▁▁", "████████████▁▁▁▁▁▁▁▁", "██████████████▁▁▁▁▁▁", "██████████████▁▁▁▁▁▁", "▁██████████████▁▁▁▁▁", "▁██████████████▁▁▁▁▁", "▁▁▁█████████████▁▁▁▁", "▁▁▁▁▁████████████▁▁▁", "▁▁▁▁▁████████████▁▁▁", "▁▁▁▁▁▁███████████▁▁▁", "▁▁▁▁▁▁▁▁█████████▁▁▁", "▁▁▁▁▁▁▁▁█████████▁▁▁", "▁▁▁▁▁▁▁▁▁█████████▁▁", "▁▁▁▁▁▁▁▁▁█████████▁▁", "▁▁▁▁▁▁▁▁▁▁█████████▁", "▁▁▁▁▁▁▁▁▁▁▁████████▁", "▁▁▁▁▁▁▁▁▁▁▁████████▁", "▁▁▁▁▁▁▁▁▁▁▁▁███████▁", "▁▁▁▁▁▁▁▁▁▁▁▁███████▁", "▁▁▁▁▁▁▁▁▁▁▁▁▁███████", "▁▁▁▁▁▁▁▁▁▁▁▁▁███████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁████", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁███", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁███", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"}
)

Spinner styles

Functions

func Color256

func Color256(n int) string

Color256 returns the ANSI escape sequence for a 256-color.

func ColorPulse

func ColorPulse(start, end int, duration time.Duration) func() string

func GreyPulse

func GreyPulse(interval time.Duration) func() string

func SpeedupInterval added in v0.1.1

func SpeedupInterval(start, end, duration time.Duration) func() time.Duration

func WithColor

func WithColor(color string) func(*Spinner)

WithColor sets a fixed color for the spinner.

func WithColorFunc

func WithColorFunc(f func() string) func(*Spinner)

WithColorFunc sets a function to determine the color for the spinner.

func WithHideCursor

func WithHideCursor(hide bool) func(*Spinner)

WithHideCursor sets whether to hide the cursor while the spinner is active.

func WithIntervalFunc

func WithIntervalFunc(f func() time.Duration) func(*Spinner)

WithIntervalFunc sets a function to determine the interval for the spinner.

Types

type Option

type Option func(*Spinner)

Option is a function that configures a Spinner.

func WithFrames

func WithFrames(frames []string) Option

WithFrames sets the frames for the spinner.

func WithInterval

func WithInterval(d time.Duration) Option

WithInterval sets a fixed interval for the spinner.

func WithPosition added in v0.1.2

func WithPosition(pos int) Option

WithPosition sets the position of the spinner.

func WithPrefix added in v0.1.4

func WithPrefix(prefix string) Option

WithPrefix sets a prefix for the spinner.

func WithSuffix added in v0.1.4

func WithSuffix(suffix string) Option

WithSuffix sets a suffix for the spinner.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the writer for the spinner.

type Spinner

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

Spinner represents a terminal spinner.

Example (Basic)
package main

import (
	"time"

	"github.com/tmc/spinner"
)

func main() {
	s := spinner.New()
	s.Start()
	time.Sleep(time.Second)
	s.Stop()
}
Example (WithAdvancedOptions)
package main

import (
	"time"

	"github.com/tmc/spinner"
)

func main() {
	s := spinner.New(
		spinner.WithFrames(spinner.Dots8),
		spinner.WithIntervalFunc(
			spinner.SpeedupInterval(90*time.Millisecond, 40*time.Millisecond, time.Second*5),
		),
		spinner.WithColorFunc(spinner.GreyPulse(15*time.Millisecond)),
	)
	s.Start()
	time.Sleep(5 * time.Second)
	s.Stop()
}
Example (WithCustomFrames)
package main

import (
	"time"

	"github.com/tmc/spinner"
)

func main() {
	s := spinner.New(spinner.WithFrames(spinner.Dots12))
	s.Start()
	time.Sleep(3 * time.Second)
	s.Stop()
}
Example (WithCustomFramesAllDots)
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/tmc/spinner"
)

func main() {
	for i, f := range [][]string{
		spinner.Dots1,
		spinner.Dots2,
		spinner.Dots3,
		spinner.Dots4,
		spinner.Dots5,
		spinner.Dots6,
		spinner.Dots7,
		spinner.Dots8,
		spinner.Dots9,
		spinner.Dots10,
		spinner.Dots11,
		spinner.Dots12,
	} {
		s := spinner.New(spinner.WithFrames(f))
		fmt.Fprintf(os.Stderr, "spinner.Dots%v\n", i+1)
		s.Start()
		time.Sleep(2 * time.Second)
		s.Stop()
		fmt.Fprintf(os.Stderr, "\n")
	}
}
Example (WithPrefixOptions)
package main

import (
	"time"

	"github.com/tmc/spinner"
)

func main() {
	prefixes := []string{
		"🚀 ",
		"\033[31mError:\033[0m ",
		"Loading: ",
	}
	for _, prefix := range prefixes {
		s := spinner.New(
			spinner.WithPrefix(prefix),
			spinner.WithFrames([]string{"▁", "▂", "▃"}),
			spinner.WithInterval(20*time.Millisecond),
		)
		s.Start()
		time.Sleep(900 * time.Millisecond)
		s.Stop()
	}
}

func New

func New(opts ...Option) *Spinner

New creates a new Spinner with the given options.

func (*Spinner) Start

func (s *Spinner) Start()

Start begins the spinner animation.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop halts the spinner animation.

Jump to

Keyboard shortcuts

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