Documentation ¶
Index ¶
- Variables
- func FormatAmount(amount uint64, width int) string
- func FormatBPS(n, sec float64, width int) string
- func FormatDuration(dt float64) string
- func Norm(col int, msg []rune) []rune
- type ANSIMeter
- func (p *ANSIMeter) Finished()
- func (p *ANSIMeter) GetWritten() float64
- func (p *ANSIMeter) Notify(msgstr string)
- func (p *ANSIMeter) Percent() string
- func (p *ANSIMeter) Set(current float64)
- func (p *ANSIMeter) SetTotal(total float64)
- func (p *ANSIMeter) SetWritten(written float64)
- func (p *ANSIMeter) Spin(msgstr string)
- func (p *ANSIMeter) Start(label string, total float64)
- func (p *ANSIMeter) Write(bs []byte) (n int, err error)
- type EscapeChars
- type Meter
- type NullMeter
- type QuietMeter
- type Terminal
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Null = NullMeter{}
Null is a default NullMeter instance
var Spinner = []string{"/", "-", "\\", "|"}
Spinner defines the ANSI spinner whilst waiting for information.
Functions ¶
func FormatAmount ¶
FormatAmount attempts to correctly format the amount into a string.
Example (Long) ¶
package main import ( "fmt" "github.com/DavinZhang/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/DavinZhang/juju/cmd/output/progress" ) func main() { fmt.Printf("%q\n", progress.FormatAmount(12345, -1)) }
Output: "12.3k"
func FormatBPS ¶
FormatBPS attempts to format bits per second into a string.
Example ¶
package main import ( "fmt" "time" "github.com/DavinZhang/juju/cmd/output/progress" ) func main() { fmt.Printf("%q\n", progress.FormatBPS(12345, (10*time.Millisecond).Seconds(), -1)) }
Output: "1.23MB/s"
func FormatDuration ¶
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/DavinZhang/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!"
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) GetWritten ¶
func (*ANSIMeter) SetWritten ¶
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))
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 ¶
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
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