Skip to content

Commit f379a0e

Browse files
kevvasindresorhus
authored andcommittedJul 23, 2017
Add support for fullwidth characters and Unicode surrogate pairs (#6)
1 parent 4da60e7 commit f379a0e

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"ansi"
3333
],
3434
"dependencies": {
35-
"slice-ansi": "0.0.4",
35+
"slice-ansi": "^1.0.0",
3636
"string-width": "^2.0.0"
3737
},
3838
"devDependencies": {

‎readme.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Truncate a string to a specific width in the terminal
44
5-
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).
5+
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.
66

77

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

30-
cliTruncate('\u001b[31municorn\u001b[39m', 4);
31-
//=> '\u001b[31muni\u001b[39m…'
30+
cliTruncate('\u001B[31municorn\u001B[39m', 4);
31+
//=> '\u001B[31muni\u001B[39m…'
32+
33+
// Truncate Unicode surrogate pairs
34+
cliTruncate('uni\uD83C\uDE00corn', 5);
35+
//=> 'uni\uD83C\uDE00…'
36+
37+
// Truncate fullwidth characters
38+
cliTruncate('안녕하세요', 3);
39+
//=> '안…'
3240

3341
// Truncate the paragraph to the terminal width
3442
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';

‎test.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import test from 'ava';
22
import m from './';
33

4-
// Because of https://github.com/chalk/slice-ansi/issues/6
5-
const mootEscapes = '\u001b[31m\u001b[31m\u001b[31m\u001b[31m';
6-
74
test(t => {
85
t.is(m('unicorn', 4), 'uni…');
96
t.is(m('unicorn', 4, {position: 'end'}), 'uni…');
@@ -13,12 +10,11 @@ test(t => {
1310
t.is(m('unicorn', 20), 'unicorn');
1411
t.is(m('unicorn', 7), 'unicorn');
1512
t.is(m('unicorn', 6), 'unico…');
16-
t.is(m('\u001b[31municorn\u001b[39m', 7), '\u001b[31municorn\u001b[39m');
17-
t.is(m('\u001b[31municorn\u001b[39m', 1), '…');
18-
t.is(m('\u001b[31municorn\u001b[39m', 4), mootEscapes + '\u001b[31muni\u001b[39m…');
19-
// TODO
20-
// t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
21-
// t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
13+
t.is(m('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m');
14+
t.is(m('\u001B[31municorn\u001B[39m', 1), '…');
15+
t.is(m('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…');
16+
t.is(m('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs');
17+
t.is(m('안녕하세요', 3), '안…', 'wide char');
2218
t.is(m('unicorn', 5, {position: 'start'}), '…corn');
2319
t.is(m('unicorn', 6, {position: 'start'}), '…icorn');
2420
t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn');

0 commit comments

Comments
 (0)
Please sign in to comment.