Skip to content

Instantly share code, notes, and snippets.

@BiggerNoise
Created April 26, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BiggerNoise/d499cac984785807d0b5f833291a86b1 to your computer and use it in GitHub Desktop.
Save BiggerNoise/d499cac984785807d0b5f833291a86b1 to your computer and use it in GitHub Desktop.
This will allow DataGrip results to be copied to the clipboard in a slack friendly markdown format.
SLACK_CODE = "```"
SEP = "|"
NEWLINE = System.getProperty("line.separator")
def scan(ROWS, COLUMNS) {
result = []
result.add(COLUMNS.collect { col -> col.name() })
ROWS.each { row -> result.add(COLUMNS.collect { col -> row.value(col).toString() }) }
return result
}
def size(extract) {
return extract.collect { row -> row*.length() }.inject([]) { result, r ->
r.eachWithIndex { v, idx ->
if (result[idx] == null || result[idx] < v) {
result[idx] = v
}
}
return result
}
}
def writeRow(row) {
OUT.append(SEP)
row.eachWithIndex {v,idx ->
OUT.append(v.padLeft(sizes[idx] +1).padRight(sizes[idx] + 2))
OUT.append(SEP)
}
OUT.append(NEWLINE)
}
def writeHSeparator(sizes) {
OUT.append(SEP)
sizes.eachWithIndex {v,idx ->
OUT.append(''.padLeft(sizes[idx] +2, '-'))
OUT.append(SEP)
}
OUT.append(NEWLINE)
}
def record(extract) {
OUT.append(SLACK_CODE)
OUT.append(NEWLINE)
sizes = size(extract)
extract.eachWithIndex { r, idx ->
if (idx == 1) {
writeHSeparator(sizes)
}
writeRow(r)
}
OUT.append(SLACK_CODE)
OUT.append(NEWLINE)
}
extract = scan(ROWS, COLUMNS)
record(extract)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment