X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fprogress.go;h=c170674334a0fff1fc2e30b9f78d75a92f9ea146;hb=5fbec42464ddeefacd99f267c8b7bfe2fa2f428a;hp=d07185749dd812670e156fb1ff800444c2c3ad0d;hpb=1d2ce674b042d07fd9b37a46578c8b62bb0345b7;p=nncp.git diff --git a/src/progress.go b/src/progress.go index d071857..c170674 100644 --- a/src/progress.go +++ b/src/progress.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2019 Sergey Matveev +Copyright (C) 2016-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,12 +21,11 @@ import ( "fmt" "io" "os" - "strings" "sync" "time" "github.com/dustin/go-humanize" - "go.cypherpunks.ru/nncp/v5/uilive" + "go.cypherpunks.ru/nncp/v8/uilive" ) func init() { @@ -38,7 +37,6 @@ var progressBarsLock sync.RWMutex type ProgressBar struct { w *uilive.Writer - hash string started time.Time initial int64 full int64 @@ -82,7 +80,8 @@ func (pb ProgressBar) Kill() { func CopyProgressed( dst io.Writer, src io.Reader, - sds SDS, + prgrsPrefix string, + les LEs, showPrgrs bool, ) (written int64, err error) { buf := make([]byte, EncBlkSize) @@ -95,8 +94,7 @@ func CopyProgressed( if nw > 0 { written += int64(nw) if showPrgrs { - sds["size"] = written - Progress(sds) + Progress(prgrsPrefix, append(les, LE{"Size", written})) } } if ew != nil { @@ -115,37 +113,64 @@ func CopyProgressed( break } } + if showPrgrs { + for _, le := range les { + if le.K == "FullSize" { + if le.V.(int64) == 0 { + Progress(prgrsPrefix, append( + les, LE{"Size", written}, LE{"FullSize", written}, + )) + } + break + } + } + } return } -func Progress(sds SDS) { - pkt := sds["pkt"].(string) +func Progress(prefix string, les LEs) { var size int64 - if sizeI, exists := sds["size"]; exists { - size = sizeI.(int64) + var fullsize int64 + var pkt string + for _, le := range les { + switch le.K { + case "Size": + size = le.V.(int64) + case "FullSize": + fullsize = le.V.(int64) + case "Pkt": + pkt = le.V.(string) + } } - fullsize := sds["fullsize"].(int64) progressBarsLock.RLock() - pb, exists := progressBars[pkt] + pb := progressBars[pkt] progressBarsLock.RUnlock() - if !exists { + if pb == nil { progressBarsLock.Lock() pb = ProgressBarNew(size, fullsize) progressBars[pkt] = pb progressBarsLock.Unlock() } what := pkt - if len(what) >= 52 { // Base32 encoded + if len(what) >= Base32Encoded32Len { // Base32 encoded what = what[:16] + ".." + what[len(what)-16:] } - if xx, exists := sds["xx"]; exists { - what = strings.Title(xx.(string)) + " " + what - } + what = prefix + " " + what pb.Render(what, size) - if size >= fullsize { + if fullsize != 0 && size >= fullsize { pb.Kill() progressBarsLock.Lock() delete(progressBars, pkt) progressBarsLock.Unlock() } } + +func ProgressKill(pkt string) { + progressBarsLock.Lock() + pb := progressBars[pkt] + if pb != nil { + pb.Kill() + delete(progressBars, pkt) + } + progressBarsLock.Unlock() +}