|
@@ -1,41 +1,148 @@
|
|
|
const stripAnsi = require('strip-ansi')
|
|
|
const log = require('../util/log')
|
|
|
|
|
|
-function normalizeCols (rows, max) {
|
|
|
+function keyValueArray (columns, keys, obj) {
|
|
|
+ return keys.map(el => columns[el](obj))
|
|
|
+}
|
|
|
+
|
|
|
+function findMaxLengths (rows) {
|
|
|
+ var widths = []
|
|
|
+
|
|
|
+ for (let i = 0; i < rows.length; i++) {
|
|
|
+ for (let j = 0; j < rows[i].length; j++) {
|
|
|
+ let item = stripAnsi(rows[i][j]) + ''
|
|
|
+ if (!widths[j] || widths[j] < item.length) {
|
|
|
+ widths[j] = item.length
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return widths
|
|
|
+}
|
|
|
+
|
|
|
+function createTable (rows, fitWidth) {
|
|
|
function padEnd (string, maxLength, fillString) {
|
|
|
- while ((stripAnsi(string) + '').length < maxLength) {
|
|
|
+
|
|
|
+ if (maxLength === Infinity) {
|
|
|
+ return string
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ string = string + ''
|
|
|
+
|
|
|
+
|
|
|
+ while (stripAnsi(string).length < maxLength) {
|
|
|
string = string + fillString
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ if (string.length > maxLength) {
|
|
|
+ return string.substr(0, maxLength)
|
|
|
+ }
|
|
|
+
|
|
|
return string
|
|
|
}
|
|
|
|
|
|
- var widths = []
|
|
|
- max = max || rows[0].length
|
|
|
+
|
|
|
+ if (fitWidth > 0) {
|
|
|
+ var targetWidth = Math.floor((fitWidth / rows[0].length) - 3)
|
|
|
+ } else {
|
|
|
+ targetWidth = Infinity
|
|
|
+ }
|
|
|
|
|
|
- for (let i = 0; i < rows.length; i++) {
|
|
|
- for (let j = 0; j < rows[i].length && j < max; j++) {
|
|
|
- if (!widths[j] || widths[j] < (stripAnsi(rows[i][j]) + '').length) {
|
|
|
- widths[j] = (stripAnsi(rows[i][j] + '') + '').length
|
|
|
+ let maxWidths = findMaxLengths(rows)
|
|
|
+ let widths = []
|
|
|
+
|
|
|
+
|
|
|
+ var budget = 0
|
|
|
+
|
|
|
+
|
|
|
+ for (let i = 0; i < maxWidths.length; i++) {
|
|
|
+ if (maxWidths[i] < targetWidth) {
|
|
|
+ budget += (targetWidth - maxWidths[i])
|
|
|
+ widths[i] = maxWidths[i]
|
|
|
+ } else {
|
|
|
+ widths[i] = targetWidth
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (targetWidth < Infinity) {
|
|
|
+
|
|
|
+ while (budget > 0) {
|
|
|
+ var okCount = 0
|
|
|
+
|
|
|
+ for (let i = 0; i < widths.length; i++) {
|
|
|
+ let diff = maxWidths[i] - widths[i]
|
|
|
+
|
|
|
+
|
|
|
+ if (diff > 0 && budget > 0) {
|
|
|
+
|
|
|
+ widths[i] += 1
|
|
|
+ budget -= 1
|
|
|
+ } else {
|
|
|
+ okCount += 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (okCount === widths.length) {
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ var outputRows = []
|
|
|
+
|
|
|
+
|
|
|
+ var cursors = []
|
|
|
+
|
|
|
+
|
|
|
+ let flag = false
|
|
|
+
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
- for (let j = 0; j < rows[i].length && j < max; j++) {
|
|
|
- if (rows[i][j] === '-') {
|
|
|
- rows[i][j] = padEnd(rows[i][j], widths[j], '-')
|
|
|
+ var line = []
|
|
|
+
|
|
|
+ for (let j = 0; j < rows[i].length; j++) {
|
|
|
+ cursors[j] = cursors[j] || 0
|
|
|
+
|
|
|
+
|
|
|
+ let rawItem = rows[i][j] + ''
|
|
|
+
|
|
|
+
|
|
|
+ let inputItem = rawItem.substr(cursors[j], widths[j])
|
|
|
+
|
|
|
+ if (inputItem === '-') {
|
|
|
+ line.push(padEnd(inputItem, widths[j], '-'))
|
|
|
} else {
|
|
|
- rows[i][j] = padEnd(rows[i][j], widths[j], ' ')
|
|
|
+
|
|
|
+ let item = padEnd(inputItem, widths[j], ' ')
|
|
|
+ line.push(item)
|
|
|
+
|
|
|
+
|
|
|
+ if (cursors[j] + inputItem.length < rawItem.length && inputItem !== '') {
|
|
|
+ flag = true
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ cursors[j] += widths[j]
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return rows
|
|
|
-}
|
|
|
+
|
|
|
+ outputRows.push(line)
|
|
|
|
|
|
-function keyValueArray (columns, keys, obj) {
|
|
|
- return keys.map(el => columns[el](obj))
|
|
|
+
|
|
|
+ if (flag) {
|
|
|
+ i -= 1
|
|
|
+ flag = false
|
|
|
+ } else {
|
|
|
+
|
|
|
+ cursors = []
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return outputRows
|
|
|
}
|
|
|
|
|
|
module.exports.format = function (data, options) {
|
|
@@ -58,8 +165,14 @@ module.exports.format = function (data, options) {
|
|
|
...data.map(data => keyValueArray(options.columns, keys, data))
|
|
|
]
|
|
|
|
|
|
+ if (options.program && options.program.output !== undefined) {
|
|
|
+ var targetWidth = 120
|
|
|
+ } else {
|
|
|
+ targetWidth = process.stdout.columns
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- items = normalizeCols(items).map(el => {
|
|
|
+ items = createTable(items, targetWidth).map(el => {
|
|
|
return el.join(' | ').replace(/\n/g, '')
|
|
|
}).join('\n')
|
|
|
|