Skip to content

Commit

Permalink
Add support for fullwidth characters and Unicode surrogate pairs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and sindresorhus committed Jul 23, 2017
1 parent 4da60e7 commit f379a0e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"ansi"
],
"dependencies": {
"slice-ansi": "0.0.4",
"slice-ansi": "^1.0.0",
"string-width": "^2.0.0"
},
"devDependencies": {
Expand Down
14 changes: 11 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Truncate a string to a specific width in the terminal
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk).
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters.


## Install
Expand All @@ -27,8 +27,16 @@ cliTruncate('unicorn', 4, {position: 'start'});
cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'

cliTruncate('\u001b[31municorn\u001b[39m', 4);
//=> '\u001b[31muni\u001b[39m…'
cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni\u001B[39m…'

// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00…'

// Truncate fullwidth characters
cliTruncate('안녕하세요', 3);
//=> '안…'

// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
Expand Down
14 changes: 5 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import test from 'ava';
import m from './';

// Because of https://github.com/chalk/slice-ansi/issues/6
const mootEscapes = '\u001b[31m\u001b[31m\u001b[31m\u001b[31m';

test(t => {
t.is(m('unicorn', 4), 'uni…');
t.is(m('unicorn', 4, {position: 'end'}), 'uni…');
Expand All @@ -13,12 +10,11 @@ test(t => {
t.is(m('unicorn', 20), 'unicorn');
t.is(m('unicorn', 7), 'unicorn');
t.is(m('unicorn', 6), 'unico…');
t.is(m('\u001b[31municorn\u001b[39m', 7), '\u001b[31municorn\u001b[39m');
t.is(m('\u001b[31municorn\u001b[39m', 1), '…');
t.is(m('\u001b[31municorn\u001b[39m', 4), mootEscapes + '\u001b[31muni\u001b[39m…');
// TODO
// t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
// t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
t.is(m('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m');
t.is(m('\u001B[31municorn\u001B[39m', 1), '…');
t.is(m('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…');
t.is(m('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs');
t.is(m('안녕하세요', 3), '안…', 'wide char');
t.is(m('unicorn', 5, {position: 'start'}), '…corn');
t.is(m('unicorn', 6, {position: 'start'}), '…icorn');
t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn');
Expand Down

0 comments on commit f379a0e

Please sign in to comment.