-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprise.go
153 lines (128 loc) · 2.92 KB
/
reprise.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Package reprise provides can provide consistent banner output for Go CLI applications
package reprise
import (
"fmt"
)
// Banner comprises the properties and methods required to render a banner
type Banner struct {
Name string
Description string
Version string
GoVersion string
WebsiteURL string
VcsURL string
VcsName string
EmailAddress string
BorderChar string
lastLen int
maxLen int
}
// WriteSimple is the only exported implementation of a banner at this time
func WriteSimple(b *Banner) {
b.maxLen = b.maxString()
if b.BorderChar == "" {
b.BorderChar = "*"
}
b.writeNewLine()
b.writeBar()
b.writeNameDescription()
b.writeUnderLine()
b.writeVersion()
b.writeGoBuildVersion()
b.writeWebsite()
b.writeVCS()
b.writeEmail()
b.writeBar()
b.writeNewLine()
}
// unexported receivers from here
func (b *Banner) maxString() int {
mS := 0
// description
cS := len(b.Name) + len(" : ") + len(b.Description) + 4
if cS > mS {
mS = cS
}
// version
cS = len("Version: ") + len(b.Version) + 4
if cS > mS {
mS = cS
}
// go build version
cS = len("Go Build Version: ") + len(b.GoVersion) + 4
if cS > mS {
mS = cS
}
// website
cS = len("Website: ") + len(b.WebsiteURL) + 4
if cS > mS {
mS = cS
}
// vcs
cS = len(b.VcsName) + len(": ") + len(b.VcsURL) + 4
if cS > mS {
mS = cS
}
// email
cS = len("Email: ") + len(b.EmailAddress) + 4
if cS > mS {
mS = cS
}
return mS
}
func (b *Banner) writeBar() {
bar := ""
for i := 0; i < b.maxLen; i++ {
bar += b.BorderChar
}
fmt.Println(bar)
}
func (b *Banner) writeNameDescription() {
textDesc := fmt.Sprintf("%s - %s", b.Name, b.Description)
fmt.Println(b.buildLine(textDesc))
}
func (b *Banner) writeBlankLine() {
fmt.Println(b.buildLine(""))
}
func (b *Banner) writeNewLine() {
fmt.Println("")
}
func (b *Banner) writeVersion() {
version := fmt.Sprintf("Version: %s", b.Version)
fmt.Println(b.buildLine(version))
}
func (b *Banner) writeGoBuildVersion() {
goVersion := fmt.Sprintf("Go Build Version: %s", b.GoVersion)
fmt.Println(b.buildLine(goVersion))
}
func (b *Banner) writeWebsite() {
website := fmt.Sprintf("Website: %s", b.WebsiteURL)
fmt.Println(b.buildLine(website))
}
func (b *Banner) writeVCS() {
vcs := fmt.Sprintf("%s: %s", b.VcsName, b.VcsURL)
fmt.Println(b.buildLine(vcs))
}
func (b *Banner) writeEmail() {
email := fmt.Sprintf("Email: %s", b.EmailAddress)
fmt.Println(b.buildLine(email))
}
func (b *Banner) writeUnderLine() {
underline := ""
for i := 0; i < b.lastLen; i++ {
underline += "-"
}
fmt.Println(b.buildLine(underline))
}
func (b *Banner) buildLine(text string) string {
pre := fmt.Sprintf("%s ", b.BorderChar)
post := fmt.Sprintf(" %s", b.BorderChar)
b.lastLen = len(text)
padSpace := b.maxLen - len(pre) - len(text) - len(post)
space := ""
for i := 0; i < padSpace; i++ {
space += " "
}
line := fmt.Sprintf("%s%s%s%s", pre, text, space, post)
return line
}