2 syncer -- stateful file/device data syncer.
3 Copyright (C) 2015-2020 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 CharChanged = byte('%')
28 CharUnchanged = byte('.')
40 func NewPrinter(total int64) *Printer {
41 p := Printer{total: total}
42 maxLen := len(fmt.Sprintf("%d", total))
43 p.frmt = "%" + strconv.Itoa(maxLen) + "d/%d %02d%% %s\n"
44 p.bufSize = BufLine - (2*maxLen + 6)
45 p.outBuf = make([]byte, 0, p.bufSize)
49 func (p *Printer) Changed() {
52 p.outBuf = append(p.outBuf, CharChanged)
56 func (p *Printer) Unchanged() {
58 p.outBuf = append(p.outBuf, CharUnchanged)
62 func (p *Printer) Output() {
63 if !(len(p.outBuf) == p.bufSize || p.i == p.total) {
66 fmt.Printf(p.frmt, p.i, p.total, 100*p.changed/p.i, string(p.outBuf))
67 p.outBuf = p.outBuf[:0]