Documentation ¶
Overview ¶
message from the author:
+--------------------------------------------------------------+ | * * * ░░░░░░░░░░░░░░░░░░░░ Hello ░░░░░░░░░░░░░░░░░░░░░░░░░░| +--------------------------------------------------------------+ | | | ++ ______________________________________ | | ++++ / \ | | ++++ | | | | ++++++++++ | Feel free to contribute to this | | | +++ | | project or contact me on | | | ++ | | manfred.life if you like this | | | + -== ==| | project! | | | ( <*> <*> | | | | | | /| :) | | | | _) / | | | | | +++ / \______________________________________/ | | \ =+ / | | \ + | | |\++++++ | | | ++++ ||// | | ___| |___ _||/__ __| | / --- \ \| ||| __ _ ___ __ __/ /| |/ | | \ \ / / ' \/ _ \/ // / / | || | | | | | /_/_/_/\___/\_,_/_/ | +--------------------------------------------------------------+
Example ¶
package main import ( "fmt" "moul.io/progress" "moul.io/u" ) func main() { // initialize a new progress.Progress prog := progress.New() prog.AddStep("init").SetDescription("initialize") prog.AddStep("step1").SetDescription("step 1") prog.AddStep("step2").SetData([]string{"hello", "world"}).SetDescription("step 2") prog.AddStep("step3") prog.AddStep("finish") // automatically mark the last step as done when the function quit defer prog.Get("finish").Done() // mark init as Done prog.Get("init").Done() // mark step1 as started prog.Get("step1").SetData(42).Start() // then, mark it as done + attach custom data prog.Get("step1").SetData(1337).Done() // mark step2 as started prog.Get("step2").Start() fmt.Println(u.PrettyJSON(prog)) // outputs something like this: // { // "steps": [ // { // "id": "init", // "description": "initialize", // "started_at": "2020-12-22T20:26:05.717427484+01:00", // "done_at": "2020-12-22T20:26:05.717427484+01:00", // "state": "done" // }, // { // "id": "step1", // "description": "step 1", // "started_at": "2020-12-22T20:26:05.71742797+01:00", // "done_at": "2020-12-22T20:26:05.717428258+01:00", // "state": "done", // "data": 1337, // "duration": 286 // }, // { // "id": "step2", // "description": "step 2", // "started_at": "2020-12-22T20:26:05.71742865+01:00", // "state": "in progress", // "data": [ // "hello", // "world" // ], // "duration": 496251 // }, // { // "id": "step3" // }, // { // "id": "finish" // } // ], // "created_at": "2020-12-22T20:26:05.717423018+01:00", // "snapshot": { // "state": "in progress", // "doing": "step 2", // "not_started": 2, // "in_progress": 1, // "completed": 2, // "total": 5, // "percent": 50, // "total_duration": 25935, // "started_at": "2020-12-22T20:26:05.717427484+01:00" // } //} }
Output:
Index ¶
- Variables
- type Progress
- func (p *Progress) AddStep(id string) *Step
- func (p *Progress) Close()
- func (p *Progress) Get(id string) *Step
- func (p *Progress) MarshalJSON() ([]byte, error)
- func (p *Progress) Progress() float64
- func (p *Progress) SafeAddStep(id string) (*Step, error)
- func (p *Progress) Snapshot() Snapshot
- func (p *Progress) Subscribe() chan *Step
- type Snapshot
- type State
- type Step
- func (s *Step) Done() *Step
- func (s *Step) Duration() time.Duration
- func (s *Step) MarshalJSON() ([]byte, error)
- func (s *Step) SetAsCurrent() *Step
- func (s *Step) SetData(data interface{}) *Step
- func (s *Step) SetDescription(desc string) *Step
- func (s *Step) SetProgress(progress float64) *Step
- func (s *Step) Start() *Step
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrStepRequiresID = errors.New("progress.AddStep requires a non-empty ID as argument") ErrStepIDShouldBeUnique = errors.New("progress.AddStep requires a unique ID as argument") )
Functions ¶
This section is empty.
Types ¶
type Progress ¶
type Progress struct { Steps []*Step `json:"steps,omitempty"` CreatedAt time.Time `json:"created_at,omitempty"` // contains filtered or unexported fields }
Progress is the top-level object of the 'progress' library.
func (*Progress) AddStep ¶
AddStep creates and returns a new Step with the provided 'id'. A non-empty, unique 'id' is required, else it will panic.
func (*Progress) Get ¶
Get retrieves a Step by its 'id'. A non-empty 'id' is required, else it will panic. If 'id' does not match an existing step, nil is returned.
func (*Progress) MarshalJSON ¶
MarshalJSON is a custom JSON marshaler that automatically computes and append the current snapshot.
func (*Progress) Progress ¶
Progress returns the current completion rate, it's a faster alternative to Progress.Snapshot().Progress. The returned value is between 0.0 and 1.0.
func (*Progress) SafeAddStep ¶
SafeAddStep is equivalent to AddStep with but returns error instead of panicking.
type Snapshot ¶
type Snapshot struct { State State `json:"state,omitempty"` Doing string `json:"doing,omitempty"` NotStarted int `json:"not_started,omitempty"` InProgress int `json:"in_progress,omitempty"` Completed int `json:"completed,omitempty"` Total int `json:"total,omitempty"` Progress float64 `json:"progress,omitempty"` TotalDuration time.Duration `json:"total_duration,omitempty"` StepDuration time.Duration `json:"step_duration,omitempty"` CompletionEstimate time.Duration `json:"completion_estimate,omitempty"` DoneAt *time.Time `json:"done_at,omitempty"` StartedAt *time.Time `json:"started_at,omitempty"` }
Snapshot represents info and stats about a progress at a given time.
type Step ¶
type Step struct { ID string `json:"id,omitempty"` Description string `json:"description,omitempty"` StartedAt *time.Time `json:"started_at,omitempty"` DoneAt *time.Time `json:"done_at,omitempty"` State State `json:"state,omitempty"` Data interface{} `json:"data,omitempty"` Progress float64 `json:"progress,omitempty"` // contains filtered or unexported fields }
Step represents a progress step. It always have an 'id' and can be customized using helpers.
func (*Step) MarshalJSON ¶
MarshalJSON is a custom JSON marshaler that automatically computes and append some runtime metadata.
func (*Step) SetAsCurrent ¶
SetAsCurrent stops all in-progress steps and start this one.
func (*Step) SetDescription ¶
SetDescription sets a custom step description. It returns itself (*Step) for chaining.
func (*Step) SetProgress ¶
SetProgress sets the current step progress rate. It may also update the current Step.State depending on the passed progress. The value should be something between 0.0 and 1.0.