Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extract output from DataGrip into evenly spaced fixed width columns so it's readable when you paste it into Slack
if (!String.prototype.repeat) {
// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
// Could we try:
// return Array(count + 1).join(this);
return rpt;
}
}
if (!Array.prototype.transpose) {
Array.prototype.transpose = function () {
var a = this;
var w = a.length ? a.length : 0;
var h = a[0] instanceof Array ? a[0].length : 0;
if (h === 0 || w === 0) {
return [];
}
var transposed = [];
for (var i = 0; i < h; i++) {
transposed[i] = [];
for (var j = 0; j < w; j++) {
transposed[i][j] = a[j][i];
}
}
return transposed;
}
}
// credit: https://github.com/kemitchell/markdown-escape.js
var replacements = [
[ /\*/g, '\\*' ],
[ /#/g, '\\#' ],
[ /\//g, '\\/' ],
[ /\(/g, '\\(' ],
[ /\)/g, '\\)' ],
[ /\[/g, '\\[' ],
[ /\]/g, '\\]' ],
[ /\</g, '&lt;' ],
[ /\>/g, '&gt;' ],
[ /_/g, '\\_' ] ];
function escapeMarkdown(string) {
return replacements.reduce(
function(string, replacement) {
return string.replace(replacement[0], replacement[1])
},
string)
}
function eachWithIdx(iterable, f) {
var i = iterable.iterator();
var idx = 0;
while (i.hasNext()) {
f(i.next(), idx++);
}
}
function mapEach(iterable, f) {
var vs = [];
eachWithIdx(iterable, function (i) {
vs.push(f(i));
});
return vs;
}
function escape(str) {
str = str.replaceAll("\t|\b|\\f", "");
str = com.intellij.openapi.util.text.StringUtil.escapeXml(str);
str = str.replaceAll("\\r|\\n|\\r\\n", "<br/>");
return escapeMarkdown(str);
}
var NEWLINE = "\n";
function output() {
for (var i = 0; i < arguments.length; i++) {
OUT.append(arguments[i]);
}
}
function outputRow(items) {
for (var i = 0; i < items.length; i++) {
output("|");
output(escape(items[i]));
if(i == items.length - 1) {
output("|"); // trailing pipe
}
}
output(NEWLINE);
}
function getMaxLength(someArray) {
return someArray
.map(function(x) { return x.length; })
.reduce(function(x, y) { return Math.max(x, y); });
}
var columnNames = mapEach(COLUMNS, function (col) { return " " + col.name() + " "; });
var rows = [];
eachWithIdx(ROWS, function (row) {
var row = mapEach(COLUMNS, function (col) { return " " + FORMATTER.format(row, col) + " "; });
rows.push(row);
});
var columnsAndRows = [];
columnsAndRows.push(columnNames);
columnsAndRows = columnsAndRows.concat(rows);
var byColumnVirtually = TRANSPOSED ? columnsAndRows.transpose() : columnsAndRows;
var byColumnInActualOutput = TRANSPOSED ? columnsAndRows : columnsAndRows.transpose();
var columnMaxWidths = byColumnInActualOutput.map(function(x) { return getMaxLength(x); });
byColumnVirtually.forEach(function(row, i) {
row.forEach(function(value, j) {
if (value.length < columnMaxWidths[j]) {
row[j] = row[j] + ' '.repeat(columnMaxWidths[j] - value.length);
}
});
});
if (TRANSPOSED) {
byColumnVirtually.forEach(function(row) {
outputRow(row);
});
} else {
outputRow(columnNames);
var separators = columnMaxWidths.map(function(x) { return '-'.repeat(x); });
outputRow(separators);
rows.forEach(function(row) {
outputRow(row);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment