From d133ebf32df1f9f294a86c98ff7f5dc67f342f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 19 Feb 2018 10:19:01 +0200 Subject: [PATCH] Remove ANSI color codes when --tsv is defined When TSV output is requested, the output should not contain the ANSI color codes. This appears to be a "feature" of the table generation library but it is quite simple to work around. --- maxctrl/lib/common.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/maxctrl/lib/common.js b/maxctrl/lib/common.js index e84169464..5f2ad151c 100644 --- a/maxctrl/lib/common.js +++ b/maxctrl/lib/common.js @@ -92,6 +92,15 @@ module.exports = function() { return table } + this.tableToString = function(table) { + str = table.toString() + if (this.argv.tsv) { + // Based on the regex found in: https://github.com/jonschlinkert/strip-color + str = str.replace( /\x1B\[[(?);]{0,2}(;?\d)*./g, '') + } + return str + } + // Get a resource as raw collection; a matrix of strings this.getRawCollection = function (host, resource, fields) { return getJson(host, resource) @@ -110,7 +119,7 @@ module.exports = function() { arr.forEach((row) => { table.push(row) }) - return table.toString() + return tableToString(table) } // Request a resource collection and format it as a table @@ -148,7 +157,7 @@ module.exports = function() { table.push(row) }) - return table.toString() + return tableToString(table) }) } @@ -174,7 +183,7 @@ module.exports = function() { table.push(o) }) - return table.toString() + return tableToString(table) }) }