aboutsummaryrefslogtreecommitdiff
path: root/node_modules/cliui/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-27 17:36:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-27 17:36:13 +0200
commit5f466137ad6ac596600e3ff53c9b786815398445 (patch)
treef914c221874f0b16bf3def7ac01d59d1a99a3b0b /node_modules/cliui/index.js
parentc9f5ac8e763eda19aa0564179300cfff76785435 (diff)
node_modules, clean up package.json
Diffstat (limited to 'node_modules/cliui/index.js')
-rw-r--r--node_modules/cliui/index.js171
1 files changed, 107 insertions, 64 deletions
diff --git a/node_modules/cliui/index.js b/node_modules/cliui/index.js
index 31b4aa7b7..e501e78fd 100644
--- a/node_modules/cliui/index.js
+++ b/node_modules/cliui/index.js
@@ -1,12 +1,14 @@
-var wrap = require('wordwrap'),
- align = {
- right: require('right-align'),
- center: require('center-align')
- },
- top = 0,
- right = 1,
- bottom = 2,
- left = 3
+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
function UI (opts) {
this.width = opts.width
@@ -42,9 +44,9 @@ UI.prototype._shouldApplyLayoutDSL = function () {
}
UI.prototype._applyLayoutDSL = function (str) {
- var _this = this,
- rows = str.split('\n'),
- leftColumnWidth = 0
+ var _this = this
+ var rows = str.split('\n')
+ var leftColumnWidth = 0
// simple heuristic for layout, make sure the
// second column lines up along the left-hand.
@@ -52,10 +54,10 @@ UI.prototype._applyLayoutDSL = function (str) {
// than 50% of the screen.
rows.forEach(function (row) {
var columns = row.split('\t')
- if (columns.length > 1 && columns[0].length > leftColumnWidth) {
+ if (columns.length > 1 && stringWidth(columns[0]) > leftColumnWidth) {
leftColumnWidth = Math.min(
Math.floor(_this.width * 0.5),
- columns[0].length
+ stringWidth(columns[0])
)
}
})
@@ -68,7 +70,7 @@ UI.prototype._applyLayoutDSL = function (str) {
_this.div.apply(_this, columns.map(function (r, i) {
return {
text: r.trim(),
- padding: [0, r.match(/\s*$/)[0].length, 0, r.match(/^\s*/)[0].length],
+ padding: _this._measurePadding(r),
width: (i === 0 && columns.length > 1) ? leftColumnWidth : undefined
}
}))
@@ -79,13 +81,20 @@ UI.prototype._applyLayoutDSL = function (str) {
UI.prototype._colFromString = function (str) {
return {
- text: str
+ text: str,
+ padding: this._measurePadding(str)
}
}
+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]
+}
+
UI.prototype.toString = function () {
- var _this = this,
- lines = []
+ var _this = this
+ var lines = []
_this.rows.forEach(function (row, i) {
_this.rowToString(row, lines)
@@ -103,13 +112,13 @@ UI.prototype.toString = function () {
}
UI.prototype.rowToString = function (row, lines) {
- var _this = this,
- paddingLeft,
- rrows = this._rasterize(row),
- str = '',
- ts,
- width,
- wrapWidth
+ var _this = this
+ var padding
+ var rrows = this._rasterize(row)
+ var str = ''
+ var ts
+ var width
+ var wrapWidth
rrows.forEach(function (rrow, r) {
str = ''
@@ -118,27 +127,30 @@ UI.prototype.rowToString = function (row, lines) {
width = row[c].width // the width with padding.
wrapWidth = _this._negatePadding(row[c]) // the width without padding.
- for (var i = 0; i < Math.max(wrapWidth, col.length); i++) {
- ts += col.charAt(i) || ' '
+ ts += col
+
+ for (var i = 0; i < wrapWidth - stringWidth(col); i++) {
+ ts += ' '
}
// align the string within its column.
if (row[c].align && row[c].align !== 'left' && _this.wrap) {
- ts = align[row[c].align](ts.trim() + '\n' + new Array(wrapWidth + 1).join(' '))
- .split('\n')[0]
- if (ts.length < wrapWidth) ts += new Array(width - ts.length).join(' ')
+ ts = align[row[c].align](ts, wrapWidth)
+ if (stringWidth(ts) < wrapWidth) ts += new Array(width - stringWidth(ts)).join(' ')
}
- // add left/right padding and print string.
- paddingLeft = (row[c].padding || [0, 0, 0, 0])[left]
- if (paddingLeft) str += new Array(row[c].padding[left] + 1).join(' ')
+ // 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, '| ')
str += ts
- if (row[c].padding && row[c].padding[right]) str += new Array(row[c].padding[right] + 1).join(' ')
+ str += addBorder(row[c], ts, ' |')
+ if (padding[right]) str += new Array(padding[right] + 1).join(' ')
// if prior row is span, try to render the
// current row on the prior line.
if (r === 0 && lines.length > 0) {
- str = _this._renderInline(str, lines[lines.length - 1], paddingLeft)
+ str = _this._renderInline(str, lines[lines.length - 1])
}
})
@@ -152,11 +164,21 @@ UI.prototype.rowToString = function (row, lines) {
return lines
}
+function addBorder (col, ts, style) {
+ if (col.border) {
+ if (/[.']-+[.']/.test(ts)) return ''
+ else if (ts.trim().length) return style
+ else return ' '
+ }
+ return ''
+}
+
// if the full 'source' can render in
// the target line, do so.
-UI.prototype._renderInline = function (source, previousLine, paddingLeft) {
- var target = previousLine.text,
- str = ''
+UI.prototype._renderInline = function (source, previousLine) {
+ var leadingWhitespace = source.match(/^ */)[0].length
+ var target = previousLine.text
+ var targetTextWidth = stringWidth(target.trimRight())
if (!previousLine.span) return source
@@ -167,39 +189,34 @@ UI.prototype._renderInline = function (source, previousLine, paddingLeft) {
return target + source
}
- for (var i = 0, tc, sc; i < Math.max(source.length, target.length); i++) {
- tc = target.charAt(i) || ' '
- sc = source.charAt(i) || ' '
- // we tried to overwrite a character in the other string.
- if (tc !== ' ' && sc !== ' ') return source
- // there is not enough whitespace to maintain padding.
- if (sc !== ' ' && i < paddingLeft + target.length) return source
- // :thumbsup:
- if (tc === ' ') str += sc
- else str += tc
- }
+ if (leadingWhitespace < targetTextWidth) return source
previousLine.hidden = true
- return str
+ return target.trimRight() + new Array(leadingWhitespace - targetTextWidth + 1).join(' ') + source.trimLeft()
}
UI.prototype._rasterize = function (row) {
- var _this = this,
- i,
- rrow,
- rrows = [],
- widths = this._columnWidths(row),
- wrapped
+ var _this = this
+ var i
+ var rrow
+ var rrows = []
+ var widths = this._columnWidths(row)
+ var wrapped
// 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]
- if (_this.wrap) wrapped = wrap.hard(_this._negatePadding(col))(col.text).split('\n')
+ if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), {hard: true}).split('\n')
else wrapped = col.text.split('\n')
+ if (col.border) {
+ wrapped.unshift('.' + new Array(_this._negatePadding(col) + 3).join('-') + '.')
+ wrapped.push("'" + new Array(_this._negatePadding(col) + 3).join('-') + "'")
+ }
+
// add top and bottom padding.
if (col.padding) {
for (i = 0; i < (col.padding[top] || 0); i++) wrapped.unshift('')
@@ -224,15 +241,16 @@ UI.prototype._rasterize = function (row) {
UI.prototype._negatePadding = function (col) {
var wrapWidth = col.width
if (col.padding) wrapWidth -= (col.padding[left] || 0) + (col.padding[right] || 0)
+ if (col.border) wrapWidth -= 4
return wrapWidth
}
UI.prototype._columnWidths = function (row) {
- var _this = this,
- widths = [],
- unset = row.length,
- unsetWidth,
- remainingWidth = this.width
+ var _this = this
+ var widths = []
+ var unset = row.length
+ var unsetWidth
+ var remainingWidth = this.width
// column widths can be set in config.
row.forEach(function (col, i) {
@@ -248,7 +266,7 @@ UI.prototype._columnWidths = function (row) {
// any unset widths should be calculated.
if (unset) unsetWidth = Math.floor(remainingWidth / unset)
widths.forEach(function (w, i) {
- if (!_this.wrap) widths[i] = row[i].width || row[i].text.length
+ if (!_this.wrap) widths[i] = row[i].width || stringWidth(row[i].text)
else if (w === undefined) widths[i] = Math.max(unsetWidth, _minWidth(row[i]))
})
@@ -259,8 +277,33 @@ UI.prototype._columnWidths = function (row) {
// a column, based on padding preferences.
function _minWidth (col) {
var padding = col.padding || []
+ 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(' ')
+ }
- return 1 + (padding[left] || 0) + (padding[right] || 0)
+ return padding + str
}
module.exports = function (opts) {