termloader

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: MIT Imports: 16 Imported by: 1

README

Termloader

Build Status GoDoc

Termloader is a simple library to add a loading screen to your terminal applications. Termloader will render the loader at the center of your terminal screen. Currently termloader is supported only on *nix operating systems.

Installation

go get github.com/bharath-srinivas/termloader

Example Usage

package main

import (
	"time"

	"github.com/bharath-srinivas/termloader"
)

func main() {
	loader := termloader.New(termloader.CharsetConfigs["default"]) // construct a new loader with config

	loader.Start() // start the loader
	time.Sleep(5 * time.Second) // sleep for sometime to simulate a task
	loader.Stop() // stop the loader
}

Loader color

loader.Color = termloader.Green // provide a color for the loader (white if not provided)

Provide a loading text

loader.Text = "Loading" // provide a text to show above the loader

Loading text color

loadingText := termloader.ColorString("Now Loading", termloader.Green) // color the string
loader.Text = loadingText // provide the colored string as loading text

Custom delay

loader.Delay = 100 * time.Millisecond // delay in milliseconds

Loading image

Termloader supports only jpeg and png formats as of now. Support for more formats might be added later.

loader.Image.SetPath("/path/to/image") // provide the path of the loading image
loader.Image.SetWidth(55) // set custom width for the image
loader.Image.SetHeight(15) // set custom height for the image
loader.Image.Sharpen(6.5) // sharpens the image

Provide your own character set for loader

charsetConfig := termloader.CharsetConfig{Charset: []string{"|", "/", "-", "\\"}, Delay: 100 * time.Millisecond}
loader := termloader.New(charsetConfig)

Todo

  • Loader
  • Optional loading text support
  • Optional image/icon support
  • Add a gif

License

MIT

Documentation

Index

Constants

View Source
const (
	None    = 0
	Red     = 31
	Green   = 32
	Yellow  = 33
	Blue    = 34
	Magenta = 35
	Cyan    = 36
	Gray    = 37
)

Color constants.

Variables

View Source
var CharsetConfigs = map[string]CharsetConfig{
	"default": {
		[]string{"▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒", "█▒▒▒▒▒▒▒▒▒▒▒▒▒▒", "████▒▒▒▒▒▒▒▒▒▒▒", "███████▒▒▒▒▒▒▒▒", "██████████▒▒▒▒▒", "█████████████▒▒", "███████████████", "▒▒█████████████", "▒▒▒▒▒██████████", "▒▒▒▒▒▒▒▒███████", "▒▒▒▒▒▒▒▒▒▒▒████", "▒▒▒▒▒▒▒▒▒▒▒▒▒▒█"},
		100 * time.Millisecond,
	},
	"dots": {
		[]string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
		80 * time.Millisecond,
	},
	"dots2": {
		[]string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
		80 * time.Millisecond,
	},
	"dots3": {
		[]string{"⠋", "⠙", "⠚", "⠞", "⠖", "⠦", "⠴", "⠲", "⠳", "⠓"},
		80 * time.Millisecond,
	},
	"dots4": {
		[]string{"⠄", "⠆", "⠇", "⠋", "⠙", "⠸", "⠰", "⠠", "⠰", "⠸", "⠙", "⠋", "⠇", "⠆"},
		80 * time.Millisecond,
	},
	"dots5": {
		[]string{"⠋", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋"},
		80 * time.Millisecond,
	},
	"dots6": {
		[]string{"⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠴", "⠲", "⠒", "⠂", "⠂", "⠒", "⠚", "⠙", "⠉", "⠁"},
		80 * time.Millisecond,
	},
	"dots7": {
		[]string{"⠈", "⠉", "⠋", "⠓", "⠒", "⠐", "⠐", "⠒", "⠖", "⠦", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈"},
		80 * time.Millisecond,
	},
	"dots8": {
		[]string{"⠁", "⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈", "⠈"},
		80 * time.Millisecond,
	},
	"dots9": {
		[]string{"⢹", "⢺", "⢼", "⣸", "⣇", "⡧", "⡗", "⡏"},
		80 * time.Millisecond,
	},
	"dots10": {
		[]string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
		80 * time.Millisecond,
	},
	"dots11": {
		[]string{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
		100 * time.Millisecond,
	},
	"dots12": {
		[]string{"⢀⠀", "⡀⠀", "⠄⠀", "⢂⠀", "⡂⠀", "⠅⠀", "⢃⠀", "⡃⠀", "⠍⠀", "⢋⠀", "⡋⠀", "⠍⠁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⢈⠩", "⡀⢙", "⠄⡙", "⢂⠩", "⡂⢘", "⠅⡘", "⢃⠨", "⡃⢐", "⠍⡐", "⢋⠠", "⡋⢀", "⠍⡁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⠈⠩", "⠀⢙", "⠀⡙", "⠀⠩", "⠀⢘", "⠀⡘", "⠀⠨", "⠀⢐", "⠀⡐", "⠀⠠", "⠀⢀", "⠀⡀"},
		80 * time.Millisecond,
	},
	"line": {
		[]string{"-", "\\", "|", "/"},
		130 * time.Millisecond,
	},
	"line2": {
		[]string{"⠂", "-", "–", "—", "–", "-"},
		100 * time.Millisecond,
	},
	"line3": {
		[]string{"|", "||", "|||", "||||", "|||||", "|||||||", "||||||||", "|||||||", "||||||", "|||||", "||||", "|||", "||", "|"},
		100 * time.Millisecond,
	},
	"pipe": {
		[]string{"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"},
		100 * time.Millisecond,
	},
	"simpleDots": {
		[]string{".  ", ".. ", "...", "   "},
		400 * time.Millisecond,
	},
	"simpleDotsScrolling": {
		[]string{".  ", ".. ", "...", " ..", "  .", "   "},
		200 * time.Millisecond,
	},
	"star": {
		[]string{"✶", "✸", "✹", "✺", "✹", "✷"},
		70 * time.Millisecond,
	},
	"star2": {
		[]string{"+", "x", "*"},
		80 * time.Millisecond,
	},
	"star3": {
		[]string{"(*----------)", "(-*---------)", "(--*--------)", "(---*-------)", "(----*------)", "(-----*-----)", "(------*----)", "(-------*---)", "(--------*--)", "(---------*-)", "(----------*)"},
		100 * time.Millisecond,
	},
	"flip": {
		[]string{"_", "_", "_", "-", "`", "`", "'", "´", "-", "_", "_", "_"},
		70 * time.Millisecond,
	},
	"hamburger": {
		[]string{"☱", "☲", "☴"},
		100 * time.Millisecond,
	},
	"growVertical": {
		[]string{"▁", "▃", "▄", "▅", "▆", "▇", "▆", "▅", "▄", "▃"},
		120 * time.Millisecond,
	},
	"growHorizontal": {
		[]string{"▏", "▎", "▍", "▌", "▋", "▊", "▉", "▊", "▋", "▌", "▍", "▎"},
		120 * time.Millisecond,
	},
	"balloon": {
		[]string{" ", ".", "o", "O", "@", "*", " "},
		120 * time.Millisecond,
	},
	"balloon2": {
		[]string{".", "o", "O", "°", "O", "o", "."},
		120 * time.Millisecond,
	},
	"noise": {
		[]string{"▓", "▒", "░"},
		100 * time.Millisecond,
	},
	"bounce": {
		[]string{"⠁", "⠂", "⠄", "⠂"},
		120 * time.Millisecond,
	},
	"boxBounce": {
		[]string{"▖", "▘", "▝", "▗"},
		120 * time.Millisecond,
	},
	"boxBounce2": {
		[]string{"▌", "▀", "▐", "▄"},
		100 * time.Millisecond,
	},
	"triangle": {
		[]string{"◢", "◣", "◤", "◥"},
		50 * time.Millisecond,
	},
	"arc": {
		[]string{"◜", "◠", "◝", "◞", "◡", "◟"},
		100 * time.Millisecond,
	},
	"circle": {
		[]string{"◡", "⊙", "◠"},
		120 * time.Millisecond,
	},
	"squareCorners": {
		[]string{"◰", "◳", "◲", "◱"},
		180 * time.Millisecond,
	},
	"circleQuarters": {
		[]string{"◴", "◷", "◶", "◵"},
		120 * time.Millisecond,
	},
	"circleHalves": {
		[]string{"◐", "◓", "◑", "◒"},
		50 * time.Millisecond,
	},
	"squish": {
		[]string{"╫", "╪"},
		100 * time.Millisecond,
	},
	"toggle": {
		[]string{"⊶", "⊷"},
		250 * time.Millisecond,
	},
	"toggle2": {
		[]string{"▫", "▪"},
		80 * time.Millisecond,
	},
	"toggle3": {
		[]string{"□", "■"},
		120 * time.Millisecond,
	},
	"toggle4": {
		[]string{"■", "□", "▪", "▫"},
		100 * time.Millisecond,
	},
	"toggle5": {
		[]string{"▮", "▯"},
		100 * time.Millisecond,
	},
	"toggle6": {
		[]string{"ဝ", "၀"},
		300 * time.Millisecond,
	},
	"toggle7": {
		[]string{"⦾", "⦿"},
		80 * time.Millisecond,
	},
	"toggle8": {
		[]string{"◍", "◌"},
		100 * time.Millisecond,
	},
	"toggle9": {
		[]string{"◉", "◎"},
		100 * time.Millisecond,
	},
	"toggle10": {
		[]string{"㊂", "㊀", "㊁"},
		100 * time.Millisecond,
	},
	"toggle11": {
		[]string{"⧇", "⧆"},
		50 * time.Millisecond,
	},
	"toggle12": {
		[]string{"☗", "☖"},
		120 * time.Millisecond,
	},
	"toggle13": {
		[]string{"=", "*", "-"},
		80 * time.Millisecond,
	},
	"arrow": {
		[]string{"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
		100 * time.Millisecond,
	},
	"arrow2": {
		[]string{"⬆️ ", "↗️ ", "➡️ ", "↘️ ", "⬇️ ", "↙️ ", "⬅️ ", "↖️ "},
		80 * time.Millisecond,
	},
	"arrow3": {
		[]string{"▹▹▹▹▹", "▸▹▹▹▹", "▹▸▹▹▹", "▹▹▸▹▹", "▹▹▹▸▹", "▹▹▹▹▸"},
		120 * time.Millisecond,
	},
	"arrow4": {
		[]string{">>--->", " >>--->", "  >>--->", "   >>--->", "    >>--->", "    <---<<", "   <---<<", "  <---<<", " <---<<", "<---<<"},
		100 * time.Millisecond,
	},
	"bouncingBar": {
		[]string{"[    ]", "[=   ]", "[==  ]", "[=== ]", "[ ===]", "[  ==]", "[   =]", "[    ]", "[   =]", "[  ==]", "[ ===]", "[====]", "[=== ]", "[==  ]", "[=   ]"},
		80 * time.Millisecond,
	},
	"bouncingBall": {
		[]string{"( ●    )", "(  ●   )", "(   ●  )", "(    ● )", "(     ●)", "(    ● )", "(   ●  )", "(  ●   )", "( ●    )", "(●     )"},
		80 * time.Millisecond,
	},
	"smiley": {
		[]string{"😄 ", "😝 "},
		200 * time.Millisecond,
	},
	"moon": {
		[]string{"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "},
		80 * time.Millisecond,
	},
	"pong": {
		[]string{"▐⠂       ▌", "▐⠈       ▌", "▐ ⠂      ▌", "▐ ⠠      ▌", "▐  ⡀     ▌", "▐  ⠠     ▌", "▐   ⠂    ▌", "▐   ⠈    ▌", "▐    ⠂   ▌", "▐    ⠠   ▌", "▐     ⡀  ▌", "▐     ⠠  ▌", "▐      ⠂ ▌", "▐      ⠈ ▌", "▐       ⠂▌", "▐       ⠠▌", "▐       ⡀▌", "▐      ⠠ ▌", "▐      ⠂ ▌", "▐     ⠈  ▌", "▐     ⠂  ▌", "▐    ⠠   ▌", "▐    ⡀   ▌", "▐   ⠠    ▌", "▐   ⠂    ▌", "▐  ⠈     ▌", "▐  ⠂     ▌", "▐ ⠠      ▌", "▐ ⡀      ▌", "▐⠠       ▌"},
		80 * time.Millisecond,
	},
	"shark": {
		[]string{"▐|\\____________▌", "▐_|\\___________▌", "▐__|\\__________▌", "▐___|\\_________▌", "▐____|\\________▌", "▐_____|\\_______▌", "▐______|\\______▌", "▐_______|\\_____▌", "▐________|\\____▌", "▐_________|\\___▌", "▐__________|\\__▌", "▐___________|\\_▌", "▐____________|\\▌", "▐____________/|▌", "▐___________/|_▌", "▐__________/|__▌", "▐_________/|___▌", "▐________/|____▌", "▐_______/|_____▌", "▐______/|______▌", "▐_____/|_______▌", "▐____/|________▌", "▐___/|_________▌", "▐__/|__________▌", "▐_/|___________▌", "▐/|____________▌"},
		120 * time.Millisecond,
	},
	"dqpb": {
		[]string{"d", "q", "p", "b"},
		100 * time.Millisecond,
	},
	"grenade": {
		[]string{"،   ", "′   ", " ´ ", " ‾ ", "  ⸌", "  ⸊", "  |", "  ⁎", "  ⁕", " ෴ ", "  ⁓", "   ", "   ", "   "},
		80 * time.Millisecond,
	},
	"point": {
		[]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"},
		125 * time.Millisecond,
	},
	"layer": {
		[]string{"-", "=", "≡"},
		150 * time.Millisecond,
	},
	"betaWave": {
		[]string{"ρββββββ", "βρβββββ", "ββρββββ", "βββρβββ", "ββββρββ", "βββββρβ", "ββββββρ"},
		80 * time.Millisecond,
	},
}

Predefined charset configurations for the loader.

Functions

func ColorString

func ColorString(str string, color int) string

ColorString will wrap the provided string with ANSI escape sequences for color and return the colored string.

Types

type Braille added in v0.2.0

type Braille [2][4]int

Braille config

func (Braille) Rune added in v0.2.0

func (b Braille) Rune() rune

Rune maps each point in braille to a dot identifier and calculates the corresponding unicode symbol.

func (Braille) String added in v0.2.0

func (b Braille) String() string

String returns a unicode braille character.

type CharsetConfig added in v0.2.0

type CharsetConfig struct {
	Charset []string
	Delay   time.Duration
}

Charset config type

type Filters added in v0.2.0

type Filters struct {
	Sharpness float64 // Sharpness
}

Image filters

type Image added in v0.2.0

type Image struct {
	Path    string    // Image path
	Filters *Filters  // Image Filters
	Writer  io.Writer // Stdout
	// contains filtered or unexported fields
}

Image config

func (*Image) Render added in v0.2.0

func (i *Image) Render(tWidth, tHeight, offset int)

Renders the image at the center of the stdout depending the provided terminal width and height of the terminal. The offset can be used to adjust the vertical positioning of the image. It'll panic if any error occurs while rendering the image.

func (*Image) SetHeight added in v0.2.0

func (i *Image) SetHeight(height int)

SetHeight sets the given custom image height to the image config.

func (*Image) SetPath added in v0.2.0

func (i *Image) SetPath(path string)

SetPath sets the provided image path to the image config.

func (*Image) SetWidth added in v0.2.0

func (i *Image) SetWidth(width int)

SetWidth sets the given custom image width to the image config.

func (*Image) Sharpen added in v0.2.0

func (i *Image) Sharpen(sigma float64)

Sharpen sharpens the image by factor of given sigma value. Sigma must be a positive value.

type Loader

type Loader struct {
	Image  *Image        // Loading image
	Color  int           // Color of the loader
	Delay  time.Duration // Animation speed of the loader
	Text   string        // Text to be displayed above the loader
	Writer io.Writer     // Stdout
	// contains filtered or unexported fields
}

Loader config.

func New

func New(charsetConfig CharsetConfig) *Loader

New returns a pointer to the Loader interface with provided options. Default loader color will be white.

func (*Loader) Start

func (l *Loader) Start()

Starts the loader.

func (*Loader) Stop

func (l *Loader) Stop()

Stops the loader.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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