Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataGrip extractor for txt file with fixed length columns
SEPARATOR = "|"
SPACE = " "
CORNER = "+"
LINE = "-"
NEWLINE = System.getProperty("line.separator")
def record(values, valueToString) {
values.collect { value ->
valueToString(value)
}
}
def outRecord(columnLengths, row, separator=SEPARATOR, padding=SPACE) {
OUT.append(separator)
[columnLengths, row].transpose().each { size, value ->
OUT.append(padding + value.padRight(size, padding) + padding + separator)
};
OUT.append(NEWLINE)
}
COLUMN_NAMES = COLUMNS.collect { col -> col.name() }
def formattedRows = ROWS.collect { row -> record(COLUMNS, { FORMATTER.format(row, it) }) }
def columnLengths = formattedRows.inject(COLUMN_NAMES.collect { col -> col.size() }, { columnLengths, row ->
[columnLengths, row.collect { it.length() }].transpose().collect { collectedSize, size -> Math.max(collectedSize, size) }
})
// OUTPUT
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE)
outRecord(columnLengths, COLUMN_NAMES)
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE)
formattedRows.each { row -> outRecord(columnLengths, row) }
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE)
@mvitaly

This comment has been minimized.

Show comment
Hide comment
@mvitaly

mvitaly Jul 29, 2016

Will not work well for huge output as it's not streaming, it needs to go over all the results first to determine the max size of each column

Owner

mvitaly commented Jul 29, 2016

Will not work well for huge output as it's not streaming, it needs to go over all the results first to determine the max size of each column

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment