-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
67 lines (61 loc) · 1.36 KB
/
table.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
/*
* Copyright (c) 2023 Brandon Jordan
*/
package ttuy
import (
"fmt"
)
var combined []Row
// Table prints rows with headers in a text table
func Table(headers []string, rows []Row) {
combineHeadersRows(&headers, &rows)
checkEven(&combined)
calcDimensions(&combined)
var line = makeLine()
fmt.Println(line)
fmt.Print(Style("|", Dim))
for h, header := range headers {
fmt.Printf(" %s", header)
if len(header) < dimensions[h] {
var spaceDiff = dimensions[h] - len(header)
for i := 0; i < spaceDiff; i++ {
fmt.Print(" ")
}
}
fmt.Print(Style(" |", Dim))
}
fmt.Print(eol)
fmt.Println(line)
for _, row := range rows {
fmt.Print(Style("|", Dim))
for c, column := range row.Columns {
fmt.Printf(" %s", column)
if len(column) < dimensions[c] {
var spaceDiff = dimensions[c] - len(column)
for i := 0; i < spaceDiff; i++ {
fmt.Print(" ")
}
}
fmt.Print(Style(" |", Dim))
}
fmt.Print(eol)
}
fmt.Println(line)
}
func combineHeadersRows(headers *[]string, rows *[]Row) {
var headersRow = Row{Columns: *headers}
combined = append(combined, headersRow)
combined = append(combined, *rows...)
}
func makeLine() (line string) {
line = "+"
for c := range combined[0].Columns {
var columnLength = dimensions[c] + 2
for idx := 0; idx < columnLength; idx++ {
line += "-"
}
line += "+"
}
line = Style(line, Dim)
return
}