refactor(prog): add formatter param

This commit is contained in:
iyear 2022-09-15 23:06:01 +08:00
parent 7d99bf8560
commit 6428dc44ef
4 changed files with 12 additions and 10 deletions

View file

@ -21,6 +21,8 @@ import (
const TempExt = ".tmp"
var formatter = utils.Byte.FormatBinaryBytes
type Downloader struct {
client *tg.Client
pw progress.Writer
@ -32,7 +34,7 @@ type Downloader struct {
func New(client *tg.Client, partSize int, threads int, iter Iter) *Downloader {
return &Downloader{
client: client,
pw: prog.New(),
pw: prog.New(formatter),
partSize: partSize,
threads: threads,
iter: iter,
@ -86,7 +88,7 @@ func (d *Downloader) Download(ctx context.Context, limit int) error {
}
func (d *Downloader) download(ctx context.Context, item *Item) error {
tracker := prog.AppendTracker(d.pw, item.Name, item.Size)
tracker := prog.AppendTracker(d.pw, formatter, item.Name, item.Size)
filename := fmt.Sprintf("%s%s", utils.FS.GetNameWithoutExt(item.Name), TempExt)
path := filepath.Join(consts.DownloadPath, filename)

View file

@ -2,12 +2,11 @@ package prog
import (
"github.com/fatih/color"
"github.com/iyear/tdl/pkg/utils"
"github.com/jedib0t/go-pretty/v6/progress"
"time"
)
func New() progress.Writer {
func New(formatter progress.UnitsFormatter) progress.Writer {
pw := progress.NewWriter()
pw.SetAutoStop(true)
pw.SetTrackerLength(20)
@ -23,7 +22,7 @@ func New() progress.Writer {
pw.Style().Visibility.Speed = true
pw.Style().Visibility.SpeedOverall = true
pw.Style().Options.TimeInProgressPrecision = time.Millisecond
pw.Style().Options.SpeedOverallFormatter = utils.Byte.FormatBinaryBytes
pw.Style().Options.SpeedOverallFormatter = formatter
pw.Style().Options.ErrorString = color.RedString("failed!")
pw.Style().Options.DoneString = color.GreenString("done!")

View file

@ -2,13 +2,12 @@ package prog
import (
"github.com/fatih/color"
"github.com/iyear/tdl/pkg/utils"
"github.com/jedib0t/go-pretty/v6/progress"
)
func AppendTracker(pw progress.Writer, message string, total int64) *progress.Tracker {
func AppendTracker(pw progress.Writer, formatter progress.UnitsFormatter, message string, total int64) *progress.Tracker {
units := progress.UnitsBytes
units.Formatter = utils.Byte.FormatBinaryBytes
units.Formatter = formatter
tracker := progress.Tracker{
Message: color.BlueString(message),

View file

@ -16,6 +16,8 @@ import (
"time"
)
var formatter = utils.Byte.FormatBinaryBytes
type Uploader struct {
client *tg.Client
pw progress.Writer
@ -27,7 +29,7 @@ type Uploader struct {
func New(client *tg.Client, partSize int, threads int, iter Iter) *Uploader {
return &Uploader{
client: client,
pw: prog.New(),
pw: prog.New(formatter),
partSize: partSize,
threads: threads,
iter: iter,
@ -85,7 +87,7 @@ func (u *Uploader) upload(ctx context.Context, item *Item) error {
_ = R.Close()
}(item.R)
tracker := prog.AppendTracker(u.pw, item.Name, item.Size)
tracker := prog.AppendTracker(u.pw, formatter, item.Name, item.Size)
up := uploader.NewUploader(u.client).
WithPartSize(u.partSize).WithThreads(u.threads).WithProgress(&_progress{tracker: tracker})