Skip to content

Commit

Permalink
fix for span with ansi escape codes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Oct 16, 2015
1 parent 8141239 commit 8d1551a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
48 changes: 31 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var stringWidth = require('string-width')
var wrap = require('wrap-ansi')
var align = {
right: require('right-align'),
center: require('center-align')
right: alignRight,
center: alignCenter
}
var top = 0
var right = 1
Expand Down Expand Up @@ -127,8 +127,7 @@ UI.prototype.rowToString = function (row, lines) {

// 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]
ts = align[row[c].align](ts, wrapWidth)
if (stringWidth(ts) < wrapWidth) ts += new Array(width - stringWidth(ts)).join(' ')
}

Expand Down Expand Up @@ -158,8 +157,9 @@ UI.prototype.rowToString = function (row, lines) {
// if the full 'source' can render in
// the target line, do so.
UI.prototype._renderInline = function (source, previousLine, paddingLeft) {
var leadingWhitespace = source.match(/^ */)[0].length
var target = previousLine.text
var str = ''
var targetTextWidth = stringWidth(target.trimRight())

if (!previousLine.span) return source

Expand All @@ -170,21 +170,11 @@ 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) {
Expand Down Expand Up @@ -266,6 +256,30 @@ function _minWidth (col) {
return 1 + (padding[left] || 0) + (padding[right] || 0)
}

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 padding + str
}

module.exports = function (opts) {
opts = opts || {}

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"dependencies": {
"center-align": "^0.1.1",
"right-align": "^0.1.1",
"string-width": "^1.0.1",
"wrap-ansi": "^1.0.0"
},
Expand Down
23 changes: 23 additions & 0 deletions test/cliui.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,29 @@ describe('cliui', function () {

ui.toString().split('\n').should.eql(expected)
})

it('appends to prior line appropriately when strings contain ansi escape codes', function () {
var ui = cliui({
width: 40
})

ui.span(
{text: chalk.green('i am a string that will be wrapped'), width: 30}
)

ui.div(
{text: chalk.blue(' [required] [default: 99]'), align: 'right'}
)

var expected = [
'i am a string that will be',
'wrapped [required] [default: 99]'
]

ui.toString().split('\n').map(function (l) {
return stripAnsi(l)
}).should.eql(expected)
})
})

describe('layoutDSL', function () {
Expand Down

0 comments on commit 8d1551a

Please sign in to comment.