-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtabprinter.go
60 lines (51 loc) · 1.18 KB
/
tabprinter.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
package pretty
import (
"fmt"
"os"
"text/tabwriter"
)
// A TabPrinter is an object that allows printing tab-aligned words on multiple lines,
// up to a maximum number per line
type TabPrinter struct {
w *tabwriter.Writer
current, max int
}
// create a TabPrinter
//
// max specifies the maximum number of 'words' per line
func NewTabPrinter(max int) *TabPrinter {
tp := &TabPrinter{w: new(tabwriter.Writer), max: max}
tp.w.Init(os.Stdout, 0, 8, 1, '\t', 0)
return tp
}
// update tab width (minimal space between words)
//
func (tp *TabPrinter) TabWidth(n int) {
tp.w.Init(os.Stdout, n, 0, 1, ' ', 0)
}
// print a 'word'
//
// when the maximum number of words per lines is reached, this will print the formatted line
func (tp *TabPrinter) Print(arg interface{}) {
if tp.current > 0 {
if (tp.current % tp.max) == 0 {
fmt.Fprintln(tp.w, "")
tp.w.Flush()
tp.current = 0
} else {
fmt.Fprint(tp.w, "\t")
}
}
tp.current++
fmt.Fprint(tp.w, arg)
}
// print current line
//
// terminate current line and print - call this after all words have been printed
func (tp *TabPrinter) Println() {
if tp.current > 0 {
fmt.Fprintln(tp.w, "")
tp.w.Flush()
}
tp.current = 0
}