2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/dustin/go-humanize"
28 "go.cypherpunks.ru/nncp/v8/uilive"
32 uilive.Out = os.Stderr
35 var progressBars = make(map[string]*ProgressBar)
36 var progressBarsLock sync.RWMutex
38 type ProgressBar struct {
45 func ProgressBarNew(initial, full int64) *ProgressBar {
56 func (pb ProgressBar) Render(what string, size int64) {
57 now := time.Now().UTC()
58 timeDiff := now.Sub(pb.started).Seconds()
62 percentage := int64(100)
64 percentage = 100 * size / pb.full
67 pb.w, "%s %s %s/%s %d%% (%s/sec)\n",
68 now.Format(time.RFC3339), what,
69 humanize.IBytes(uint64(size)),
70 humanize.IBytes(uint64(pb.full)),
72 humanize.IBytes(uint64(float64(size-pb.initial)/timeDiff)),
76 func (pb ProgressBar) Kill() {
86 ) (written int64, err error) {
87 buf := make([]byte, EncBlkSize)
91 nr, er = src.Read(buf)
93 nw, ew = dst.Write(buf[:nr])
97 Progress(prgrsPrefix, append(les, LE{"Size", written}))
105 err = io.ErrShortWrite
117 for _, le := range les {
118 if le.K == "FullSize" {
119 if le.V.(int64) == 0 {
120 Progress(prgrsPrefix, append(
121 les, LE{"Size", written}, LE{"FullSize", written},
131 func Progress(prefix string, les LEs) {
135 for _, le := range les {
140 fullsize = le.V.(int64)
145 progressBarsLock.RLock()
146 pb := progressBars[pkt]
147 progressBarsLock.RUnlock()
149 progressBarsLock.Lock()
150 pb = ProgressBarNew(size, fullsize)
151 progressBars[pkt] = pb
152 progressBarsLock.Unlock()
155 if len(what) >= Base32Encoded32Len { // Base32 encoded
156 what = what[:16] + ".." + what[len(what)-16:]
158 what = prefix + " " + what
159 pb.Render(what, size)
160 if fullsize != 0 && size >= fullsize {
162 progressBarsLock.Lock()
163 delete(progressBars, pkt)
164 progressBarsLock.Unlock()
168 func ProgressKill(pkt string) {
169 progressBarsLock.Lock()
170 pb := progressBars[pkt]
173 delete(progressBars, pkt)
175 progressBarsLock.Unlock()