2017-05-27 17:36:13 +02:00
|
|
|
var stringWidth = require('string-width')
|
|
|
|
var stripAnsi = require('strip-ansi')
|
|
|
|
var wrap = require('wrap-ansi')
|
|
|
|
var align = {
|
|
|
|
right: alignRight,
|
|
|
|
center: alignCenter
|
|
|
|
}
|
|
|
|
var top = 0
|
|
|
|
var right = 1
|
|
|
|
var bottom = 2
|
|
|
|
var left = 3
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function UI (opts) {
|
|
|
|
this.width = opts.width
|
|
|
|
this.wrap = opts.wrap
|
|
|
|
this.rows = []
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype.span = function () {
|
|
|
|
var cols = this.div.apply(this, arguments)
|
|
|
|
cols.span = true
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype.div = function () {
|
|
|
|
if (arguments.length === 0) this.div('')
|
|
|
|
if (this.wrap && this._shouldApplyLayoutDSL.apply(this, arguments)) {
|
|
|
|
return this._applyLayoutDSL(arguments[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
var cols = []
|
|
|
|
|
|
|
|
for (var i = 0, arg; (arg = arguments[i]) !== undefined; i++) {
|
|
|
|
if (typeof arg === 'string') cols.push(this._colFromString(arg))
|
|
|
|
else cols.push(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rows.push(cols)
|
|
|
|
return cols
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._shouldApplyLayoutDSL = function () {
|
|
|
|
return arguments.length === 1 && typeof arguments[0] === 'string' &&
|
|
|
|
/[\t\n]/.test(arguments[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._applyLayoutDSL = function (str) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var _this = this
|
|
|
|
var rows = str.split('\n')
|
|
|
|
var leftColumnWidth = 0
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// simple heuristic for layout, make sure the
|
|
|
|
// second column lines up along the left-hand.
|
|
|
|
// don't allow the first column to take up more
|
|
|
|
// than 50% of the screen.
|
|
|
|
rows.forEach(function (row) {
|
|
|
|
var columns = row.split('\t')
|
2017-05-27 17:36:13 +02:00
|
|
|
if (columns.length > 1 && stringWidth(columns[0]) > leftColumnWidth) {
|
2017-05-03 15:35:00 +02:00
|
|
|
leftColumnWidth = Math.min(
|
|
|
|
Math.floor(_this.width * 0.5),
|
2017-05-27 17:36:13 +02:00
|
|
|
stringWidth(columns[0])
|
2017-05-03 15:35:00 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// generate a table:
|
|
|
|
// replacing ' ' with padding calculations.
|
|
|
|
// using the algorithmically generated width.
|
|
|
|
rows.forEach(function (row) {
|
|
|
|
var columns = row.split('\t')
|
|
|
|
_this.div.apply(_this, columns.map(function (r, i) {
|
|
|
|
return {
|
|
|
|
text: r.trim(),
|
2017-05-27 17:36:13 +02:00
|
|
|
padding: _this._measurePadding(r),
|
2017-05-03 15:35:00 +02:00
|
|
|
width: (i === 0 && columns.length > 1) ? leftColumnWidth : undefined
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
return this.rows[this.rows.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._colFromString = function (str) {
|
|
|
|
return {
|
2017-05-27 17:36:13 +02:00
|
|
|
text: str,
|
|
|
|
padding: this._measurePadding(str)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
UI.prototype._measurePadding = function (str) {
|
|
|
|
// measure padding without ansi escape codes
|
|
|
|
var noAnsi = stripAnsi(str)
|
|
|
|
return [0, noAnsi.match(/\s*$/)[0].length, 0, noAnsi.match(/^\s*/)[0].length]
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
UI.prototype.toString = function () {
|
2017-05-27 17:36:13 +02:00
|
|
|
var _this = this
|
|
|
|
var lines = []
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
_this.rows.forEach(function (row, i) {
|
|
|
|
_this.rowToString(row, lines)
|
|
|
|
})
|
|
|
|
|
|
|
|
// don't display any lines with the
|
|
|
|
// hidden flag set.
|
|
|
|
lines = lines.filter(function (line) {
|
|
|
|
return !line.hidden
|
|
|
|
})
|
|
|
|
|
|
|
|
return lines.map(function (line) {
|
|
|
|
return line.text
|
|
|
|
}).join('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype.rowToString = function (row, lines) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var _this = this
|
|
|
|
var padding
|
|
|
|
var rrows = this._rasterize(row)
|
|
|
|
var str = ''
|
|
|
|
var ts
|
|
|
|
var width
|
|
|
|
var wrapWidth
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
rrows.forEach(function (rrow, r) {
|
|
|
|
str = ''
|
|
|
|
rrow.forEach(function (col, c) {
|
|
|
|
ts = '' // temporary string used during alignment/padding.
|
|
|
|
width = row[c].width // the width with padding.
|
|
|
|
wrapWidth = _this._negatePadding(row[c]) // the width without padding.
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
ts += col
|
|
|
|
|
|
|
|
for (var i = 0; i < wrapWidth - stringWidth(col); i++) {
|
|
|
|
ts += ' '
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// align the string within its column.
|
|
|
|
if (row[c].align && row[c].align !== 'left' && _this.wrap) {
|
2017-05-27 17:36:13 +02:00
|
|
|
ts = align[row[c].align](ts, wrapWidth)
|
|
|
|
if (stringWidth(ts) < wrapWidth) ts += new Array(width - stringWidth(ts)).join(' ')
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
// apply border and padding to string.
|
|
|
|
padding = row[c].padding || [0, 0, 0, 0]
|
|
|
|
if (padding[left]) str += new Array(padding[left] + 1).join(' ')
|
|
|
|
str += addBorder(row[c], ts, '| ')
|
2017-05-03 15:35:00 +02:00
|
|
|
str += ts
|
2017-05-27 17:36:13 +02:00
|
|
|
str += addBorder(row[c], ts, ' |')
|
|
|
|
if (padding[right]) str += new Array(padding[right] + 1).join(' ')
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// if prior row is span, try to render the
|
|
|
|
// current row on the prior line.
|
|
|
|
if (r === 0 && lines.length > 0) {
|
2017-05-27 17:36:13 +02:00
|
|
|
str = _this._renderInline(str, lines[lines.length - 1])
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// remove trailing whitespace.
|
|
|
|
lines.push({
|
|
|
|
text: str.replace(/ +$/, ''),
|
|
|
|
span: row.span
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
function addBorder (col, ts, style) {
|
|
|
|
if (col.border) {
|
|
|
|
if (/[.']-+[.']/.test(ts)) return ''
|
|
|
|
else if (ts.trim().length) return style
|
|
|
|
else return ' '
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
// if the full 'source' can render in
|
|
|
|
// the target line, do so.
|
2017-05-27 17:36:13 +02:00
|
|
|
UI.prototype._renderInline = function (source, previousLine) {
|
|
|
|
var leadingWhitespace = source.match(/^ */)[0].length
|
|
|
|
var target = previousLine.text
|
|
|
|
var targetTextWidth = stringWidth(target.trimRight())
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
if (!previousLine.span) return source
|
|
|
|
|
|
|
|
// if we're not applying wrapping logic,
|
|
|
|
// just always append to the span.
|
|
|
|
if (!this.wrap) {
|
|
|
|
previousLine.hidden = true
|
|
|
|
return target + source
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
if (leadingWhitespace < targetTextWidth) return source
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
previousLine.hidden = true
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
return target.trimRight() + new Array(leadingWhitespace - targetTextWidth + 1).join(' ') + source.trimLeft()
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._rasterize = function (row) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var _this = this
|
|
|
|
var i
|
|
|
|
var rrow
|
|
|
|
var rrows = []
|
|
|
|
var widths = this._columnWidths(row)
|
|
|
|
var wrapped
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// word wrap all columns, and create
|
|
|
|
// a data-structure that is easy to rasterize.
|
|
|
|
row.forEach(function (col, c) {
|
|
|
|
// leave room for left and right padding.
|
|
|
|
col.width = widths[c]
|
2017-05-27 17:36:13 +02:00
|
|
|
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), {hard: true}).split('\n')
|
2017-05-03 15:35:00 +02:00
|
|
|
else wrapped = col.text.split('\n')
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
if (col.border) {
|
|
|
|
wrapped.unshift('.' + new Array(_this._negatePadding(col) + 3).join('-') + '.')
|
|
|
|
wrapped.push("'" + new Array(_this._negatePadding(col) + 3).join('-') + "'")
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
// add top and bottom padding.
|
|
|
|
if (col.padding) {
|
|
|
|
for (i = 0; i < (col.padding[top] || 0); i++) wrapped.unshift('')
|
|
|
|
for (i = 0; i < (col.padding[bottom] || 0); i++) wrapped.push('')
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapped.forEach(function (str, r) {
|
|
|
|
if (!rrows[r]) rrows.push([])
|
|
|
|
|
|
|
|
rrow = rrows[r]
|
|
|
|
|
|
|
|
for (var i = 0; i < c; i++) {
|
|
|
|
if (rrow[i] === undefined) rrow.push('')
|
|
|
|
}
|
|
|
|
rrow.push(str)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return rrows
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._negatePadding = function (col) {
|
|
|
|
var wrapWidth = col.width
|
|
|
|
if (col.padding) wrapWidth -= (col.padding[left] || 0) + (col.padding[right] || 0)
|
2017-05-27 17:36:13 +02:00
|
|
|
if (col.border) wrapWidth -= 4
|
2017-05-03 15:35:00 +02:00
|
|
|
return wrapWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
UI.prototype._columnWidths = function (row) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var _this = this
|
|
|
|
var widths = []
|
|
|
|
var unset = row.length
|
|
|
|
var unsetWidth
|
|
|
|
var remainingWidth = this.width
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// column widths can be set in config.
|
|
|
|
row.forEach(function (col, i) {
|
|
|
|
if (col.width) {
|
|
|
|
unset--
|
|
|
|
widths[i] = col.width
|
|
|
|
remainingWidth -= col.width
|
|
|
|
} else {
|
|
|
|
widths[i] = undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// any unset widths should be calculated.
|
|
|
|
if (unset) unsetWidth = Math.floor(remainingWidth / unset)
|
|
|
|
widths.forEach(function (w, i) {
|
2017-05-27 17:36:13 +02:00
|
|
|
if (!_this.wrap) widths[i] = row[i].width || stringWidth(row[i].text)
|
2017-05-03 15:35:00 +02:00
|
|
|
else if (w === undefined) widths[i] = Math.max(unsetWidth, _minWidth(row[i]))
|
|
|
|
})
|
|
|
|
|
|
|
|
return widths
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculates the minimum width of
|
|
|
|
// a column, based on padding preferences.
|
|
|
|
function _minWidth (col) {
|
|
|
|
var padding = col.padding || []
|
2017-05-27 17:36:13 +02:00
|
|
|
var minWidth = 1 + (padding[left] || 0) + (padding[right] || 0)
|
|
|
|
if (col.border) minWidth += 4
|
|
|
|
return minWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
function alignRight (str, width) {
|
|
|
|
str = str.trim()
|
|
|
|
var padding = ''
|
|
|
|
var strWidth = stringWidth(str)
|
|
|
|
|
|
|
|
if (strWidth < width) {
|
|
|
|
padding = new Array(width - strWidth + 1).join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
return padding + str
|
|
|
|
}
|
|
|
|
|
|
|
|
function alignCenter (str, width) {
|
|
|
|
str = str.trim()
|
|
|
|
var padding = ''
|
|
|
|
var strWidth = stringWidth(str.trim())
|
|
|
|
|
|
|
|
if (strWidth < width) {
|
|
|
|
padding = new Array(parseInt((width - strWidth) / 2, 10) + 1).join(' ')
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
return padding + str
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (opts) {
|
|
|
|
opts = opts || {}
|
|
|
|
|
|
|
|
return new UI({
|
|
|
|
width: (opts || {}).width || 80,
|
|
|
|
wrap: typeof opts.wrap === 'boolean' ? opts.wrap : true
|
|
|
|
})
|
|
|
|
}
|