progress

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 2

Documentation

Overview

Package progress provides methods and structs for creating terminal progress bar

Index

Examples

Constants

View Source
const MIN_REFRESH_RATE = time.Duration(time.Millisecond)

MIN_REFRESH_RATE is minimal refresh rate (1 ms)

View Source
const MIN_WIDTH = 80

MIN_WIDTH is minimal progress bar width

View Source
const PROGRESS_BAR_SYMBOL = "—"

PROGRESS_BAR_SYMBOL is symbol for creating progress bar

Variables

View Source
var DefaultSettings = Settings{
	RefreshRate:       100 * time.Millisecond,
	NameColorTag:      "{b}",
	BarFgColorTag:     "{r}",
	BarBgColorTag:     "{s-}",
	PercentColorTag:   "{m}",
	SpeedColorTag:     "{r}",
	ProgressColorTag:  "{g}",
	RemainingColorTag: "{c}",
	ShowName:          true,
	ShowPercentage:    true,
	ShowProgress:      true,
	ShowSpeed:         true,
	ShowRemaining:     true,
	IsSize:            true,
	Width:             88,
	WindowSizeSec:     15.0,
}

DefaultSettings is default progress bar settings

View Source
var (
	ErrBarIsNil = fmt.Errorf("Progress bar struct is nil")
)

Functions

This section is empty.

Types

type Bar

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

Bar is progress bar struct

func New

func New(total int64, name string) *Bar

New creates new progress bar struct

Example (Download)
package main

import (
	"io"
	"os"

	"github.com/essentialkaos/ek/v12/progress"
	"github.com/essentialkaos/ek/v12/req"
)

func main() {
	pb := progress.New(0, "file.zip")
	resp, err := req.Request{URL: "https://domain.com/file.zip"}.Get()

	if err != nil {
		panic(err.Error())
	}

	if resp.StatusCode != 200 {
		panic("Looks like something is wrong")
	}

	defer resp.Body.Close()

	fd, err := os.OpenFile("file.zip", os.O_CREATE|os.O_WRONLY, 0644)

	if err != nil {
		panic(err.Error())
	}

	defer fd.Close()

	// Set total size to content length
	pb.SetTotal(resp.ContentLength)
	pb.Start()
	io.Copy(fd, pb.Reader(resp.Body))
	pb.Finish()
}
Output:

Example (Simple)
package main

import (
	"time"

	"github.com/essentialkaos/ek/v12/progress"
)

func main() {
	pb := progress.New(1000, "Counting…")

	// You can use default settings as a starting point
	pbs := progress.DefaultSettings
	pbs.RefreshRate = 50 * time.Millisecond
	pbs.IsSize = false

	// You can update all settings except RefreshRate before and after
	// calling Start method. RefreshRate must be set before calling
	// Start method.
	pb.UpdateSettings(pbs)

	pb.Start() // Start async progress handling

	for i := 0; i < 1000; i++ {
		time.Sleep(time.Second / 100)
		pb.Add(1)
	}

	pb.Finish() // Stop async progress handling
}
Output:

func (*Bar) Add

func (b *Bar) Add(v int)

func (*Bar) Add64

func (b *Bar) Add64(v int64)

Add64 adds given value ti

func (*Bar) Current

func (b *Bar) Current() int64

Current returns current progress bar value

func (*Bar) Finish

func (b *Bar) Finish() error

Finish finishes progress processing

func (*Bar) IsFinished

func (b *Bar) IsFinished() bool

IsFinished returns true if progress processing is finished

func (*Bar) IsStarted

func (b *Bar) IsStarted() bool

IsStarted returns true if progress processing is started

func (*Bar) Name

func (b *Bar) Name() string

Name returns progress bar name

func (*Bar) Reader

func (b *Bar) Reader(r io.ReadCloser) io.ReadCloser

Reader creates and returns pass thru-proxy reader

func (*Bar) SetCurrent

func (b *Bar) SetCurrent(v int64)

SetCurrent sets current progress bar value

func (*Bar) SetName

func (b *Bar) SetName(name string)

SetName sets progress bar name

func (*Bar) SetTotal

func (b *Bar) SetTotal(v int64)

SetTotal sets total progress bar value

func (*Bar) Start

func (b *Bar) Start() error

Start starts progress processing

func (*Bar) Total

func (b *Bar) Total() int64

Total returns total progress bar value

func (*Bar) UpdateSettings

func (b *Bar) UpdateSettings(s Settings) error

UpdateSettings updates progress settings

func (*Bar) Writer

func (b *Bar) Writer(w io.WriteCloser) io.WriteCloser

Writer creates and returns pass-thru proxy reader

type Settings

type Settings struct {
	RefreshRate time.Duration

	NameColorTag      string
	BarFgColorTag     string
	BarBgColorTag     string
	PercentColorTag   string
	ProgressColorTag  string
	SpeedColorTag     string
	RemainingColorTag string

	ShowSpeed      bool
	ShowName       bool
	ShowPercentage bool
	ShowProgress   bool
	ShowRemaining  bool

	Width    int
	NameSize int

	WindowSizeSec int64 // Window size for passtru reader

	IsSize bool
}

Settings contains progress bar settings

func (Settings) Validate added in v12.83.0

func (s Settings) Validate() error

Validate validates settings struct

Jump to

Keyboard shortcuts

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