-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTable-Text-Groovy.txt.groovy
92 lines (79 loc) · 2.63 KB
/
Table-Text-Groovy.txt.groovy
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
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
NEWLINE = System.getProperty("line.separator")
def determineMaxWidths = { columns, rows ->
def rowValuesMaxWidth = { column ->
rows.collect { row ->
def value = FORMATTER.format(row, column)
value ? value.length() : 0
}.max()
}
// { columnNumber -> max size }
columns.collectEntries { column ->
[column.columnNumber(), Math.max(column.name().length(), rowValuesMaxWidth(column))]
}
}
def printResult = { columnWidthMap, columns, rows ->
def columnWidth = { column ->
columnWidthMap[column.columnNumber()]
}
// prints +----------------+-----+----------+
def printDivider = {
columns.each { column ->
OUT.append("+")
OUT.append("-" * (columnWidth(column) + 2))
}
OUT.append("+")
OUT.append(NEWLINE)
}
// prints | Header1 | Header2 | ... |
def printHeaderRow = {
columns.each { column ->
OUT.append("| ")
OUT.append(column.name())
OUT.append(" " * (columnWidth(column) - column.name().length() + 1))
}
OUT.append("|")
OUT.append(NEWLINE)
}
// prints | Value1 | Value 2 | ... |
def printRow = { row ->
columns.each { column ->
def value = FORMATTER.format(row, column)
OUT.append("| ")
OUT.append(value)
OUT.append(" " * (columnWidth(column) - value.length() + 1))
}
OUT.append("|")
OUT.append(NEWLINE)
}
printDivider()
printHeaderRow()
printDivider()
rows.each { printRow(it) }
printDivider()
OUT.append(rows.size().toString()).append(" rows in set.")
}
if (TRANSPOSED) {
throw new RuntimeException("Not implemented!")
} else {
// we need to iterate twice (which we can't do with a single iterator) so store in a temporary
// collection. That does mean that this export is (potentially) heavy on resource, so don't
// do this with large result sets
def columns = COLUMNS.collect { it }
def rows = ROWS.collect { it }
// map from column number to max witdth of the values in that column
def columnWidthMap = determineMaxWidths(columns, rows)
printResult(columnWidthMap, columns, rows)
}