Documentation ¶
Overview ¶
Package passthru provides Reader and Writer with information about the amount of data being passed.
Example ¶
package main // ////////////////////////////////////////////////////////////////////////////////// // // // // Copyright (c) 2024 ESSENTIAL KAOS // // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // // // // ////////////////////////////////////////////////////////////////////////////////// // import ( "io" "os" "time" "github.com/essentialkaos/ek/v12/fmtutil" "github.com/essentialkaos/ek/v12/passthru" "github.com/essentialkaos/ek/v12/path" "github.com/essentialkaos/ek/v12/req" "github.com/essentialkaos/ek/v12/spinner" "github.com/essentialkaos/ek/v12/terminal" ) type DLSpinner struct { file string lastUpdate time.Time reader *passthru.Reader } func main() { file := "https://mirror.yandex.ru/fedora/linux/releases/39/Server/x86_64/iso/Fedora-Server-netinst-x86_64-39-1.5.iso" filename := path.Base(file) spinner.Show("Download file {c}%s{!}", filename) resp, err := req.Request{URL: file, AutoDiscard: true}.Get() if err != nil { spinner.Done(false) terminal.Error(err) return } fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { spinner.Done(false) terminal.Error(err) return } r := passthru.NewReader(resp.Body, resp.ContentLength) s := &DLSpinner{file: filename, reader: r} s.reader.Update = s.Update _, err = io.Copy(fd, r) spinner.Update( "Download file {c}%s{!} {s}(%s){!}", filename, fmtutil.PrettySize(resp.ContentLength), ) spinner.Done(err == nil) s.reader.Close() if err != nil { terminal.Error(err) return } } func (s *DLSpinner) Update(_ int) { now := time.Now() if now.Sub(s.lastUpdate) < time.Second/10 { return } speed, _ := s.reader.Speed() spinner.Update( "{s}[ %4.1f%% | %6s/s ]{!} Download file {c}%s{!}", s.reader.Progress(), fmtutil.PrettySize(speed), s.file, ) s.lastUpdate = now }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNilReader = errors.New("Reader is nil") ErrNilWriter = errors.New("Writer is nil") )
Functions ¶
This section is empty.
Types ¶
type Calculator ¶
type Calculator struct {
// contains filtered or unexported fields
}
Calculator calculates pass-thru speed and remaining time
func NewCalculator ¶
func NewCalculator(total int64, winSizeSec float64) *Calculator
NewCalculator creates new Calculator struct
func (*Calculator) Calculate ¶
func (c *Calculator) Calculate(current int64) (float64, time.Duration)
Calculate calculates speed and remaining time
func (*Calculator) SetTotal ¶
func (c *Calculator) SetTotal(total int64)
SetTotal sets total number of objects
type Reader ¶
type Reader struct { Calculator *Calculator Update func(n int) // contains filtered or unexported fields }
Reader is pass-thru Reader
func NewReader ¶
func NewReader(reader io.ReadCloser, total int64) *Reader
NewReader creates new passthru reader
type Writer ¶
type Writer struct { Calculator *Calculator Update func(n int) // contains filtered or unexported fields }
Writer is pass-thru Writer
func NewWriter ¶
func NewWriter(writer io.WriteCloser, total int64) *Writer
NewWriter creates new passthru writer
Click to show internal directories.
Click to hide internal directories.