pb

package
v0.0.0-...-6202178 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package pb implements a progressbar component for CLI user interfaces

Index

Constants

View Source
const (
	// DefaultWidth of the progress bar
	DefaultWidth = 40
)

Variables

This section is empty.

Functions

func Clampf

func Clampf(val, min, max float64) float64

Clampf returns the given value, "clamped" to the range [min, max]. This is copied from lib/util.go to avoid circular imports.

func GetFixedLengthDuration

func GetFixedLengthDuration(d, maxDuration time.Duration) (result string)

GetFixedLengthDuration takes a *positive* duration and its max value and returns a string with a fixed width so we can prevent UI elements jumping around. The format is "___d__h__m__s.s", but leading values can be omitted based on the maxDuration value, the results can be: "___h__m__s.s".

This is code was inspired by the Go stdlib's time.Duration.String() code. TODO: more flexibility - negative values or variable precision?

func GetFixedLengthFloatFormat

func GetFixedLengthFloatFormat(maxValue float64, precision uint) (formatStr string)

GetFixedLengthFloatFormat returns "%0__.__f" format argument for fmt functions that will produce a base-10 right-aligned zero-padded string representation of the supplied float value, with the specified decimal precision. The number of characters (i.e. the actual number + maybe dot and precision + how many zeros it will be padded on the left with) in the returned string corresponds to the number of digits in the supplied maxValue and the desired precision.

func GetFixedLengthIntFormat

func GetFixedLengthIntFormat(maxValue int64) (formatStr string)

GetFixedLengthIntFormat returns "%0__d" format argument for fmt functions that will produce a base-10 right-aligned zero-padded string representation of the supplied integer value. The number of characters (i.e. the actual number + how many zeros it will be padded on the left with) in the returned string corresponds to the number of digits in the supplied maxValue.

Types

type ProgressBar

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

ProgressBar is a simple thread-safe progressbar implementation with callbacks.

func New

func New(options ...ProgressBarOption) *ProgressBar

New creates and initializes a new ProgressBar struct, calling all of the supplied options

func (*ProgressBar) Left

func (pb *ProgressBar) Left() string

Left returns the left part of the progressbar in a thread-safe way.

func (*ProgressBar) Modify

func (pb *ProgressBar) Modify(options ...ProgressBarOption)

Modify changes the progressbar options in a thread-safe way.

func (*ProgressBar) Render

func (pb *ProgressBar) Render(maxLeft, widthDelta int) ProgressBarRender

Render locks the progressbar struct for reading and calls all of its methods to return the final output. A struct is returned over a plain string to allow dynamic padding and positioning of elements depending on other elements on the screen.

  • maxLeft defines the maximum character length of the left-side text. Characters exceeding this length will be replaced with a single ellipsis. Passing <=0 disables this.
  • widthDelta changes the progress bar width the specified amount of characters. E.g. passing -2 would shorten the width by 2 chars. If the resulting width is lower than minWidth, progress will be rendered as a percentage instead of a filling bar.

type ProgressBarOption

type ProgressBarOption func(*ProgressBar)

ProgressBarOption is used for helper functions that modify the progressbar parameters, either in the constructor or via the Modify() method.

func WithConstLeft

func WithConstLeft(left string) ProgressBarOption

WithConstLeft sets the left progressbar value to the supplied const.

func WithConstProgress

func WithConstProgress(progress float64, right ...string) ProgressBarOption

WithConstProgress sets the progress and right values to the supplied consts.

func WithHijack

func WithHijack(hijack func() string) ProgressBarOption

WithHijack replaces the progressbar Render function with the argument.

func WithLeft

func WithLeft(left func() string) ProgressBarOption

WithLeft modifies the function that returns the left progressbar value.

func WithLogger

func WithLogger(logger *logrus.Entry) ProgressBarOption

WithLogger modifies the logger instance

func WithProgress

func WithProgress(progress func() (float64, []string)) ProgressBarOption

WithProgress modifies the progress calculation function.

func WithStatus

func WithStatus(status Status) ProgressBarOption

WithStatus modifies the progressbar status

type ProgressBarRender

type ProgressBarRender struct {
	Right []string

	Left, Hijack string

	Color bool
	// contains filtered or unexported fields
}

ProgressBarRender stores the different rendered parts of the progress bar UI to allow dynamic positioning and padding of elements in the terminal output (e.g. for responsive progress bars).

func (*ProgressBarRender) Progress

func (pbr *ProgressBarRender) Progress() string

Progress returns an assembled and optionally colorized progress string

func (*ProgressBarRender) Status

func (pbr *ProgressBarRender) Status() string

Status returns an optionally colorized status string

func (ProgressBarRender) String

func (pbr ProgressBarRender) String() string

type Status

type Status rune

Status of the progress bar

const (
	Running     Status = ' '
	Waiting     Status = '•'
	Stopping    Status = '↓'
	Interrupted Status = '✗'
	Done        Status = '✓'
)

Progress bar status symbols

Jump to

Keyboard shortcuts

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