|
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; |
|
} |
|
} |
|
|
|
// credit: https://github.com/kemitchell/markdown-escape.js |
|
var replacements = [ |
|
[ /\*/g, '\\*' ], |
|
[ /#/g, '\\#' ], |
|
[ /\//g, '\\/' ], |
|
[ /\(/g, '\\(' ], |
|
[ /\)/g, '\\)' ], |
|
[ /\[/g, '\\[' ], |
|
[ /\]/g, '\\]' ], |
|
[ /\</g, '<' ], |
|
[ /\>/g, '>' ], |
|
[ /_/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); |
|
} |
|
|
|
output("Queried: " + (new Date()).toLocaleDateString(), NEWLINE, NEWLINE); |
|
|
|
if (TRANSPOSED) { |
|
var values = mapEach(COLUMNS, function(col) { return [col.name()]; }); |
|
eachWithIdx(ROWS, function (row) { |
|
eachWithIdx(COLUMNS, function (col, i) { |
|
values[i].push(FORMATTER.format(row, col)); |
|
}); |
|
}); |
|
eachWithIdx(COLUMNS, function (_, i) { outputRow(values[i]); }); |
|
} |
|
else { |
|
outputRow(mapEach(COLUMNS, function (col) { return col.name(); })); // header names |
|
outputRow(mapEach(COLUMNS, function (col) { return '-'.repeat(Math.max(4, col.name().length)); })); // header separator |
|
|
|
eachWithIdx(ROWS, function (row) { |
|
outputRow(mapEach(COLUMNS, function (col) { return FORMATTER.format(row, col); })) |
|
}); |
|
} |
|
|
|
output(NEWLINE); |