progress

package
v0.0.18 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bar

type Bar interface {
	UpdateDisplay(newDisplay *DisplayProps)
}

Bar controls how a bar is displayed, for both single bar or multi bar item.

type DisplayProps

type DisplayProps struct {
	Prefix string `json:"prefix,omitempty"`
	Suffix string `json:"suffix,omitempty"` // If `Mode == Done / Error`, Suffix is not printed
	Mode   Mode   `json:"mode,omitempty"`
	Detail string `json:"detail,omitempty"`
}

DisplayProps controls the display of the progress bar.

func (*DisplayProps) String

func (dp *DisplayProps) String() string

String implements string

type Mode

type Mode int

Mode determines how the progress bar is rendered

const (
	// ModeSpinner renders a Spinner
	ModeSpinner Mode = iota
	// ModeProgress renders a ProgressBar. Not supported yet.
	ModeProgress
	// ModeDone renders as "Done" message.
	ModeDone
	// ModeError renders as "Error" message.
	ModeError
)

func (Mode) MarshalJSON

func (m Mode) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON marshaler

func (Mode) String

func (m Mode) String() string

String implements string

func (*Mode) UnmarshalJSON

func (m *Mode) UnmarshalJSON(b []byte) error

UnmarshalJSON implements JSON unmarshaler

type MultiBar

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

MultiBar renders multiple progress bars.

func NewMultiBar

func NewMultiBar(prefix string) *MultiBar

NewMultiBar creates a new MultiBar.

func (*MultiBar) AddBar

func (b *MultiBar) AddBar(prefix string) *MultiBarItem

AddBar adds a new bar item. This function is not thread safe. Must be called before render loop is started.

func (*MultiBar) StartRenderLoop

func (b *MultiBar) StartRenderLoop()

StartRenderLoop starts the render loop. This function is thread safe.

func (*MultiBar) StopRenderLoop

func (b *MultiBar) StopRenderLoop()

StopRenderLoop stops the render loop. This function is thread safe.

type MultiBarItem

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

MultiBarItem controls a bar item inside MultiBar.

func (*MultiBarItem) UpdateDisplay

func (i *MultiBarItem) UpdateDisplay(newDisplay *DisplayProps)

UpdateDisplay updates the display property of this bar item. This function is thread safe.

type SingleBar

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

SingleBar renders single progress bar.

Example
package main

import (
	"strconv"
	"time"

	"github.com/wentaojin/dbms/utils/progress"
)

func main() {
	b := progress.NewSingleBar("Prefix")

	b.UpdateDisplay(&progress.DisplayProps{
		Prefix: "Prefix",
		Suffix: "Suffix",
	})

	n := 3

	go func() {
		time.Sleep(time.Second)
		for i := 0; i < n; i++ {
			b.UpdateDisplay(&progress.DisplayProps{
				Prefix: "Prefix" + strconv.Itoa(i),
				Suffix: "Suffix" + strconv.Itoa(i),
			})
			time.Sleep(time.Second)
		}
	}()

	b.StartRenderLoop()

	time.Sleep(time.Second * time.Duration(n+1))

	b.UpdateDisplay(&progress.DisplayProps{
		Mode: progress.ModeDone,
		// Mode:   progress.ModeError,
		Prefix: "Prefix",
	})

	b.StopRenderLoop()
}
Output:

Example (Err)
package main

import (
	"errors"
	"strconv"
	"time"

	"github.com/wentaojin/dbms/utils/progress"
)

func main() {
	b := progress.NewSingleBar("Prefix")

	b.UpdateDisplay(&progress.DisplayProps{
		Prefix: "Prefix",
		Suffix: "Suffix",
	})

	n := 3

	go func() {
		time.Sleep(time.Second)
		for i := 0; i < n; i++ {
			b.UpdateDisplay(&progress.DisplayProps{
				Prefix: "Prefix" + strconv.Itoa(i),
				Suffix: "Suffix" + strconv.Itoa(i),
			})
			time.Sleep(time.Second)
		}
	}()

	b.StartRenderLoop()

	time.Sleep(time.Second * time.Duration(n+1))

	b.UpdateDisplay(&progress.DisplayProps{
		Mode:   progress.ModeError,
		Prefix: "Prefix",
		Detail: errors.New("expected failure").Error(),
	})

	b.StopRenderLoop()
}
Output:

func NewSingleBar

func NewSingleBar(prefix string) *SingleBar

NewSingleBar creates a new SingleBar.

func (*SingleBar) StartRenderLoop

func (b *SingleBar) StartRenderLoop()

StartRenderLoop starts the render loop. This function is thread safe.

func (*SingleBar) StopRenderLoop

func (b *SingleBar) StopRenderLoop()

StopRenderLoop stops the render loop. This function is thread safe.

func (*SingleBar) UpdateDisplay

func (b *SingleBar) UpdateDisplay(newDisplay *DisplayProps)

UpdateDisplay updates the display property of this single bar. This function is thread safe.

Jump to

Keyboard shortcuts

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