-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
105 lines (93 loc) · 2.11 KB
/
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
package main
import (
"fmt"
"io"
"math/bits"
"strings"
)
// print item
const (
PrintURL uint = 1 << iota
PrintName
PrintEmail
PrintSigningKey
)
// print item
const (
PrintDefault uint = 0
PrintALL = PrintURL | PrintName | PrintEmail | PrintSigningKey
)
// Printer print formatter
type Printer struct {
flag uint
writer io.Writer
}
// NewPrinter inti Printer
func NewPrinter(flag uint, writer io.Writer) *Printer {
if flag == PrintDefault {
flag = PrintALL
}
return &Printer{
flag: flag,
writer: writer,
}
}
func (p Printer) buf(user *User) []string {
var buf []string
if p.flag&PrintURL == PrintURL {
buf = append(buf, fmt.Sprintf("URL: %s", user.URL))
}
if p.flag&PrintName == PrintName {
buf = append(buf, fmt.Sprintf("Name: %s", user.Name))
}
if p.flag&PrintEmail == PrintEmail {
buf = append(buf, fmt.Sprintf("Email: %s", user.Email))
}
if p.flag&PrintSigningKey == PrintSigningKey {
buf = append(buf, strings.TrimSpace(fmt.Sprintf("SigningKey: %s", user.SigningKey)))
}
if p.flag == PrintALL {
buf = append(buf, fmt.Sprintf("Hash: %s", user.Hash()))
}
return buf
}
// PrintUser print user
func (p Printer) PrintUser(user *User) Printer {
fmt.Fprintln(p.writer, strings.Join(p.buf(user), " "))
return p
}
// PrintUsers print users
func (p Printer) PrintUsers(users []*User) Printer {
lines := make([][]string, len(users))
colMaxLen := make([]int, bits.OnesCount(p.flag)+1)
for i, u := range users {
lines[i] = p.buf(u)
for j, s := range lines[i] {
l := len(s)
if colMaxLen[j] < l {
colMaxLen[j] = l
}
}
}
for _, line := range lines {
buf := make([]string, len(line))
for i, col := range line {
buf = append(
buf,
fmt.Sprintf("%-0"+fmt.Sprint(colMaxLen[i]+2)+"s", col),
)
}
fmt.Fprintln(p.writer, strings.TrimSpace(strings.Join(buf, "")))
}
return p
}
// Println print message with line feed
func (p Printer) Println(message string) Printer {
fmt.Fprintln(p.writer, message)
return p
}
// Printf print formatted message
func (p Printer) Printf(format string, a ...interface{}) Printer {
fmt.Fprintf(p.writer, format, a...)
return p
}