mobyprogress

package module
v0.0.0-...-669a780 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

README

mobyprogress

ci godoc

The progress bar from the docker/moby project, repackaged as a standalone library.

Have you ever wanted a progress bar for your golang-based CLI tool only to find that the library either does not work on all platforms or does not have multibar support?
Me too! I've always liked the docker cli progress bar so I went ahead and broke it out into it's own library.

Note that since I am not the original author of this code I won't accept new features; this code is considered "complete".

Installation

go get github.com/pcj/mobyprogress@latest

Usage

Create a mobyprogess.Output instance over an io.Writer:

out := mobyprogress.NewOut(os.Stdout)

progress := mobyprogress.NewProgressOutput(out)

Then, send progress updates to the output:

progress.WriteProgress(progress.Progress{
    ID:      "some.tar.gz",
    Action:  "downloading",
    Total:   int64(bytesTotal),
    Current: int64(bytesSent),
    Units:   "bytes",
})

The progress.ID field is used as the key to identify the progress bar to update.

Documentation

Index

Constants

View Source
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"

RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to ensure the formatted time isalways the same number of characters.

Variables

This section is empty.

Functions

func DisplayJSONMessagesStream

func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool, auxCallback func(JSONMessage)) error

DisplayJSONMessagesStream displays a json message stream from `in` to `out`, `isTerminal` describes if `out` is a terminal. If this is the case, it will print `\n` at the end of each line and move the cursor while displaying.

func DisplayJSONMessagesToStream

func DisplayJSONMessagesToStream(in io.Reader, stream stream, auxCallback func(JSONMessage)) error

DisplayJSONMessagesToStream prints json messages to the output stream

func FormatError

func FormatError(err error) []byte

FormatError formats the error as a JSON object

func FormatStatus

func FormatStatus(id, format string, a ...interface{}) []byte

FormatStatus formats the specified objects according to the specified format (and id).

func Message

func Message(out Output, id, message string)

Message is a convenience function to write a progress message to the channel.

func Messagef

func Messagef(out Output, id, format string, a ...interface{})

Messagef is a convenience function to write a printf-formatted progress message to the channel.

func Update

func Update(out Output, id, action string)

Update is a convenience function to write a progress update to the channel.

func Updatef

func Updatef(out Output, id, format string, a ...interface{})

Updatef is a convenience function to write a printf-formatted progress update to the channel.

Types

type JSONError

type JSONError struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

JSONError wraps a concrete Code and Message, `Code` is is an integer error code, `Message` is the error message.

func (*JSONError) Error

func (e *JSONError) Error() string

type JSONMessage

type JSONMessage struct {
	Stream          string        `json:"stream,omitempty"`
	Status          string        `json:"status,omitempty"`
	Progress        *JSONProgress `json:"progressDetail,omitempty"`
	ProgressMessage string        `json:"progress,omitempty"` // deprecated
	ID              string        `json:"id,omitempty"`
	From            string        `json:"from,omitempty"`
	Time            int64         `json:"time,omitempty"`
	TimeNano        int64         `json:"timeNano,omitempty"`
	Error           *JSONError    `json:"errorDetail,omitempty"`
	ErrorMessage    string        `json:"error,omitempty"` // deprecated
	// Aux contains out-of-band data, such as digests for push signing and image id after building.
	Aux *json.RawMessage `json:"aux,omitempty"`
}

JSONMessage defines a message struct. It describes the created time, where it from, status, ID of the message. It's used for docker events.

func (*JSONMessage) Display

func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error

Display displays the JSONMessage to `out`. If `isTerminal` is true, it will erase the entire current line when displaying the progressbar.

type JSONProgress

type JSONProgress struct {
	Current int64 `json:"current,omitempty"`
	Total   int64 `json:"total,omitempty"`
	Start   int64 `json:"start,omitempty"`
	// If true, don't show xB/yB
	HideCounts bool   `json:"hidecounts,omitempty"`
	Units      string `json:"units,omitempty"`
	// contains filtered or unexported fields
}

JSONProgress describes a Progress. terminalFd is the fd of the current terminal, Start is the initial value for the operation. Current is the current status and value of the progress made towards Total. Total is the end value describing when we made 100% progress for an operation.

func (*JSONProgress) String

func (p *JSONProgress) String() string

type Out

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

Out is an output stream used by the DockerCli to write normal program output.

func NewOut

func NewOut(out io.Writer) *Out

NewOut returns a new Out object from a Writer

func (*Out) FD

func (s *Out) FD() uintptr

FD returns the file descriptor number for this stream

func (*Out) GetTtySize

func (o *Out) GetTtySize() (uint, uint)

GetTtySize returns the height and width in characters of the tty

func (*Out) IsTerminal

func (s *Out) IsTerminal() bool

IsTerminal returns true if this stream is connected to a terminal

func (*Out) RestoreTerminal

func (s *Out) RestoreTerminal()

RestoreTerminal restores normal mode to the terminal

func (*Out) SetIsTerminal

func (s *Out) SetIsTerminal(isTerminal bool)

SetIsTerminal sets the boolean used for isTerminal

func (*Out) SetRawTerminal

func (o *Out) SetRawTerminal() (err error)

SetRawTerminal sets raw mode on the input terminal

func (*Out) Write

func (o *Out) Write(p []byte) (int, error)

type Output

type Output interface {
	WriteProgress(Progress) error
}

func ChanOutput

func ChanOutput(progressChan chan<- Progress) Output

ChanOutput returns an Output that writes progress updates to the supplied channel.

func NewJSONProgressOutput

func NewJSONProgressOutput(out io.Writer, newLines bool) Output

NewJSONProgressOutput returns a progress.Output that formats output using JSON objects

func NewProgressOutput

func NewProgressOutput(out io.Writer) Output

type Progress

type Progress struct {
	ID string

	// Progress contains a Message or...
	Message string

	// ...progress of an action
	Action  string
	Current int64
	Total   int64

	// If true, don't show xB/yB
	HideCounts bool
	// If not empty, use units instead of bytes for counts
	Units string

	// Aux contains extra information not presented to the user, such as
	// digests for push signing.
	Aux interface{}

	LastUpdate bool
}

type Reader

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

Reader is a Reader with progress bar.

func NewProgressReader

func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader

NewProgressReader creates a new ProgressReader.

func (*Reader) Close

func (p *Reader) Close() error

Close closes the progress reader and its underlying reader.

func (*Reader) Read

func (p *Reader) Read(buf []byte) (n int, err error)

Jump to

Keyboard shortcuts

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