-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
103 lines (94 loc) · 3.02 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
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
/**
* Copyright (c) 2024 Peking University and Peking University
* Changsha Institute for Computing and Digital Economy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cranetoolkit
import (
"strings"
"github.com/olekukonko/tablewriter"
)
func SetBorderlessTable(table *tablewriter.Table) {
table.SetBorder(false)
table.SetAutoFormatHeaders(true)
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderLine(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetTablePadding(" ")
table.SetNoWhiteSpace(true)
}
func SetBorderTable(table *tablewriter.Table) {
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetAutoFormatHeaders(true)
table.SetAutoWrapText(true)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("|")
table.SetTablePadding("\t")
}
func FormatTable(tableOutputWidth []int, tableHeader []string,
tableData [][]string) (formatTableHeader []string, formatTableData [][]string) {
for i, h := range tableHeader {
if tableOutputWidth[i] != -1 {
padLength := tableOutputWidth[i] - len(h)
if padLength >= 0 {
tableHeader[i] = h + strings.Repeat(" ", padLength)
} else {
tableHeader[i] = h[:tableOutputWidth[i]]
}
}
}
for i, row := range tableData {
for j, cell := range row {
if tableOutputWidth[j] != -1 {
padLength := tableOutputWidth[j] - len(cell)
if padLength >= 0 {
tableData[i][j] = cell + strings.Repeat(" ", padLength)
} else {
tableData[i][j] = cell[:tableOutputWidth[j]]
}
}
}
}
return tableHeader, tableData
}
// Trim the cell of the table, if the cell is longer than 30 characters,
// the rest of the characters will be replaced by `...`.
// `excepts` means do not trim the specified columns
func TrimTableExcept(rows *[][]string, excepts ...int) {
removal := make(map[int]bool)
for _, except := range excepts {
removal[except] = true
}
for i, row := range *rows {
for j, cell := range row {
if removal[j] {
continue
}
if len(cell) > 30 {
(*rows)[i][j] = cell[:30] + "..."
}
}
}
}
// Trim the cell of the table, if the cell is longer than 30 characters,
// the rest of the characters will be replaced by `...`.
func TrimTable(rows *[][]string) {
TrimTableExcept(rows)
}