]> Sergey Matveev's repositories - syncer.git/blob - printer.go
Raise copyright years
[syncer.git] / printer.go
1 /*
2 syncer -- stateful file/device data syncer.
3 Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "fmt"
23         "strconv"
24 )
25
26 const (
27         BufLine       = 80
28         CharChanged   = byte('%')
29         CharUnchanged = byte('.')
30 )
31
32 type Printer struct {
33         i       int64
34         total   int64
35         changed int64
36         frmt    string
37         bufSize int
38         outBuf  []byte
39 }
40
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)
47         return &p
48 }
49
50 func (p *Printer) Changed() {
51         p.i++
52         p.changed++
53         p.outBuf = append(p.outBuf, CharChanged)
54         p.Output()
55 }
56
57 func (p *Printer) Unchanged() {
58         p.i++
59         p.outBuf = append(p.outBuf, CharUnchanged)
60         p.Output()
61 }
62
63 func (p *Printer) Output() {
64         if !(len(p.outBuf) == p.bufSize || p.i == p.total) {
65                 return
66         }
67         fmt.Printf(p.frmt, p.i, p.total, 100*p.changed/p.i, string(p.outBuf))
68         p.outBuf = p.outBuf[:0]
69 }