2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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/v5/uilive"
32 uilive.Out = os.Stderr
35 var progressBars = make(map[string]*ProgressBar)
36 var progressBarsLock sync.RWMutex
38 type ProgressBar struct {
46 func ProgressBarNew(initial, full int64) *ProgressBar {
57 func (pb ProgressBar) Render(what string, size int64) {
58 now := time.Now().UTC()
59 timeDiff := now.Sub(pb.started).Seconds()
63 percentage := int64(100)
65 percentage = 100 * size / pb.full
68 pb.w, "%s %s %s/%s %d%% (%s/sec)\n",
69 now.Format(time.RFC3339), what,
70 humanize.IBytes(uint64(size)),
71 humanize.IBytes(uint64(pb.full)),
73 humanize.IBytes(uint64(float64(size-pb.initial)/timeDiff)),
77 func (pb ProgressBar) Kill() {
87 ) (written int64, err error) {
88 buf := make([]byte, EncBlkSize)
92 nr, er = src.Read(buf)
94 nw, ew = dst.Write(buf[:nr])
98 Progress(prgrsPrefix, append(les, LE{"Size", written}))
106 err = io.ErrShortWrite
120 func Progress(prefix string, les LEs) {
124 for _, le := range les {
129 fullsize = le.V.(int64)
134 progressBarsLock.RLock()
135 pb, exists := progressBars[pkt]
136 progressBarsLock.RUnlock()
138 progressBarsLock.Lock()
139 pb = ProgressBarNew(size, fullsize)
140 progressBars[pkt] = pb
141 progressBarsLock.Unlock()
144 if len(what) >= Base32Encoded32Len { // Base32 encoded
145 what = what[:16] + ".." + what[len(what)-16:]
147 what = prefix + " " + what
148 pb.Render(what, size)
149 if size >= fullsize {
151 progressBarsLock.Lock()
152 delete(progressBars, pkt)
153 progressBarsLock.Unlock()