spinners

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

README

spinners

Library for displaying terminal spinners in Go.

Usage

package main

import (
	"math/rand"
	"time"

	"git.brokenmouse.studio/bms/go-spinners"
	"git.brokenmouse.studio/bms/go-spinners-ora"
)

func main() {
	spinner := spinners.New(oraSpinners.DOTS)
	spinner.Text = "Spinning ..."

	spinner.Start()

	time.Sleep(time.Second * 2)

	if rand.Intn(2) == 1 {
		spinner.Success()
	} else {
		spinner.Failure()
	}
}

Data

Spinners are easy to create:

spinner := spinners.New(spinners.SpinnerData{
	Frames:   []string{"-", "\\", "|", "/"},
	Interval: time.Millisecond * 100,
})

Alternatively, you can use pre-made spinners:

  • oraSpinners - All spinners from the Node.js package cli-spinners (which ora uses).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Spinner

type Spinner struct {
	// Text that will be printed before the spinner.
	Prefix string
	// Text that will be printed after the spinner but before the text.
	Suffix string
	// Text that will be printed after the end of the line.
	Text string
	// Prefix that will be shown after the spinner has stopped.
	StopPrefix *string
	// Suffix that will be shown after the spinner has stopped.
	StopSuffix *string
	// Text that will be shown after the spinner has stopped.
	StopText *string
	// Text replacing the spinner after it has stopped.
	StopFrame *string
	// contains filtered or unexported fields
}

func New

func New(data SpinnerData) (spinner *Spinner)

Returns a new spinner.

func (*Spinner) ChangeData

func (spinner *Spinner) ChangeData(newData SpinnerData)

Changes the spinner's data.

This can also be called while the spinner is running.

func (*Spinner) Failure

func (spinner *Spinner) Failure()

Stops the spinner, leaving a cross as the last frame.

func (*Spinner) IsSpinning

func (spinner *Spinner) IsSpinning() bool

Returns whether the spinner is spinning. While it is active, the current line will constantly get overwritten.

func (*Spinner) Start

func (spinner *Spinner) Start()

Starts the spinner.

When this function is called while the spinner is already spinning, it will restart the spinner.

func (*Spinner) Stop

func (spinner *Spinner) Stop()

Stops the spinner and blocks until finished.

It's safe to call this when the spinner has already stopped.

func (*Spinner) StopAsync

func (spinner *Spinner) StopAsync()

Stops the spinner without blocking. You shouldn't write while the spinner is still spinning.

Like Stop(), this is safe to call when the spinner has already stopped.

func (*Spinner) StopAsyncAndSignal

func (spinner *Spinner) StopAsyncAndSignal() (stopChan chan struct{})

Stops the spinner without blocking and returns a channel that will be closed when the spinner has stopped.

func (*Spinner) Success

func (spinner *Spinner) Success()

Stops the spinner, leaving a check mark as the last frame.

type SpinnerData

type SpinnerData struct {
	// Every string in the array is a single frame. Frames are played from the beginning to the end of the array.
	Frames []string
	// The time between two frames.
	Interval time.Duration
}

Jump to

Keyboard shortcuts

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