-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
160 lines (135 loc) · 3.53 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
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
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This module implements a simple ASCII table formatter for printing
tabular values into a text terminal.
Example usage:
func main() {
// building a table
t := MakeTable([]string{"Name", "Motto", "Age"})
t.AddRow([]string{"Joe Forrester", "Trains are much better than cars", "40"})
t.AddRow([]string{"Jesus", "Read the bible", "2018"})
// using the table:
t.WriteTo(os.Stdout)
}
*/
package asciitable
import (
"bytes"
"fmt"
"strings"
)
type column struct {
width int
title string
}
type PrintOptions int
type Table struct {
columns []column
rows [][]string
}
// MakeTable creates a new instance of the table with a given title
func MakeTable(headers []string) Table {
t := MakeHeadlessTable(len(headers))
for i := range t.columns {
t.columns[i].title = headers[i]
t.columns[i].width = len(headers[i])
}
return t
}
// MakeTable creates a new instance of the table without a title,
// but the number of columns must be set
func MakeHeadlessTable(columnCount int) Table {
return Table{
columns: make([]column, columnCount),
rows: make([][]string, 0),
}
}
// Body returns the fully formatted table body as a buffer
func (t *Table) Body() *bytes.Buffer {
var (
padding string
buf bytes.Buffer
)
for _, row := range t.rows {
for columnIndex, cell := range row {
padding = strings.Repeat(" ", t.columns[columnIndex].width-len(cell)+1)
fmt.Fprintf(&buf, "%s%s", cell, padding)
}
fmt.Fprintln(&buf, "")
}
return &buf
}
// Header returns the fully formatted header as a buffer
func (t *Table) Header() *bytes.Buffer {
var (
buf bytes.Buffer
padding string
)
for i := range t.columns {
title := t.columns[i].title
padding = strings.Repeat(" ", t.columns[i].width-len(title)+1)
fmt.Fprintf(&buf, "%s%s", title, padding)
}
return &buf
}
// ColumnWidths returns the slice of ints that are the widths of each column
func (t *Table) ColumnWidths() []int {
retval := make([]int, len(t.columns))
for i := range t.columns {
retval[i] = t.columns[i].width
}
return retval
}
func (t *Table) AddRow(row []string) {
limit := min(len(row), len(t.columns))
for i := 0; i < limit; i++ {
cellWidth := len(row[i])
t.columns[i].width = max(cellWidth, t.columns[i].width)
}
t.rows = append(t.rows, row[:limit])
}
// WriteTo prints the table to the given writer
func (t *Table) AsBuffer() *bytes.Buffer {
var buf bytes.Buffer
// the hearder:
if !t.IsHeadless() {
fmt.Fprintf(&buf, "%s\n", t.Header().String())
// the separator:
for _, w := range t.ColumnWidths() {
fmt.Fprintf(&buf, "%s ", strings.Repeat("-", w))
}
buf.WriteString("\n")
}
// the body:
fmt.Fprintf(&buf, "%s", t.Body().String())
return &buf
}
// IsHeadless returns 'true' if none of the table title cells contains any text
func (t *Table) IsHeadless() bool {
total := 0
for i := range t.columns {
total += len(t.columns[i].title)
}
return total == 0
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}