/
pretty.go
359 lines (299 loc) · 10.2 KB
/
pretty.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright © 2016-2023 Wei Shen <shenwei356@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"math"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/shenwei356/stable"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
)
// prettyCmd represents the pretty command
var prettyCmd = &cobra.Command{
GroupID: "format",
Use: "pretty",
Short: "convert CSV to a readable aligned table",
Long: `convert CSV to a readable aligned table
How to:
1. First -n/--buf-rows rows are read to check the minimum and maximum widths
of each columns. You can also set the global thresholds -w/--min-width and
-W/--max-width.
1a. Cells longer than the maximum width will be wrapped (default) or
clipped (--clip).
Usually, the text is wrapped in space (-x/--wrap-delimiter). But if one
word is longer than the -W/--max-width, it will be force split.
1b. Texts are aligned left (default), center (-m/--align-center)
or right (-r/--align-right). Users can specify columns with column names,
field indexes or ranges.
Examples:
-m A,B # column A and B
-m 1,2 # 1st and 2nd column
-m -1 # the last column (it's not unselecting in other commands)
-m 1,3-5 # 1st, from 3rd to 5th column
-m 1- # 1st and later columns (all columns)
-m -3- # the last 3 columns
-m -3--2 # the 2nd and 3rd to last columns
-m 1- -r -1 # all columns are center-aligned, except the last column
# which is right-aligned. -r overides -m.
2. Remaining rows are read and immediately outputted, one by one, till the end.
Styles:
Some preset styles are provided (-S/--style).
default:
id size
-- ----
1 Huge
2 Tiny
plain:
id size
1 Huge
2 Tiny
simple:
-----------
id size
-----------
1 Huge
2 Tiny
-----------
3line:
━━━━━━━━━━━
id size
-----------
1 Huge
2 Tiny
━━━━━━━━━━━
grid:
+----+------+
| id | size |
+====+======+
| 1 | Huge |
+----+------+
| 2 | Tiny |
+----+------+
light:
┌----┬------┐
| id | size |
├====┼======┤
| 1 | Huge |
├----┼------┤
| 2 | Tiny |
└----┴------┘
bold:
┏━━━━┳━━━━━━┓
┃ id ┃ size ┃
┣━━━━╋━━━━━━┫
┃ 1 ┃ Huge ┃
┣━━━━╋━━━━━━┫
┃ 2 ┃ Tiny ┃
┗━━━━┻━━━━━━┛
double:
╔════╦══════╗
║ id ║ size ║
╠════╬══════╣
║ 1 ║ Huge ║
╠════╬══════╣
║ 2 ║ Tiny ║
╚════╩══════╝
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)
if len(files) > 1 {
checkError(fmt.Errorf("no more than one file should be given"))
}
runtime.GOMAXPROCS(config.NumCPUs)
alignRights := getFlagStringSlice(cmd, "align-right")
alignCenters := getFlagStringSlice(cmd, "align-center")
separator := getFlagString(cmd, "separator")
minWidth := getFlagNonNegativeInt(cmd, "min-width")
maxWidth := getFlagNonNegativeInt(cmd, "max-width")
bufRows := getFlagNonNegativeInt(cmd, "buf-rows")
style := getFlagString(cmd, "style")
clip := getFlagBool(cmd, "clip")
clipMark := getFlagString(cmd, "clip-mark")
wrapDelimiter := getFlagString(cmd, "wrap-delimiter")
if len(wrapDelimiter) != 1 {
checkError(fmt.Errorf("the value of flag -x/--wrap-delimiter should be a single character: %s", wrapDelimiter))
}
outfh, err := xopen.Wopen(config.OutFile)
checkError(err)
defer outfh.Close()
file := files[0]
csvReader, err := newCSVReaderByConfig(config, file)
if err != nil {
if err == xopen.ErrNoContent {
if config.Verbose {
log.Warningf("csvtk pretty: skipping empty input file: %s", file)
}
return
}
checkError(err)
}
csvReader.Read(ReadOption{
FieldStr: "1-",
ShowRowNumber: config.ShowRowNumber,
})
styles := map[string]*stable.TableStyle{
"default": &stable.TableStyle{
Name: "default",
LineBelowHeader: stable.LineStyle{"", "-", separator, ""},
HeaderRow: stable.RowStyle{"", separator, ""},
DataRow: stable.RowStyle{"", separator, ""},
Padding: "",
},
"plain": stable.StylePlain,
"simple": stable.StyleSimple,
"3line": stable.StyleThreeLine,
"grid": stable.StyleGrid,
"light": stable.StyleLight,
"bold": stable.StyleBold,
"double": stable.StyleDouble,
}
if style == "" {
style = "default"
}
tbl := stable.New()
tbl.WrapDelimiter(rune(wrapDelimiter[0]))
if _style, ok := styles[strings.ToLower(style)]; ok {
tbl.Style(_style)
} else {
checkError(fmt.Errorf("style not available: %s. available vaules: default, plain, simple, 3line, grid, light, bold, double", style))
}
if minWidth > 0 {
tbl.MinWidth(minWidth)
}
if maxWidth > 0 {
tbl.MaxWidth(maxWidth)
}
if clip {
tbl.ClipCell(clipMark)
}
_bufRow := uint(bufRows)
if bufRows == 0 {
_bufRow = math.MaxUint
}
tbl.Writer(outfh, _bufRow)
checkFirstLine := true
var hasHeaderRow bool
var header []stable.Column
var negativeField = regexp.MustCompile(`^(-\d+)$`)
for record := range csvReader.Ch {
if record.Err != nil {
checkError(record.Err)
}
if checkFirstLine {
checkFirstLine = false
ncols := len(record.All)
if config.ShowRowNumber {
ncols++
}
header = make([]stable.Column, ncols)
// the fields is 1-based
colnames2fileds := make(map[string][]int, ncols)
var i int
var col string
var ok bool
if !config.NoHeaderRow || record.IsHeaderRow {
hasHeaderRow = true
for i, col = range record.All {
if config.ShowRowNumber {
i++
}
if _, ok = colnames2fileds[col]; !ok {
colnames2fileds[col] = []int{i + 1}
} else {
colnames2fileds[col] = append(colnames2fileds[col], i+1)
}
}
for i, col = range record.Selected {
header[i].Header = col
}
}
for i = range record.All {
col = strconv.Itoa(i + 1)
if _, ok = colnames2fileds[col]; !ok {
colnames2fileds[col] = []int{i + 1}
} else {
colnames2fileds[col] = append(colnames2fileds[col], i+1)
}
}
var _range []int
for _, col = range alignCenters {
if negativeField.MatchString(col) { // negatvie field
found := negativeField.FindAllStringSubmatch(col, -1)
f, _ := strconv.Atoi(found[0][1])
_range = []int{len(record.All) + 1 + f}
} else if reIntegerRange.MatchString(col) { // field range
_range = fieldRange(len(record.All), col)
} else { // colname
_range = colnames2fileds[col]
}
for _, i = range _range {
if config.ShowRowNumber {
i++
}
header[i-1].Align = stable.AlignCenter
}
}
for _, col = range alignRights {
if negativeField.MatchString(col) { // negatvie field
found := negativeField.FindAllStringSubmatch(col, -1)
f, _ := strconv.Atoi(found[0][1])
_range = []int{len(record.All) + 1 + f}
} else if reIntegerRange.MatchString(col) { // field range
_range = fieldRange(len(record.All), col)
} else { // colname
_range = colnames2fileds[col]
}
for _, i = range _range {
if config.ShowRowNumber {
i++
}
header[i-1].Align = stable.AlignRight
}
}
tbl.HeaderWithFormat(header)
if !hasHeaderRow {
tbl.AddRowStringSlice(record.Selected)
}
continue
}
tbl.AddRowStringSlice(record.Selected)
}
tbl.Flush()
readerReport(&config, csvReader, file)
},
}
func init() {
RootCmd.AddCommand(prettyCmd)
prettyCmd.Flags().StringP("separator", "s", " ", "fields/columns separator")
prettyCmd.Flags().StringSliceP("align-right", "r", []string{}, `align right for selected columns (field index/range or column name, type "csvtk pretty -h" for examples)`)
prettyCmd.Flags().StringSliceP("align-center", "m", []string{}, `align right for selected columns (field index/range or column name, type "csvtk pretty -h" for examples)`)
prettyCmd.Flags().IntP("min-width", "w", 0, "min width")
prettyCmd.Flags().IntP("max-width", "W", 0, "max width")
prettyCmd.Flags().StringP("wrap-delimiter", "x", " ", "delimiter for wrapping cells")
prettyCmd.Flags().IntP("buf-rows", "n", 1024, "the number of rows to determine the min and max widths (0 for all rows)")
prettyCmd.Flags().StringP("style", "S", "", "output syle. available vaules: default, plain, simple, 3line, grid, light, bold, double. check https://github.com/shenwei356/stable")
prettyCmd.Flags().BoolP("clip", "", false, "clip longer cell instead of wrapping")
prettyCmd.Flags().StringP("clip-mark", "", "...", "clip mark")
}