forked from CiscoCloud/columnize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumnize.go
More file actions
293 lines (255 loc) · 8.11 KB
/
columnize.go
File metadata and controls
293 lines (255 loc) · 8.11 KB
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
package columnize
import (
"fmt"
"strings"
"unicode"
// "github.com/TwiN/go-color"
"github.com/DeMille/termsize"
)
// Cellcolor slice
type ColorList []func(s any) string
type Config struct {
// The string by which the lines of input will be split.
Delim string
// The string by which columns of output will be separated.
Glue string
// The string by which columns of output will be prefixed.
Prefix string
// A replacement string to replace empty fields
Empty string
// Maximum width of output; set to AUTO to use actual console width
OutputWidth int
// Maximum width of each column
MaxWidth []int
// Header colors
// Function to call that returns colored output
// Check out github.com/TwiN/go-color color.In[Color]() functions
// Pointer incase user never sets it
HeaderColors *ColorList
// Body colors
BodyColors *ColorList
}
const (
AUTO = -1
)
// Returns a Config with default values.
func DefaultConfig() *Config {
return &Config{
Delim: "|",
Glue: " ",
Prefix: "",
OutputWidth: -999,
MaxWidth: []int{},
HeaderColors: nil,
BodyColors: nil,
}
}
// Returns a list of elements, each representing a single item which will
// belong to a column of output.
func getElementsFromLine(config *Config, line string) []interface{} {
elements := make([]interface{}, 0)
for _, field := range strings.Split(line, config.Delim) {
value := strings.TrimSpace(field)
if value == "" && config.Empty != "" {
value = config.Empty
}
elements = append(elements, value)
}
return elements
}
// Examines a list of strings and determines how wide each column should be
// considering all of the elements that need to be printed within it.
func getWidthsFromLines(config *Config, lines []string) []int {
widths := calculateColumnWidths(config, lines)
outputWidth := getOutputWidth(config)
widths = adjustWidths(config, widths, outputWidth)
return widths
}
// Calculate column widths by comparing data width and MaxWidth
func calculateColumnWidths(config *Config, lines []string) (widths []int) {
for _, line := range lines {
elems := getElementsFromLine(config, line)
for i := 0; i < len(elems); i++ {
lenElem := len(elems[i].(string))
if i < len(config.MaxWidth) {
if config.MaxWidth[i] < 0 {
fmt.Printf("Columnize: negative MaxWidth value not supported - please use OutputWidth\n")
} else if config.MaxWidth[i] > 0 && config.MaxWidth[i] < lenElem {
lenElem = config.MaxWidth[i]
}
}
if len(widths) <= i {
widths = append(widths, lenElem)
} else if widths[i] < lenElem {
widths[i] = lenElem
}
}
}
return
}
// Get output width specification
func getOutputWidth(config *Config) (outputWidth int) {
outputWidth = config.OutputWidth
if outputWidth == AUTO {
var e error
if outputWidth, e = GetConsoleWidth(); e != nil {
fmt.Printf("Unable to set AUTO OutputWidth: %s\n", e.Error())
}
}
return
}
// If the output width is restricted and the output line will exceed that width,
// attempt to meet the restriction by adjusting the width of the rightmost
// unrestricted column, or the rightmost column if all columns are restricted.
func adjustWidths(config *Config, widths []int, outputWidth int) []int {
if outputWidth > 0 {
totalLineWidth := len(config.Prefix) + len(config.Glue)*(len(widths)-1)
for _, width := range widths {
totalLineWidth += width
}
if totalLineWidth > outputWidth {
adjIndex := -1
for i := len(widths) - 1; i >= 0; i-- {
if i >= len(config.MaxWidth) || config.MaxWidth[i] <= 0 {
adjIndex = i
break
}
}
if adjIndex < 0 {
adjIndex = len(widths) - 1
}
adjustedWidth := outputWidth - (totalLineWidth - widths[adjIndex])
if adjustedWidth > 0 {
widths[adjIndex] = adjustedWidth
}
}
}
return widths
}
// Given a set of column widths and the number of columns in the current line,
// returns a sprintf-style format string which can be used to print output
// aligned properly with other lines using the same widths set.
func (c *Config) getStringFormat(widths []int, columns int, colors *ColorList) string {
// Start with the prefix, if any was given.
stringfmt := c.Prefix
// Create the format string from the discovered widths
for i := 0; i < columns && i < len(widths); i++ {
tostringfmt := ""
if i == columns-1 {
tostringfmt = "%s\n"
} else {
tostringfmt = fmt.Sprintf("%%-%ds%s", widths[i], c.Glue)
}
if colors != nil && len((*colors)) > i {
tostringfmt = (*colors)[i](tostringfmt)
}
stringfmt += tostringfmt
}
return stringfmt
}
// MergeConfig merges two config objects together and returns the resulting
// configuration. Values from the right take precedence over the left side.
func MergeConfig(a, b *Config) *Config {
var result Config = *a
// Return quickly if either side was nil
if a == nil || b == nil {
return &result
}
if b.Delim != "" {
result.Delim = b.Delim
}
if b.Glue != "" {
result.Glue = b.Glue
}
if b.Prefix != "" {
result.Prefix = b.Prefix
}
if b.Empty != "" {
result.Empty = b.Empty
}
if b.OutputWidth >= 0 || b.OutputWidth == AUTO {
result.OutputWidth = b.OutputWidth
}
if len(b.MaxWidth) > 0 {
result.MaxWidth = b.MaxWidth
}
return &result
}
// Format is the public-facing interface that takes either a plain string
// or a list of strings and returns nicely aligned output.
func Format(lines []string, config *Config) string {
var result string
conf := MergeConfig(DefaultConfig(), config)
widths := getWidthsFromLines(conf, lines)
// Create the formatted output using the format string
for i, line := range lines {
elems := getElementsFromLine(conf, line)
extensionLineElems := []string{}
isStillDataToFormat := true
for isStillDataToFormat {
// get the right pallet up
var colorsToUse *ColorList
if config != nil {
if i == 0 {
colorsToUse = config.HeaderColors
} else {
colorsToUse = config.BodyColors
}
}
isStillDataToFormat = truncateToWidth(&elems, &extensionLineElems, widths)
stringfmt := conf.getStringFormat(widths, len(elems), colorsToUse)
result += fmt.Sprintf(stringfmt, elems...)
}
}
// Remove trailing newline without removing leading/trailing space
if n := len(result); n > 0 && result[n-1] == '\n' {
result = result[:n-1]
}
return result
}
// Convenience function for using Columnize as easy as possible.
func SimpleFormat(lines []string) string {
return Format(lines, nil)
}
// Truncate any elements exceeding their maximum width, and save their remaining
// data for an extension line.
func truncateToWidth(elems *[]interface{}, extensionLineElems *[]string, widths []int) (isStillDataToFormat bool) {
// If this an extension line, make its list of elements current
if len(*extensionLineElems) > 0 {
for i, elem := range *extensionLineElems {
(*elems)[i] = elem
}
*extensionLineElems = []string{}
}
// Examine each element to determine if it exceeds its maximum allowed width.
// If so, truncate it at the closest whitespace to the limit and save its remaining
// data for the next extension line.
for i, elem := range *elems {
stringElem := strings.TrimSpace(fmt.Sprintf("%s", elem))
if len(stringElem) > widths[i] {
isStillDataToFormat = true
splitPoint := widths[i]
for ; splitPoint > 0; splitPoint-- {
if unicode.IsSpace(rune(stringElem[splitPoint])) {
break
}
}
if splitPoint == 0 {
splitPoint = widths[i]
}
(*elems)[i] = strings.TrimSpace(stringElem[:splitPoint])
if len(*extensionLineElems) == 0 {
(*extensionLineElems) = make([]string, len(*elems))
}
(*extensionLineElems)[i] = strings.TrimSpace(stringElem[splitPoint:])
}
}
return
}
// Get the width of the console
func GetConsoleWidth() (width int, e error) {
if e = termsize.Init(); e == nil {
width, _, e = termsize.Size()
}
return
}