-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb8126f
commit fb7ad53
Showing
9 changed files
with
172 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const Table = require('cli-table2'); | ||
|
||
const MIN_CELL_WIDTH = 15; | ||
|
||
class TablePrinter { | ||
constructor(params) { | ||
this.params = params; | ||
this._hasWritten = false; | ||
this.colWidths; | ||
} | ||
|
||
push(csv) { | ||
const lines = csv.split(this.params.eol); | ||
|
||
const chars = { | ||
'bottom': '', | ||
'bottom-mid': '', | ||
'bottom-left': '', | ||
'bottom-right': '' | ||
}; | ||
if (!this._hasWritten) { | ||
this.colWidths = this.getColumnWidths(lines[0]); | ||
if (this.params.header) { | ||
const head = lines.shift().split(this.params.delimiter); | ||
const table = new Table({ head, colWidths: this.colWidths, chars }); | ||
this.print(table, []); | ||
this._hasWritten = true; | ||
} | ||
} else { | ||
chars['top-left'] = '├'; | ||
chars['top-mid'] = '┼'; | ||
chars['top-right'] = '┤'; | ||
} | ||
|
||
if (!lines.length) return; | ||
|
||
const table = new Table({ colWidths: this.colWidths, chars }); | ||
this.print(table, lines); | ||
this._hasWritten = true; | ||
} | ||
|
||
end(csv) { | ||
const lines = csv.split(this.params.eol); | ||
const chars = { 'top-left': '├' , 'top-mid': '┼', 'top-right': '┤' }; | ||
const table = new Table({ colWidths: this.colWidths, chars }); | ||
this.print(table, lines); | ||
} | ||
|
||
printCSV(csv) { | ||
let lines = csv.split(this.params.eol); | ||
|
||
this.colWidths = this.getColumnWidths(lines[0]); | ||
const head = this.params.header | ||
? lines.shift().split(this.params.delimiter) | ||
: undefined; | ||
|
||
const table = new Table(head | ||
? { head, colWidths: this.colWidths } | ||
: { colWidths: this.colWidths }); | ||
|
||
this.print(table, lines); | ||
} | ||
|
||
getColumnWidths(line) { | ||
return line | ||
.split(this.params.delimiter) | ||
.map(elem => Math.max(elem.length * 2, MIN_CELL_WIDTH)); | ||
} | ||
|
||
print(table, lines) { | ||
lines.forEach(line => table.push(line.split(this.params.delimiter))); | ||
// eslint-disable-next-line no-console | ||
console.log(table.toString()); | ||
} | ||
} | ||
|
||
module.exports = TablePrinter; |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
┌────────────────────┬──────────────┬──────────────┐ | ||
│ "carModel" │ "price" │ "color" │ | ||
├────────────────────┼──────────────┼──────────────┤ | ||
│ "Audi" │ 0 │ "blue" │ | ||
├────────────────────┼──────────────┼──────────────┤ | ||
│ "BMW" │ 15000 │ "red" │ | ||
├────────────────────┼──────────────┼──────────────┤ | ||
│ "Mercedes" │ 20000 │ "yellow" │ | ||
├────────────────────┼──────────────┼──────────────┤ | ||
│ "Porsche" │ 30000 │ "green" │ | ||
└────────────────────┴──────────────┴──────────────┘ | ||
┌────────────────────┬───────────────┬───────────────┐ | ||
│ "carModel" │ "price" │ "color" │ | ||
├────────────────────┼───────────────┼───────────────┤ | ||
│ "Audi" │ 0 │ "blue" │ | ||
├────────────────────┼───────────────┼───────────────┤ | ||
│ "BMW" │ 15000 │ "red" │ | ||
├────────────────────┼───────────────┼───────────────┤ | ||
│ "Mercedes" │ 20000 │ "yellow" │ | ||
├────────────────────┼───────────────┼───────────────┤ | ||
│ "Porsche" │ 30000 │ "green" │ | ||
└────────────────────┴───────────────┴───────────────┘ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
┌───────────────┬───────────────┬───────────────┐ | ||
│ "Audi" │ 0 │ "blue" │ | ||
├───────────────┼───────────────┼───────────────┤ | ||
│ "BMW" │ 15000 │ "red" │ | ||
├───────────────┼───────────────┼───────────────┤ | ||
│ "Mercedes" │ 20000 │ "yellow" │ | ||
├───────────────┼───────────────┼───────────────┤ | ||
│ "Porsche" │ 30000 │ "green" │ | ||
└───────────────┴───────────────┴───────────────┘ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters