This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_printer.go
109 lines (89 loc) · 2.75 KB
/
table_printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"fmt"
"os"
"text/tabwriter"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// TablePrinter is a printer of Counter object
type TablePrinter struct {
flushMilliSec int64
topnPrint int
lastFlushedDatetime time.Time
tscreen tcell.Screen
}
func (printer *TablePrinter) print(counter Counter, nBytes int64, nChunks int64, forcePrint bool) {
printer.tscreen.Clear()
currentDatetime := time.Now()
elapsedNanoSecondsAfterFlushed := currentDatetime.Sub(printer.lastFlushedDatetime).Nanoseconds()
if !forcePrint && elapsedNanoSecondsAfterFlushed < 1000*1000*printer.flushMilliSec {
return
}
var buffer bytes.Buffer
writer := tabwriter.NewWriter(&buffer, 0, 0, 2, ' ', tabwriter.Debug)
countingResult := counter.getCountingResult()
end := Min(len(countingResult), printer.topnPrint)
topnItems := extractTopnItems(countingResult, end)
formatter := message.NewPrinter(language.English)
maxCountLength := len(formatter.Sprintf("%v", topnItems[0].value))
countFormat := formatter.Sprintf("%%%dv", maxCountLength+1)
for _, c := range topnItems {
count := formatter.Sprintf(countFormat, c.value)
line := fmt.Sprintf(" %v\t%v\n", c.key, count)
writer.Write([]byte(line))
}
writer.Write([]byte(createCaption(nBytes, counter.getSize())))
writer.Flush()
printer.printOnTscreen(buffer.String())
printer.lastFlushedDatetime = currentDatetime
}
func (printer TablePrinter) printOnTscreen(content string) {
x := 0
y := 0
for _, r := range []rune(content) {
if r != '\n' {
printer.tscreen.SetCell(x, y, tcell.StyleDefault, r)
x++
} else {
x = 0
y++
}
}
printer.tscreen.Show()
}
func (printer *TablePrinter) exit(counter Counter) {
printer.tscreen.Fini()
}
func (printer *TablePrinter) initializeTscreen() {
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
printer.tscreen, _ = tcell.NewScreen()
if err := printer.tscreen.Init(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
encoding.Register()
printer.tscreen.SetStyle(tcell.StyleDefault)
printer.tscreen.Clear()
printer.tscreen.Show()
}
// NewTablePrinter is a constructor of TablePrinter
func NewTablePrinter(flushMilliSec int64, topnPrint int) *TablePrinter {
printer := &TablePrinter{}
printer.lastFlushedDatetime = time.Now()
printer.flushMilliSec = flushMilliSec
printer.topnPrint = topnPrint
printer.initializeTscreen()
return printer
}
func createCaption(nBytes int64, nItems uint64) string {
byteSize := bytefmt.ByteSize(uint64(nBytes))
counterSizeString := bytefmt.ByteSize(nItems)
caption := fmt.Sprintf("Read: %v, size of counter: %v", byteSize, counterSizeString)
return caption
}