spinner

package module
v0.0.0-...-383070f Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2020 License: MIT Imports: 11 Imported by: 0

README

🏵️ Go Console Spinner

UPD. development frozen

*** COMING SOON ***

Build Status Go Report Card Coverage Status GitHub

API may be a subject to change

Features
  • highly configurable (options)

  • progress indication during spin spinner.Progress(0.5)50%

  • messages during spin spinner.Message("message")

  • configurable elements order - chars, messages and progress

  • separated format settings for chars, messages and progress

  • hides cursor on spinner.Start(), shows on spinner.Stop()

  • cursor hide can be disabled spinner.HideCursor(false)

  • has Erase() method

  • has Current() method to write current frame again for smooth animation

  • final message

  • supports pipe | and redirect > output

  • separated color settings for chars, messages and progress

  • has Disable() and Enable() methods (questionable)

It's a proof of concept and kinda port of alecrabbit/php-console-spinner

For now you can try it as is and shape it's development if you wish

Xterm terminal(package uses ANSI codes)

Works on Windows too! Thanks to mattn/go-colorable

Examples
Quickstart
package main

import (
    "time"

    "github.com/alecrabbit/go-cli-spinner"
)

func main() {
    s, _ := spinner.New()
    // Start spinner
    s.Start()
    // Doing some work
    time.Sleep(3 * time.Second)
    // Set current message
    s.Message("Current message")
    // Set current progress
    s.Progress(0.511)
    // Doing some work
    time.Sleep(3 * time.Second)
    // Stop spinner
    s.Stop()
}
Usage

Documentation

Overview

Package spinner implements a colorful console spinner

Index

Constants

View Source
const (
	BlockVertical int = iota
	// Arrows
	BouncingBlock
	Blink
	FlyingLine
	RotatingCircle
	Clock
	HalfClock
	HalfClock2
	Snake
	Snake2
	FlyingDots
	Dots10
	Dots13
	Dots14
	BlockHorizontal
	Toggle
	ToggleSmall
	Arrows01
	Arrows02
	Arrows03
	Arrows04
	Dots21
	Dots22
	Dots23
	Dots24
	Dots25
	Dots26
	Dev
	Dev2
	// Weather
	Simple
)

Declared spinner variants

View Source
const (
	// Char represents char element
	Char = 1 + iota
	// Message represents message element
	Message
	// Progress represents progress element
	Progress
)
View Source
const Line = Simple

Line is alias for Simple

Variables

View Source
var CharSets = map[int]settings{

	Arrows01: {
		120 * time.Millisecond,
		[]string{"←", "↑", "→", "↓"},
		&defaultPalette,
	},
	Arrows02: {
		120 * time.Millisecond,
		[]string{"↖", "↗", "↘", "↙"},
		&defaultPalette,
	},
	Arrows03: {
		120 * time.Millisecond,
		[]string{"⇐", "⇖", "⇑", "⇗", "⇒", "⇘", "⇓", "⇙"},
		&defaultPalette,
	},
	Arrows04: {
		120 * time.Millisecond,
		[]string{"▹▹▹▹▹", "▸▹▹▹▹", "▹▸▹▹▹", "▹▹▸▹▹", "▹▹▹▸▹", "▹▹▹▹▸"},
		&defaultPalette,
	},
	Simple: {
		120 * time.Millisecond,
		[]string{"|", "\\", "─", "/"},
		&defaultPalette,
	},
	Dev: {
		400 * time.Millisecond,
		[]string{"+"},
		&defaultPalette,
	},
	Dev2: {
		250 * time.Millisecond,
		[]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
		&defaultPalette,
	},
	BlockVertical: {
		120 * time.Millisecond,
		[]string{"▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▁"},
		&defaultPalette,
	},
	BlockHorizontal: {
		120 * time.Millisecond,
		[]string{"▉", "▊", "▋", "▌", "▍", "▎", "▏", "▎", "▍", "▌", "▋", "▊", "▉"},
		&defaultPalette,
	},
	BouncingBlock: {
		120 * time.Millisecond,
		[]string{"▖", "▘", "▝", "▗"},
		&defaultPalette,
	},
	RotatingCircle: {
		120 * time.Millisecond,
		[]string{"◐", "◓", "◑", "◒"},
		&defaultPalette,
	},
	Snake: {
		150 * time.Millisecond,
		[]string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
		&defaultPalette,
	},
	Snake2: {
		120 * time.Millisecond,
		[]string{"⠏", "⠛", "⠹", "⢸", "⣰", "⣤", "⣆", "⡇"},
		&defaultPalette,
	},
	FlyingDots: {
		120 * time.Millisecond,
		[]string{
			"⢀⠀", "⡀⠀", "⠄⠀", "⢂⠀", "⡂⠀", "⠅⠀", "⢃⠀", "⡃⠀", "⠍⠀", "⢋⠀", "⡋⠀", "⠍⠁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉",
			"⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⢈⠩", "⡀⢙", "⠄⡙", "⢂⠩", "⡂⢘", "⠅⡘", "⢃⠨", "⡃⢐", "⠍⡐", "⢋⠠", "⡋⢀", "⠍⡁",
			"⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⠈⠩", "⠀⢙", "⠀⡙", "⠀⠩", "⠀⢘", "⠀⡘", "⠀⠨",
			"⠀⢐", "⠀⡐", "⠀⠠", "⠀⢀", "⠀⡀",
		},
		&defaultPalette,
	},
	FlyingLine: {
		120 * time.Millisecond,
		[]string{"|   ", " |  ", "  | ", "   |", "   |", "  | ", " |  ", "|   "},
		&defaultPalette,
	},
	Dots10: {
		120 * time.Millisecond,
		[]string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
		&defaultPalette,
	},
	Dots13: {
		120 * time.Millisecond,
		[]string{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
		&defaultPalette,
	},
	Dots14: {
		120 * time.Millisecond,
		[]string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
		&defaultPalette,
	},
	Dots21: {
		120 * time.Millisecond,
		[]string{
			"⠁", "⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄",
			"⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈", "⠈",
		},
		&defaultPalette,
	},
	Dots22: {
		120 * time.Millisecond,
		[]string{
			"⠈", "⠉", "⠋", "⠓", "⠒", "⠐", "⠐", "⠒", "⠖", "⠦", "⠤", "⠠",
			"⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈",
		},
		&defaultPalette,
	},
	Dots26: {
		120 * time.Millisecond,
		[]string{"⢹", "⢺", "⢼", "⣸", "⣇", "⡧", "⡗", "⡏"},
		&defaultPalette,
	},
	Blink: {
		200 * time.Millisecond,
		[]string{"▓", "▒", "░"},
		&defaultPalette,
	},
	Toggle: {
		250 * time.Millisecond,
		[]string{"■", "□"},
		&defaultPalette,
	},
	Dots23: {
		120 * time.Millisecond,
		[]string{
			"⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄",
			"⠄", "⠤", "⠴", "⠲", "⠒", "⠂", "⠂", "⠒", "⠚", "⠙", "⠉", "⠁",
		},
		&defaultPalette,
	},
	Dots24: {
		120 * time.Millisecond,
		[]string{".  ", ".. ", "...", " ..", "  .", "   "},
		&defaultPalette,
	},
	Dots25: {
		120 * time.Millisecond,
		[]string{"⠋", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋"},
		&defaultPalette,
	},
}

CharSets contains the available character sets

Functions

This section is empty.

Types

type Option

type Option func(*Spinner) error

Option type for functional options

func CharSet

func CharSet(c []string) Option

CharSet sets spinner char set

func ColorLevel

func ColorLevel(cl color.Level) Option

ColorLevel sets color level support for spinner - TNoColor, TColor16, TColor256, TTrueColor

func FinalMessage

func FinalMessage(m string) Option

FinalMessage sets spinner's final message

func Format

func Format(f string) Option

Format sets spinner format

func HideCursor

func HideCursor(h bool) Option

HideCursor sets spinner's hideCursor flag

func Interval

func Interval(d time.Duration) Option

Interval sets interval between spinner refreshes

func MaxMessageLength

func MaxMessageLength(l int) Option

MaxMessageLength sets spinner's max message length

func MessageEllipsis

func MessageEllipsis(l string) Option

MessageEllipsis sets spinner's messageEllipsis

func MessageFormat

func MessageFormat(f string) Option

MessageFormat sets spinner message format

func Order

func Order(o ...int) Option

Order sets spinner elements order

func Prefix

func Prefix(p string) Option

Prefix sets spinner prefix

func ProgressFormat

func ProgressFormat(f string) Option

ProgressFormat sets spinner progress indicator format

func ProgressIndicatorFormat

func ProgressIndicatorFormat(f string) Option

ProgressIndicatorFormat sets spinner progress indicator format

func Reverse

func Reverse() Option

Reverse sets spinner's flag to rotate in reverse

func Variant

func Variant(v int) Option

Variant sets spinner variant

type Spinner

type Spinner struct {
	Writer io.Writer //
	// contains filtered or unexported fields
}

Spinner struct representing spinner instance

func New

func New(options ...Option) (*Spinner, error)

New provides a pointer to an instance of Spinner

func (*Spinner) Active

func (s *Spinner) Active() bool

Active returns true if spinner is currently active

func (*Spinner) Current

func (s *Spinner) Current()

Current writes spinner current frame to output represented by spinner writer

func (*Spinner) Erase

func (s *Spinner) Erase()

Erase erases spinner output

func (*Spinner) Message

func (s *Spinner) Message(m string)

Message sets spinner message

func (*Spinner) Progress

func (s *Spinner) Progress(p float32)

Progress sets spinner progress value 0..1 → 0%..100%

func (*Spinner) Start

func (s *Spinner) Start()

Start will start the spinner

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop stops the spinner

Directories

Path Synopsis
Package auxiliary contains auxiliary constants, functions and variables
Package auxiliary contains auxiliary constants, functions and variables
examples

Jump to

Keyboard shortcuts

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