-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for surrogate pairs and full width characters #20
Changes from 5 commits
fc4430c
0feb955
0885158
0f7c2ca
aedde44
e61b63c
ef6e6c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,14 +45,23 @@ const wordLengths = str => str.split(' ').map(s => stringWidth(s)); | |
|
||
// Wrap a long word across multiple rows | ||
// Ansi escape codes do not count towards length | ||
function wrapWord(rows, word, cols) { | ||
const wrapWord = (rows, word, cols) => { | ||
const arr = Array.from(word); | ||
|
||
let insideEscape = false; | ||
let visible = stripAnsi(rows[rows.length - 1]).length; | ||
let visible = stringWidth(stripAnsi(rows[rows.length - 1])); | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
const x = word[i]; | ||
for (const item of arr.entries()) { | ||
const i = item[0]; | ||
const x = item[1]; | ||
const charLength = stringWidth(x); | ||
|
||
rows[rows.length - 1] += x; | ||
if (visible + charLength <= cols) { | ||
rows[rows.length - 1] += x; | ||
} else { | ||
rows.push(x); | ||
visible = 0; | ||
} | ||
|
||
if (ESCAPES.indexOf(x) !== -1) { | ||
insideEscape = true; | ||
|
@@ -65,9 +74,9 @@ function wrapWord(rows, word, cols) { | |
continue; | ||
} | ||
|
||
visible++; | ||
visible += charLength; | ||
|
||
if (visible >= cols && i < word.length - 1) { | ||
if (visible === cols && i < arr.length - 1) { | ||
rows.push(''); | ||
visible = 0; | ||
} | ||
|
@@ -78,7 +87,7 @@ function wrapWord(rows, word, cols) { | |
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) { | ||
rows[rows.length - 2] += rows.pop(); | ||
} | ||
} | ||
}; | ||
|
||
// The wrap-ansi module can be invoked | ||
// in either 'hard' or 'soft' wrap mode | ||
|
@@ -87,7 +96,7 @@ function wrapWord(rows, word, cols) { | |
// than cols characters | ||
// | ||
// 'soft' allows long words to expand past the column length | ||
function exec(str, cols, opts) { | ||
const exec = (str, cols, opts) => { | ||
const options = opts || {}; | ||
|
||
let pre = ''; | ||
|
@@ -98,7 +107,10 @@ function exec(str, cols, opts) { | |
const words = str.split(' '); | ||
const rows = ['']; | ||
|
||
for (let i = 0, word; (word = words[i]) !== undefined; i++) { | ||
for (const item of Array.from(words).entries()) { | ||
const i = item[0]; | ||
const x = item[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a more descriptive variable than |
||
|
||
let rowLength = stringWidth(rows[rows.length - 1]); | ||
|
||
if (rowLength) { | ||
|
@@ -112,56 +124,58 @@ function exec(str, cols, opts) { | |
if (rowLength) { | ||
rows.push(''); | ||
} | ||
wrapWord(rows, word, cols); | ||
wrapWord(rows, x, cols); | ||
continue; | ||
} | ||
|
||
if (rowLength + lengths[i] > cols && rowLength > 0) { | ||
if (options.wordWrap === false && rowLength < cols) { | ||
wrapWord(rows, word, cols); | ||
wrapWord(rows, x, cols); | ||
continue; | ||
} | ||
|
||
rows.push(''); | ||
} | ||
|
||
if (rowLength + lengths[i] > cols && options.wordWrap === false) { | ||
wrapWord(rows, word, cols); | ||
wrapWord(rows, x, cols); | ||
continue; | ||
} | ||
|
||
rows[rows.length - 1] += word; | ||
rows[rows.length - 1] += x; | ||
} | ||
|
||
pre = rows.map(x => x.trim()).join('\n'); | ||
|
||
for (let j = 0; j < pre.length; j++) { | ||
const y = pre[j]; | ||
for (const item of Array.from(pre).entries()) { | ||
const i = item[0]; | ||
const x = item[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call |
||
|
||
ret += y; | ||
ret += x; | ||
|
||
if (ESCAPES.indexOf(y) !== -1) { | ||
const code = parseFloat(/\d[^m]*/.exec(pre.slice(j, j + 4))); | ||
if (ESCAPES.indexOf(x) !== -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make |
||
const code = parseFloat(/\d[^m]*/.exec(pre.slice(i, i + 4))); | ||
escapeCode = code === END_CODE ? null : code; | ||
} | ||
|
||
const code = ESCAPE_CODES.get(parseInt(escapeCode, 10)); | ||
const code = ESCAPE_CODES.get(Number(escapeCode)); | ||
|
||
if (escapeCode && code) { | ||
if (pre[j + 1] === '\n') { | ||
if (pre[i + 1] === '\n') { | ||
ret += wrapAnsi(code); | ||
} else if (y === '\n') { | ||
} else if (x === '\n') { | ||
ret += wrapAnsi(escapeCode); | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
}; | ||
|
||
// For each newline, invoke the method separately | ||
module.exports = (str, cols, opts) => { | ||
return String(str) | ||
.normalize() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To normalize unicode characters that creates the same characters but with different code points and possibly different lengths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this which both produces '\xF1' === 'n\u0303'
//=> false |
||
.split('\n') | ||
.map(line => exec(line, cols, opts)) | ||
.join('\n'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,12 +86,11 @@ test('no word-wrapping', t => { | |
t.is(res3, 'The q\nuick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe l\nazy \u001B[32md\u001B[39m\n\u001B[32mog an\u001B[39m\n\u001B[32md the\u001B[39m\n\u001B[32mn ran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe u\u001B[39m\n\u001B[32mnicor\u001B[39m\n\u001B[32mn.\u001B[39m'); | ||
}); | ||
|
||
// https://github.com/chalk/wrap-ansi/issues/10 | ||
test.failing('supports fullwidth characters', t => { | ||
test('supports fullwidth characters', t => { | ||
t.is(m('안녕하세', 4, {hard: true}), '안녕\n하세'); | ||
}); | ||
|
||
// https://github.com/chalk/wrap-ansi/issues/11 | ||
test.failing('supports unicode surrogate pairs', t => { | ||
test('supports unicode surrogate pairs', t => { | ||
t.is(m('a\ud83c\ude00bc', 2, {hard: true}), 'a\n\ud83c\ude00\nbc'); | ||
t.is(m('a\ud83c\ude00bc\ud83c\ude00d\ud83c\ude00', 2, {hard: true}), 'a\n\ud83c\ude00\nbc\n\ud83c\ude00\nd\n\ud83c\ude00'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use uppercase escapes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe we should set
visible = 0
here too since it's reset.