2 syncer -- stateful file/device data syncer.
3 Copyright (C) 2015-2018 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 CharChanged = byte('%')
29 CharUnchanged = byte('.')
41 func NewPrinter(total int64) *Printer {
42 p := Printer{total: total}
43 maxLen := len(fmt.Sprintf("%d", total))
44 p.frmt = "%" + strconv.Itoa(maxLen) + "d/%d %02d%% %s\n"
45 p.bufSize = BufLine - (2*maxLen + 6)
46 p.outBuf = make([]byte, 0, p.bufSize)
50 func (p *Printer) Changed() {
53 p.outBuf = append(p.outBuf, CharChanged)
57 func (p *Printer) Unchanged() {
59 p.outBuf = append(p.outBuf, CharUnchanged)
63 func (p *Printer) Output() {
64 if !(len(p.outBuf) == p.bufSize || p.i == p.total) {
67 fmt.Printf(p.frmt, p.i, p.total, 100*p.changed/p.i, string(p.outBuf))
68 p.outBuf = p.outBuf[:0]