passthru

package
v12.122.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: Apache-2.0 Imports: 6 Imported by: 2

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

func (*Reader) Close

func (r *Reader) Close() error

Close closes the reader

func (*Reader) Current

func (r *Reader) Current() int64

Current returns read amount of data

func (*Reader) IsClosed

func (r *Reader) IsClosed() bool

IsClosed returns true if reader is closed

func (*Reader) Progress

func (r *Reader) Progress() float64

Progress returns percentage of data read

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read implements the standard Read interface

func (*Reader) SetTotal

func (r *Reader) SetTotal(total int64)

SetTotal sets total amount of data

func (*Reader) Speed

func (r *Reader) Speed() (float64, time.Duration)

Speed calculates passthru speed and time remaining to process all data. If Calculator was not set on the first call, a default calculator with a 10 second window is created.

func (*Reader) Total

func (r *Reader) Total() int64

Total returns total amount of data

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

func (*Writer) Close

func (w *Writer) Close() error

Close closes the writer

func (*Writer) Current

func (w *Writer) Current() int64

Current returns written amount of data

func (*Writer) IsClosed

func (w *Writer) IsClosed() bool

IsClosed returns true if writer is closed

func (*Writer) Progress

func (w *Writer) Progress() float64

Progress returns percentage of data written

func (*Writer) SetTotal

func (w *Writer) SetTotal(total int64)

SetTotal sets total amount of data

func (*Writer) Speed

func (w *Writer) Speed() (float64, time.Duration)

Speed calculates passthru speed and time remaining to process all data. If Calculator was not set on the first call, a default calculator with a 10 second window is created.

func (*Writer) Total

func (w *Writer) Total() int64

Total returns total amount of data

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write implements the standard Write interface

Jump to

Keyboard shortcuts

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