From a20ddfc63a32d8dad06f40925465313ce65dd51a Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Mon, 29 Apr 2019 18:16:26 +0200 Subject: [PATCH] fix: pretty print when there is no rows (#390) --- bin/utils/TablePrinter.js | 16 ++++++---------- test/CLI.js | 12 +++++++++++- test/fixtures/csv/prettyprintWithoutRows.txt | 3 +++ 3 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/csv/prettyprintWithoutRows.txt diff --git a/bin/utils/TablePrinter.js b/bin/utils/TablePrinter.js index 1e732e81..fb0f1dfe 100644 --- a/bin/utils/TablePrinter.js +++ b/bin/utils/TablePrinter.js @@ -13,9 +13,7 @@ class TablePrinter { if (!lines.length) return; - if (!this._hasWritten) { - this.setColumnWidths(lines[0]); - } + if (!this._hasWritten) this.setColumnWidths(lines[0]); const top = this._hasWritten ? this.middleLine : this.topLine; this.print(top, lines); @@ -23,16 +21,14 @@ class TablePrinter { } end(csv) { - const lines = csv.split(this.opts.eol); - this.print(this.middleLine, lines, this.bottomLine); + let lines = csv.split(this.opts.eol); + if (!this._hasWritten) this.setColumnWidths(lines[0]); + const top = this._hasWritten ? this.middleLine : this.topLine; + this.print(top, lines, this.bottomLine); } printCSV(csv) { - let lines = csv.split(this.opts.eol); - - this.setColumnWidths(lines[0]); - - this.print(this.topLine, lines, this.bottomLine); + this.end(csv); } setColumnWidths(line) { diff --git a/test/CLI.js b/test/CLI.js index bc49ac41..34ff7fe6 100644 --- a/test/CLI.js +++ b/test/CLI.js @@ -826,5 +826,15 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { t.end(); }); }); -}; + testRunner.add('should print pretty table without rows', (t) => { + const opts = ' --fields fieldA,fieldB,fieldC --pretty'; + + child_process.exec(cli + '-i ' + getFixturePath('/json/default.json') + opts, (err, stdout, stderr) => { + t.notOk(stderr); + const csv = stdout; + t.equal(csv, csvFixtures.prettyprintWithoutRows); + t.end(); + }); + }); +}; diff --git a/test/fixtures/csv/prettyprintWithoutRows.txt b/test/fixtures/csv/prettyprintWithoutRows.txt new file mode 100644 index 00000000..f6a17ae8 --- /dev/null +++ b/test/fixtures/csv/prettyprintWithoutRows.txt @@ -0,0 +1,3 @@ +┌────────────────┬────────────────┬────────────────┐ +│ "fieldA" │ "fieldB" │ "fieldC" │ +└────────────────┴────────────────┴────────────────┘