/
frontend.go
314 lines (271 loc) · 7.92 KB
/
frontend.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package qiic
import (
"fmt"
"strconv"
"strings"
eastasianwidth "github.com/moznion/go-unicode-east-asian-width"
)
const (
accessNumName = "A No"
maxAccessNumHalfLen = 4
titleName = "TITLE"
maxTitleHalfLen = 51
tagName = "TAG"
maxTagHalfLen = 21
likeName = "LIKE"
maxLikeHalfLen = 5
)
// Column is struct of Column
type Column interface {
GetMaxHalfCharLen() int
MakeFilledInStr(splitChar string) string
MakeCenterAlignedStr() string
GenerateTurnedLines(artIndex int, art *Article) []string
}
// AccessNumColumn is struct
type AccessNumColumn struct {
Name string
MaxHalfCharLen int
}
// GetMaxHalfCharLen is func.
func (c AccessNumColumn) GetMaxHalfCharLen() int {
return c.MaxHalfCharLen
}
// MakeFilledInStr is func.
func (c AccessNumColumn) MakeFilledInStr(splitChar string) string {
return strings.Repeat(splitChar, c.MaxHalfCharLen)
}
// MakeCenterAlignedStr is func.
func (c AccessNumColumn) MakeCenterAlignedStr() string {
return CenterAligned(c.Name, c.MaxHalfCharLen)
}
// GenerateTurnedLines is func.
func (c AccessNumColumn) GenerateTurnedLines(artIndex int, art *Article) []string {
iStr := strconv.Itoa(artIndex + 1) // On Display, Finally artIndex Increments
restLen := c.MaxHalfCharLen - len(iStr)
likeLine := iStr + strings.Repeat(" ", restLen)
return []string{likeLine}
}
// TitleColumn is struct.
type TitleColumn struct {
Name string
MaxHalfCharLen int
}
// GetMaxHalfCharLen is a func.
func (c TitleColumn) GetMaxHalfCharLen() int {
return c.MaxHalfCharLen
}
// MakeFilledInStr is a func.
func (c TitleColumn) MakeFilledInStr(splitChar string) string {
return strings.Repeat(splitChar, c.MaxHalfCharLen)
}
// MakeCenterAlignedStr is a func.
func (c TitleColumn) MakeCenterAlignedStr() string {
return CenterAligned(c.Name, c.MaxHalfCharLen)
}
// GenerateTurnedLines is a func.
func (c TitleColumn) GenerateTurnedLines(artIndex int, art *Article) []string {
return MakeTurnedLines(art.Title, c.MaxHalfCharLen)
}
// TagColumn is struct.
type TagColumn struct {
Name string
MaxHalfCharLen int
}
// GetMaxHalfCharLen is a func.
func (c TagColumn) GetMaxHalfCharLen() int {
return c.MaxHalfCharLen
}
// MakeFilledInStr is a func.
func (c TagColumn) MakeFilledInStr(splitChar string) string {
return strings.Repeat(splitChar, c.MaxHalfCharLen)
}
// MakeCenterAlignedStr is a func.
func (c TagColumn) MakeCenterAlignedStr() string {
return CenterAligned(c.Name, c.MaxHalfCharLen)
}
// GenerateTurnedLines in a func.
func (c TagColumn) GenerateTurnedLines(artIndex int, art *Article) []string {
var tagsNameList []string
for _, tag := range art.Tags {
tagsNameList = append(tagsNameList, tag.Name)
}
tagsName := strings.Join(tagsNameList, ",")
return MakeTurnedLines(tagsName, c.MaxHalfCharLen)
}
// LikesColumn is a struct.
type LikesColumn struct {
Name string
MaxHalfCharLen int
}
// GetMaxHalfCharLen is func.
func (c LikesColumn) GetMaxHalfCharLen() int {
return c.MaxHalfCharLen
}
// MakeFilledInStr is a func.
func (c LikesColumn) MakeFilledInStr(splitChar string) string {
return strings.Repeat(splitChar, c.MaxHalfCharLen)
}
// MakeCenterAlignedStr is a func.
func (c LikesColumn) MakeCenterAlignedStr() string {
return CenterAligned(c.Name, c.MaxHalfCharLen)
}
// GenerateTurnedLines is a func.
func (c LikesColumn) GenerateTurnedLines(artIndex int, art *Article) []string {
iStr := strconv.Itoa(art.LikesCount)
restLen := c.MaxHalfCharLen - len(iStr)
likeLine := strings.Repeat(" ", restLen) + iStr
return []string{likeLine}
}
// Table is struct of table
type Table struct {
Columns []Column
}
// GenerateTopLine is func
func (t Table) GenerateTopLine() string {
lList := make([]string, len(t.Columns))
for i, c := range t.Columns {
lList[i] = c.MakeFilledInStr("─")
}
return "┌" + strings.Join(lList, "┬") + "┐"
}
// GenerateColumnnameLine is func
func (t Table) GenerateColumnnameLine() string {
lList := make([]string, len(t.Columns))
for i, c := range t.Columns {
lList[i] = c.MakeCenterAlignedStr()
}
return "|" + strings.Join(lList, "|") + "|"
}
// GenerateSperateLine is func
func (t Table) GenerateSperateLine() string {
lList := make([]string, len(t.Columns))
for i, c := range t.Columns {
lList[i] = c.MakeFilledInStr("─")
}
return "|" + strings.Join(lList, "|") + "|"
}
// CenterAligned is a util func
func CenterAligned(str string, max int) (ret string) {
restNum := max - len(str)
ret = strings.Repeat(" ", restNum/2) + str + strings.Repeat(" ", restNum-restNum/2)
return
}
// MakeTurnedLines is a util func
func MakeTurnedLines(str string, maxLen int) (tLines []string) {
var isJustMaxLenFlag bool
var curHalfLen int
var curLine []rune
for _, r := range str { // 'r' means rune
if eastasianwidth.IsFullwidth(r) {
curHalfLen += 2
} else {
curHalfLen++
}
if curHalfLen == maxLen {
isJustMaxLenFlag = true
} else if curHalfLen > maxLen {
// Arrange to Full and Half
if isJustMaxLenFlag == true {
tLines = append(tLines, string(curLine))
} else {
tLines = append(tLines, string(curLine)+" ")
}
// Initalize stat variables
if eastasianwidth.IsFullwidth(r) {
curHalfLen = 2
} else {
curHalfLen = 1
}
isJustMaxLenFlag = false
curLine = nil
}
curLine = append(curLine, r)
}
var spaces string
if restLen := maxLen - curHalfLen; restLen > 0 {
spaces = strings.Repeat(" ", restLen)
}
tLines = append(tLines, string(curLine)+spaces)
return
}
// FindStringsMaxLen is a util func
func FindStringsMaxLen(linesList [][]string) int {
maxLen := 1
for _, lines := range linesList {
if len(lines) > maxLen {
maxLen = len(lines)
}
}
return maxLen
}
// MakeFullLines is a semi? util func
func MakeFullLines(lines []string, maxHchar int, maxLines int) []string {
if diffLinesLen := maxLines - len(lines); diffLinesLen > 0 {
for i := 0; i < diffLinesLen; i++ {
lines = append(lines, strings.Repeat(" ", maxHchar))
}
}
return lines
}
// GenerateArticleLines is func for Rendering
func GenerateArticleLines(artIndex int, art *Article, table Table) []string {
columnsLineList := make([][]string, len(table.Columns))
for i, c := range table.Columns {
columnsLineList[i] = c.GenerateTurnedLines(artIndex, art)
}
// find max len
maxInnerLinesLen := FindStringsMaxLen(columnsLineList)
// make full lines
for i, c := range table.Columns {
columnsLineList[i] = MakeFullLines(columnsLineList[i],
c.GetMaxHalfCharLen(), maxInnerLinesLen)
}
// join the columns factor to line
artInnerLineList := make([][]string, maxInnerLinesLen)
for _, cll := range columnsLineList {
for j, cl := range cll {
artInnerLineList[j] = append(artInnerLineList[j], cl)
}
}
// make ret lines
artLines := make([]string, maxInnerLinesLen)
for i, ails := range artInnerLineList {
artLines[i] = "|" + strings.Join(ails, "|") + "|"
}
return artLines
}
// Render is interface func of frontend
func Render(arts []*Article) {
// ### Change Point Start ###
// Initialize Columns
accessNumColumn := AccessNumColumn{Name: accessNumName,
MaxHalfCharLen: maxAccessNumHalfLen}
titleColumn := TitleColumn{Name: titleName,
MaxHalfCharLen: maxTitleHalfLen}
tagColumn := TagColumn{Name: tagName,
MaxHalfCharLen: maxTagHalfLen}
likeColumn := LikesColumn{Name: likeName,
MaxHalfCharLen: maxLikeHalfLen}
// make column list
columns := []Column{accessNumColumn, titleColumn, tagColumn, likeColumn}
// ### Change Point End ###
// Register Columns to Table
table := Table{Columns: columns}
// Print Top Line (Firt Line)
fmt.Println(table.GenerateTopLine())
// Print Column Name Line (Second Line)
fmt.Println(table.GenerateColumnnameLine())
// Print Articles
for artIndex, art := range arts {
// Print Sperate Line
fmt.Println(table.GenerateSperateLine())
// Print Article Lines
artLines := GenerateArticleLines(artIndex, art, table)
for _, articleLine := range artLines {
fmt.Println(articleLine)
}
}
// Print Foote
fmt.Println(table.GenerateSperateLine())
}