prettyprogress

package module
v0.0.0-...-46101f6 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

prettyprogress

a tiny library for printing nice-looking console progress

GoDoc

Quick Start

See example/multistep/main.go for the main API, or example/simple/main.go for UI-only structs

Features:

  • Nice unicode icons
  • Colors! Animated Bullets!
  • Multiple parallel steps
  • Unicode progress bars with smooth-scrolling (inspired by this blog post)
  • Simple, composable API: just prints itself, use e.g. uilive for animation
  • Basic UI-only package if you just want to print a nice progress bar or grab a pretty unicode icon

Example:

Output from /example/multistep/main.go:

example

Documentation

Overview

Package prettyprogress contains some handy objects for printing out nice-looking progress information to a terminal

Example
multiStep := prettyprogress.NewMultistep(
	writer(func(s string) {
		fmt.Println("---")
		fmt.Println(s)
	}),
	prettyprogress.WithBarWidth(5),
	prettyprogress.WithBarLabel(ui.PercentageLabel),
)

step1 := multiStep.AddStep("Prepare..", 0)
step2 := multiStep.AddStep("Download XYZ..", 100)

step1.Complete("Prepared")

bar := step2.Bar(ui.Downloading, "Downloading..")
bar.Update(80)

step2.Fail("Download Failed")
Output:

---

    Prepare..

---

    Prepare..
    Download XYZ..

---

 ✓  Prepared
    Download XYZ..

---

 ✓  Prepared
 ↡  Downloading..   [████ ] 80%

---

 ✓  Prepared
 ✗  Download Failed

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultColors = Colors{
	Future:    func(s ...interface{}) string { return fmt.Sprintf("%s", s...) },
	Completed: func(s ...interface{}) string { return fmt.Sprintf("%s", s...) },
}

Functions

func WithAnimationFrameTicker

func WithAnimationFrameTicker(c <-chan time.Time) func(*Steps)

func WithBarLabel

func WithBarLabel(fn ui.LabelFunc) func(*Steps)

func WithBarWidth

func WithBarWidth(width int) func(s *Steps)

func WithBullets

func WithBullets(b ui.BulletSet) func(*Steps)

func WithLabelColors

func WithLabelColors(colors Colors) func(*Steps)

func Write

func Write(w io.Writer) func(s string)

Write creates a Watcher function that writes to a given io.Writer, useful for passing to one of the constructors in this package, e.g.

NewBar(100,100,Write(os.Stdout))

Write will panic if it is unable to write to the underlying writer

func Writeln

func Writeln(w io.Writer) func(s string)

Writeln creates a Watcher function that writes to a given io.Writer after appending a newline, useful for passing to one of the constructors in this package, e.g.

NewBar(100,100,Writeln(os.Stdout))

Writeln will panic if it is unable to write to the underlying writer

Types

type Bar

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

Bar is an updater that calls its watcher with the String() version of a ui.Bar whenever its UpdateProgress method is called.

Example
package main

import (
	"os"

	"github.com/julz/prettyprogress"
)

func main() {
	bar := prettyprogress.NewBar(100, 5, prettyprogress.Writeln(os.Stdout))
	bar.Update(20)
	bar.Update(40)
	bar.Update(80)

}
Output:

[█    ]
[██   ]
[████ ]

func NewBar

func NewBar(total, width int, w PrintFunc) *Bar

NewBar returns a new Bar object that can be updated, when the UpdateProgress method is called, the passed Watcher is called with the new state. This can be used to print the bar out, either directly to standard out or with some terminal magic to do animation.

func (*Bar) Update

func (b *Bar) Update(progress int)

Update calls the Bar's watcher (configured in NewBar) with the new state. Generally this will cause the new bar to be printed out to the user.

type Colors

type Colors struct {
	Future    func(s ...interface{}) string
	Completed func(s ...interface{}) string
}

type PrintFunc

type PrintFunc func(s string)

PrintFunc is a function that is called when new versions of a Bar, Step or Multistep are created as a result of calling UpdateX methods.

type Step

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

Step is an updater whose watcher recieves the output of ui.Step's String() method whenever its UpdateStatus method is called

Example
package main

import (
	"os"

	"github.com/julz/prettyprogress"
	"github.com/julz/prettyprogress/ui"
)

func main() {
	step := prettyprogress.NewStep(100, 5, prettyprogress.Writeln(os.Stdout))
	step.Start("Starting..")
	step.UpdateWithProgress(ui.Downloading, "Downloading", 20)
	step.UpdateWithProgress(ui.Downloading, "Downloading", 80)
	step.Complete("Done!")

}
Output:

►  Starting..
 ↡  Downloading   [█    ]
 ↡  Downloading   [████ ]
 ✓  Done!

func NewStep

func NewStep(barTotal, barWidth int, print PrintFunc) *Step

NewStep creates a new Step which can be updated with the status of a single task

func (*Step) Bar

func (b *Step) Bar(bullet ui.Bullet, status string) *Bar

Bar returns just the Bar component of the Step which can be updated to show numeric progress of the current task

For example, this could be used with a function that expects an interface with an Update method as follows:

download(step.Bar(ui.Downloading, "Downloading layer.."))

The corresponding `download` method could look as follows:

bar(p type interface{ Update(int) }) {
  p.Update(10)
  p.Update(30)
  p.Update(100)
})
Example
package main

import (
	"os"

	"github.com/julz/prettyprogress"
	"github.com/julz/prettyprogress/ui"
)

func main() {
	step := prettyprogress.NewStep(100, 5, prettyprogress.Writeln(os.Stdout))
	step.Update(ui.Running, "Preparing")

	bar := step.Bar(ui.Downloading, "Downloading")
	bar.Update(20)
	bar.Update(80)
	bar.Update(100)
}
Output:

►  Preparing
 ↡  Downloading   [█    ]
 ↡  Downloading   [████ ]
 ↡  Downloading   [█████]

func (*Step) Complete

func (b *Step) Complete(msg string)

Complete sets the steps name to the given string and changes the bullet to a symbol indicating the task has been completed

func (*Step) Fail

func (b *Step) Fail(msg string)

Fail sets the steps name to the given string and changes the bullet to a symbol indicating failure

func (*Step) Start

func (b *Step) Start(msg string)

Start sets the steps name to the given string and changes the bullet to a symbol indicating the task is running

func (*Step) Update

func (b *Step) Update(bullet ui.Bullet, status string)

Update sets the steps name to the givem status and updated the bullet to the given Bullet

func (*Step) UpdateState

func (b *Step) UpdateState(state ui.BulletState, status string)

UpdateState updates the step with the given status and the bullet from the current BulletSet corresponding to the given state

func (*Step) UpdateWithProgress

func (b *Step) UpdateWithProgress(bullet ui.Bullet, status string, progress int)

UpdateWithProgress updates the Bullet, Status and Progress Bar of the current step. Often either one of the convenience methods like Start, Fail, Complete, or Bar will be a better option.

type Steps

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

func NewFancyMultistep

func NewFancyMultistep(writer io.Writer, extraOptions ...StepsOption) *Steps

NewFancyMultistep creates a new updater that prints step progress with fancy colours, bullets and animations using the given Watcher

func NewMultistep

func NewMultistep(writer io.Writer, options ...StepsOption) *Steps

NewMultistep creates a new updater that can have multiple sub-steps. When any of the steps are updated, the Writer is called with the new string to display.

func (*Steps) AddStep

func (p *Steps) AddStep(name string, barTotal int) *Step

AddStep adds a sub-step to the display

type StepsOption

type StepsOption func(s *Steps)

Directories

Path Synopsis
example
pexec provides a wrapper for exec.Command that prints progress
pexec provides a wrapper for exec.Command that prints progress
Package ui contains some simple structs that have nice-looking String() methods for printing console output with unicode characters.
Package ui contains some simple structs that have nice-looking String() methods for printing console output with unicode characters.

Jump to

Keyboard shortcuts

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