Help us shape the future of Go! Take the 2026 Go Developer Survey

pentimento

package module
v0.0.0-...-ade47d8 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 7 Imported by: 3

README

Pentimento

GoDoc Build Status Maintainability Test Coverage

Pentimento is a utility to help show progress events in the terminal.

Example

Here is a complete working example. Each iteration of the loop overwrites the previous output from the printer so only the current information stays on-screen.

This pattern can be adapted to show outstanding tasks, current external event progress, or multiple progress bars (à la docker pull). The implementation uses ANSI codes, so beware of backing writers that do not support them.

See the file main.go in the example directory of this project for a working example.

The output of this example can be viewed here:

asciicast

License

Copyright (c) 2018 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const DefaultInterval = time.Millisecond * 250

Variables

View Source
var (
	SpinnerStrings       = []string{"/", "-", "\\", "|"}
	SpinnerXStrings      = []string{"+", "x"}
	SpinnerVStrings      = []string{"v", "<", "^", ">"}
	ThrobberStrings      = []string{".", "o", "O", "o"}
	BalloonStrings       = []string{".", "o", "O", "@", "*", " "}
	PlatformStrings      = []string{"_", "-"}
	DotsStrings          = []string{".  ", ".. ", "...", "   "}
	ScrollingDotsStrings = []string{".  ", ".. ", "...", " ..", "  .", "   "}
	StarDotsStrings      = []string{"...", "*..", ".*.", "..*", "..."}
	FlipStrings          = []string{"_", "_", "_", "-", "`", "'", "´", "-", "_", "_", "_"}
	ToggleStrings        = []string{"=", "*", "-"}
	DQPBStrings          = []string{"d", "q", "p", "b"}

	BoundingBarStrings = []string{
		"[    ]", "[=   ]", "[==  ]", "[=== ]", "[ ===]", "[  ==]", "[   =]",
		"[    ]", "[   =]", "[  ==]", "[ ===]", "[=== ]", "[==  ]", "[=   ]",
	}

	Spinner       = NewAnimatedString(SpinnerStrings, DefaultInterval)
	SpinnerX      = NewAnimatedString(SpinnerXStrings, DefaultInterval)
	SpinnerV      = NewAnimatedString(SpinnerVStrings, DefaultInterval)
	Throbber      = NewAnimatedString(ThrobberStrings, DefaultInterval)
	Balloon       = NewAnimatedString(BalloonStrings, DefaultInterval)
	Platform      = NewAnimatedString(PlatformStrings, DefaultInterval)
	Dots          = NewAnimatedString(DotsStrings, DefaultInterval)
	ScrollingDots = NewAnimatedString(ScrollingDotsStrings, DefaultInterval)
	StarDots      = NewAnimatedString(StarDotsStrings, DefaultInterval)
	Flip          = NewAnimatedString(FlipStrings, DefaultInterval)
	Toggle        = NewAnimatedString(ToggleStrings, DefaultInterval)
	DQPB          = NewAnimatedString(DQPBStrings, DefaultInterval)
	BoundingBar   = NewAnimatedString(BoundingBarStrings, DefaultInterval)
)

Functions

func PrintProgress

func PrintProgress(f func(*Printer) error, configs ...ConfigFunc) error

PrintProgress creates a printer with the given configs and calls the function with a reference to the printer. The printer is configured to refresh during the function call then stop once it returns.

Types

type AnimatedString

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

AnimatedString is an implementation of the Stringer interface that returns the next part of the string in a cycle based on the time since the string was created. Used to create spinners and throbbers in the terminal.

func NewAnimatedString

func NewAnimatedString(parts []string, interval time.Duration) *AnimatedString

NewAnimatedString makes an animated string with the given string list (which animates left to right) and animation interval.

func NewStaticString

func NewStaticString(value string) *AnimatedString

NewStaticString creates an animated string that only has a single frame.

func (*AnimatedString) String

func (ac *AnimatedString) String() string

String returns the current string in the animation cycle.

type ConfigFunc

type ConfigFunc func(*printerConfig)

ConfigFunc is a function used to initialize a printer.

func WithInterval

func WithInterval(interval time.Duration) ConfigFunc

WithInterval sets the default refresh interval for a Printer.

func WithWriter

func WithWriter(writer io.Writer) ConfigFunc

WithWriter sets the writer backing a Printer.

type Content

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

Content is a wrapper around formatted strings which can be applied to a Printer atomically.

func NewContent

func NewContent() *Content

NewContent creates an empty Content.

func (*Content) AddLine

func (c *Content) AddLine(format string, args ...interface{})

AddLine adds a format string and arg list to the end of the content list.

func (*Content) String

func (c *Content) String() string

String serializes the lines of content. If some part contains an animated string, it will be re-evaluated on each invocation of this method.

type Printer

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

Printer adds functionality over an io.Writer which uses ANSI codes to delete and overwrite previously written content.

func NewPrinter

func NewPrinter(configs ...ConfigFunc) *Printer

NewPrinter creates a new Printer with the given configs.

func NewStderrPrinter

func NewStderrPrinter() *Printer

NewStderrPrinter creates a printer that writes to stderr.

func NewStdoutPrinter

func NewStdoutPrinter() *Printer

NewStdoutPrinter creates a printer that writes to stdout.

func (*Printer) Refresh

func (p *Printer) Refresh(interval time.Duration)

Refresh starts a goroutine that re-writes the last chunk of content to the screen. This will have the effect of animating strings in the content.

func (*Printer) Reset

func (p *Printer) Reset() error

Reset clears the content written by the last call to WriteContent.

func (*Printer) Stop

func (p *Printer) Stop()

Stop will cancel an active refresh ticker.

func (*Printer) WriteContent

func (p *Printer) WriteContent(content *Content) error

WriteContent clears the content written by the last call to WriteContent, then writes the new content to the backing writer.

func (*Printer) WriteString

func (p *Printer) WriteString(format string, args ...interface{}) error

WriteString calls WriteContent described by given format string and arg list.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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