progress

package
v0.0.0-...-9ec3720 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Null = NullMeter{}

Null is a default NullMeter instance

View Source
var Spinner = []string{"/", "-", "\\", "|"}

Spinner defines the ANSI spinner whilst waiting for information.

Functions

func FormatAmount

func FormatAmount(amount uint64, width int) string

FormatAmount attempts to correctly format the amount into a string.

Example (Long)
package main

import (
	"fmt"

	"github.com/juju/juju/cmd/output/progress"
)

func main() {
	for _, amount := range []uint64{
		3,
		13, 95,
		103, 995,
		1013, 9995,
		10009, 99995,
	} {
		fmt.Printf("- %5d: 3: %q  5: %q  7: %q\n",
			amount,
			progress.FormatAmount(amount, 3),
			progress.FormatAmount(amount, -1),
			progress.FormatAmount(amount, 7),
		)
	}
}
Output:

-     3: 3: "  3"  5: "    3"  7: "     3 "
-    13: 3: " 13"  5: "   13"  7: "    13 "
-    95: 3: " 95"  5: "   95"  7: "    95 "
-   103: 3: "103"  5: "  103"  7: "   103 "
-   995: 3: "995"  5: "  995"  7: "   995 "
-  1013: 3: " 1k"  5: " 1013"  7: "  1013 "
-  9995: 3: "10k"  5: "10.0k"  7: " 9.995k"
- 10009: 3: "10k"  5: "10.0k"  7: "10.009k"
- 99995: 3: ".1M"  5: " 100k"  7: "100.00k"
Example (Short)
package main

import (
	"fmt"

	"github.com/juju/juju/cmd/output/progress"
)

func main() {
	fmt.Printf("%q\n", progress.FormatAmount(12345, -1))
}
Output:

"12.3k"

func FormatBPS

func FormatBPS(n, sec float64, width int) string

FormatBPS attempts to format bits per second into a string.

Example
package main

import (
	"fmt"
	"time"

	"github.com/juju/juju/cmd/output/progress"
)

func main() {
	fmt.Printf("%q\n", progress.FormatBPS(12345, (10*time.Millisecond).Seconds(), -1))
}
Output:

"1.23MB/s"

func FormatDuration

func FormatDuration(dt float64) string

FormatDuration takes a float and attempts to format that float into a resonable string. dt is seconds (as in the output of time.Now().Seconds())

Example
package main

import (
	"fmt"
	"math"
	"time"

	"github.com/juju/juju/cmd/output/progress"
)

func main() {
	for _, dt := range []time.Duration{
		3 * time.Nanosecond,
		36 * time.Microsecond,
		430 * time.Millisecond,
		5155 * time.Millisecond,
		time.Minute + 2*time.Second,
		124 * time.Minute / 10,
		2*time.Hour + 29*time.Minute,
		10*time.Hour + 9*time.Minute,
		10*time.Hour + 30*time.Minute,
		11*time.Hour + 2*time.Minute,
		30 * time.Hour,
		345 * time.Hour,
		357 * time.Hour,
		4272 * time.Hour,
		51368 * time.Hour,
		math.MaxInt64 / 10,
		math.MaxInt64,
	} {
		fmt.Printf("%q\n", progress.FormatDuration(dt.Seconds()))
	}
	fmt.Printf("%q\n", progress.FormatDuration(float64(math.MaxUint64)*365*24*60*60))
	fmt.Printf("%q\n", progress.FormatDuration(math.MaxFloat64))

}
Output:

"3.0ns"
" 36µs"
"430ms"
"5.16s"
"1m02s"
"12.4m"
"2h29m"
"10h9m"
"10.5h"
"11h2m"
"1d06h"
"14d9h"
"14.9d"
" 178d"
"5.86y"
"29.2y"
" 292y"
" 18Ey"
"ages!"

func Norm

func Norm(col int, msg []rune) []rune

Types

type ANSIMeter

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

ANSIMeter is a progress.Meter that uses ANSI escape codes to make better use of the available horizontal space.

func NewANSIMeter

func NewANSIMeter(stdout io.Writer, term Terminal, escapeChars EscapeChars, clock clock.Clock) *ANSIMeter

NewANSIMeter creates a new ANSIMeter using the supplied stdout.

func (*ANSIMeter) Finished

func (p *ANSIMeter) Finished()

Finished the progress display

func (*ANSIMeter) GetWritten

func (p *ANSIMeter) GetWritten() float64

func (*ANSIMeter) Notify

func (p *ANSIMeter) Notify(msgstr string)

Notify the user of miscellaneous events

func (*ANSIMeter) Percent

func (p *ANSIMeter) Percent() string

func (*ANSIMeter) Set

func (p *ANSIMeter) Set(current float64)

Set progress to the "current" step.

func (*ANSIMeter) SetTotal

func (p *ANSIMeter) SetTotal(total float64)

SetTotal sets "total" steps needed.

func (*ANSIMeter) SetWritten

func (p *ANSIMeter) SetWritten(written float64)

func (*ANSIMeter) Spin

func (p *ANSIMeter) Spin(msgstr string)

Spin indicates indefinite activity by showing a spinner.

func (*ANSIMeter) Start

func (p *ANSIMeter) Start(label string, total float64)

Start progress with max "total" steps.

func (*ANSIMeter) Write

func (p *ANSIMeter) Write(bs []byte) (n int, err error)

type EscapeChars

type EscapeChars struct {
	ClrEOL            string
	CursorInvisible   string
	CursorVisible     string
	EnterReverseMode  string
	ExitAttributeMode string
}

EscapeChars defines ANSI escape constants. These are the bits of the ANSI escapes (beyond \r) that we use (names of the terminfo capabilities, see terminfo(5))

func DefaultEscapeChars

func DefaultEscapeChars() EscapeChars

DefaultEscapeChars for ansimeter.

type Meter

type Meter interface {
	io.Writer

	// Start progress with max "total" steps.
	Start(label string, total float64)

	// Set progress to the "current" step.
	Set(current float64)

	// SetTotal sets "total" steps needed.
	SetTotal(total float64)

	// Finished the progress display
	Finished()

	// Spin indicates indefinite activity by showing a spinner.
	Spin(msg string)

	// Notify the user of miscellaneous events
	Notify(string)
}

Meter is an interface to show progress to the user

func MakeProgressBar

func MakeProgressBar(stdout io.Writer) Meter

MakeProgressBar creates an appropriate progress.Meter for the environ in which it is called:

  • if no terminal is attached, or we think we're running a test, a minimalistic QuietMeter is returned.
  • otherwise, an ANSIMeter is returned.

type NullMeter

type NullMeter struct{}

NullMeter is a Meter that does nothing

func (NullMeter) Finished

func (NullMeter) Finished()

Finished the progress display

func (NullMeter) Notify

func (NullMeter) Notify(string)

Notify the user of miscellaneous events

func (NullMeter) Set

func (NullMeter) Set(float64)

Set progress to the "current" step.

func (NullMeter) SetTotal

func (NullMeter) SetTotal(float64)

SetTotal sets "total" steps needed.

func (NullMeter) Spin

func (NullMeter) Spin(msg string)

Spin indicates indefinite activity by showing a spinner.

func (NullMeter) Start

func (NullMeter) Start(string, float64)

Start progress with max "total" steps.

func (NullMeter) Write

func (NullMeter) Write(p []byte) (int, error)

Write the update to the meter.

type QuietMeter

type QuietMeter struct {
	NullMeter
	// contains filtered or unexported fields
}

QuietMeter is a Meter that _just_ shows Notify()s.

func NewQuietMeter

func NewQuietMeter(stdout io.Writer) *QuietMeter

NewQuietMeter creates a new QuietMeter using the supplied stdout.

func (QuietMeter) Notify

func (m QuietMeter) Notify(msg string)

Notify the user of miscellaneous events

type Terminal

type Terminal interface {
	Width() int
}

Terminal defines a interface for interacting with a terminal.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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