From: Sergey Matveev Date: Sun, 20 Dec 2015 15:50:41 +0000 (+0300) Subject: Pretty output printer X-Git-Tag: v0.1.0~9 X-Git-Url: http://www.git.stargrave.org/?p=syncer.git;a=commitdiff_plain;h=116f382ec640820a94cbb37c0ad3b933d4bfd48a Pretty output printer --- diff --git a/printer.go b/printer.go new file mode 100644 index 0000000..0313d61 --- /dev/null +++ b/printer.go @@ -0,0 +1,69 @@ +/* +syncer -- stateful file/device data syncer. +Copyright (C) 2015-2016 Sergey Matveev + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package main + +import ( + "fmt" + "strconv" +) + +const ( + BufLine = 80 + CharChanged = byte('%') + CharUnchanged = byte('.') +) + +type Printer struct { + i int64 + total int64 + changed int64 + frmt string + bufSize int + outBuf []byte +} + +func NewPrinter(total int64) *Printer { + p := Printer{total: total} + maxLen := len(fmt.Sprintf("%d", total)) + p.frmt = "%" + strconv.Itoa(maxLen) + "d/%d %02d%% %s\n" + p.bufSize = BufLine - (2*maxLen + 6) + p.outBuf = make([]byte, 0, p.bufSize) + return &p +} + +func (p *Printer) Changed() { + p.i++ + p.changed++ + p.outBuf = append(p.outBuf, CharChanged) + p.Output() +} + +func (p *Printer) Unchanged() { + p.i++ + p.outBuf = append(p.outBuf, CharUnchanged) + p.Output() +} + +func (p *Printer) Output() { + if !(len(p.outBuf) == p.bufSize || p.i == p.total) { + return + } + fmt.Printf(p.frmt, p.i, p.total, 100*p.changed/p.i, string(p.outBuf)) + p.outBuf = p.outBuf[:0] +} diff --git a/syncer.go b/syncer.go index 3a9e68a..dc48314 100644 --- a/syncer.go +++ b/syncer.go @@ -1,6 +1,6 @@ /* syncer -- stateful file/device data syncer. -Copyright (C) 2015 Sergey Matveev +Copyright (C) 2015-2016 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -45,11 +45,6 @@ type SyncEvent struct { data []byte } -func prn(s string) { - os.Stdout.Write([]byte(s)) - os.Stdout.Sync() -} - func main() { flag.Parse() bs := *blkSize * int64(1<<10) @@ -155,7 +150,7 @@ func main() { syncs := make(chan chan SyncEvent, workers) // Writer - prn("[") + prn := NewPrinter(blocks) finished := make(chan struct{}) go func() { var event SyncEvent @@ -188,10 +183,10 @@ func main() { sumState := state[i*blake2b.Size : i*blake2b.Size+blake2b.Size] if bytes.Compare(sumState, sum[:]) != 0 { sync <- SyncEvent{i, buf, buf[:n]} - prn("%") + prn.Changed() } else { sync <- SyncEvent{i, buf, nil} - prn(".") + prn.Unchanged() } copy(sumState, sum[:]) close(sync) @@ -199,7 +194,7 @@ func main() { } close(syncs) <-finished - prn("]\n") + prn.Output() log.Println("Saving state") stateFile.Write(state)