|
import com.intellij.openapi.util.text.StringUtil |
|
|
|
NEWLINE = System.getProperty('line.separator') |
|
|
|
static def splitByLines(values, size) { |
|
def splitValues = [] |
|
def maxLines = 0 |
|
for (int i = 0; i < size; i++) { |
|
def splitValue = StringUtil.splitByLines(values(i)) |
|
splitValues.add(splitValue) |
|
maxLines = Math.max(maxLines, splitValue.size()) |
|
} |
|
|
|
def byLines = new ArrayList<>(maxLines) |
|
for (int i = 0; i < maxLines; i++) { |
|
def lineValues = [] |
|
byLines.add(lineValues) |
|
for (int j = 0; j < splitValues.size(); j++) { |
|
def splitValue = splitValues[j] |
|
lineValues.add(splitValue.size() <= i ? null : splitValue[i]) |
|
} |
|
} |
|
return byLines |
|
} |
|
|
|
def printRow(values, size) { |
|
def byLines = splitByLines(values, size) |
|
byLines.each { line -> |
|
def lineSize = line.size() |
|
if (lineSize > 0) OUT.append('') |
|
for (int i = 0; i < lineSize; i++) { |
|
def value = line[i] == null ? '' : line.get(i) |
|
OUT.append(value) |
|
if (i < lineSize - 1) OUT.append(' ') |
|
} |
|
OUT.append(NEWLINE) |
|
} |
|
} |
|
|
|
def printRows() { |
|
def colNames = COLUMNS*.name() |
|
def rows = [] |
|
|
|
def widths = new int[COLUMNS.size()] |
|
ROWS.each { row -> |
|
def rowValues = COLUMNS.withIndex().collect { col, idx -> |
|
def value = FORMATTER.format(row, col) |
|
widths[idx] = Math.max(widths[idx], value.length()) |
|
value |
|
} |
|
rows.add(rowValues) |
|
} |
|
width = { widths[it] } |
|
|
|
printRow({ '' }, COLUMNS.size()) |
|
printRow({ colNames[it] }, COLUMNS.size()) |
|
printRow({ '–' }, 5) |
|
|
|
rows.each { row -> |
|
printRow({ row[it] }, row.size()) |
|
} |
|
} |
|
|
|
def printRowsTransposed() { |
|
def valuesByRow = COLUMNS.collect { col -> new ArrayList<String>([col.name()]) } |
|
ROWS.each { row -> |
|
COLUMNS.eachWithIndex { col, i -> |
|
def formattedValue = FORMATTER.format(row, col) |
|
valuesByRow[i].add(formattedValue) |
|
} |
|
} |
|
valuesByRow.each { row -> |
|
printRow({ row[it] }, row.size()) |
|
} |
|
} |
|
|
|
if (TRANSPOSED) { |
|
printRowsTransposed() |
|
} else { |
|
printRows() |
|
} |