@@ -2,7 +2,7 @@ import test from 'ava';
2
2
import chalk from 'chalk' ;
3
3
import hasAnsi from 'has-ansi' ;
4
4
import stripAnsi from 'strip-ansi' ;
5
- import m from '.' ;
5
+ import wrapAnsi from '.' ;
6
6
7
7
chalk . enabled = true ;
8
8
@@ -15,130 +15,128 @@ const fixture4 = '12345678\n';
15
15
const fixture5 = '12345678\n ' ;
16
16
17
17
test ( 'wraps string at 20 characters' , t => {
18
- const res20 = m ( fixture , 20 ) ;
18
+ const result = wrapAnsi ( fixture , 20 ) ;
19
19
20
- t . is ( res20 , 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m' ) ;
21
- t . true ( stripAnsi ( res20 ) . split ( '\n' ) . every ( x => x . length <= 20 ) ) ;
20
+ t . is ( result , 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m' ) ;
21
+ t . true ( stripAnsi ( result ) . split ( '\n' ) . every ( line => line . length <= 20 ) ) ;
22
22
} ) ;
23
23
24
24
test ( 'wraps string at 30 characters' , t => {
25
- const res30 = m ( fixture , 30 ) ;
25
+ const result = wrapAnsi ( fixture , 30 ) ;
26
26
27
- t . is ( res30 , 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m' ) ;
28
- t . true ( stripAnsi ( res30 ) . split ( '\n' ) . every ( x => x . length <= 30 ) ) ;
27
+ t . is ( result , 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m' ) ;
28
+ t . true ( stripAnsi ( result ) . split ( '\n' ) . every ( line => line . length <= 30 ) ) ;
29
29
} ) ;
30
30
31
31
test ( 'does not break strings longer than "cols" characters' , t => {
32
- const res5 = m ( fixture , 5 , { hard : false } ) ;
32
+ const result = wrapAnsi ( fixture , 5 , { hard : false } ) ;
33
33
34
- t . is ( res5 , 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m' ) ;
35
- t . true ( stripAnsi ( res5 ) . split ( '\n' ) . filter ( x => x . length > 5 ) . length > 0 ) ;
34
+ t . is ( result , 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m' ) ;
35
+ t . true ( stripAnsi ( result ) . split ( '\n' ) . filter ( line => line . length > 5 ) . length > 0 ) ;
36
36
} ) ;
37
37
38
38
test ( 'handles colored string that wraps on to multiple lines' , t => {
39
- const res = m ( chalk . green ( 'hello world' ) + ' hey!' , 5 , { hard : false } ) ;
40
- const lines = res . split ( '\n' ) ;
39
+ const result = wrapAnsi ( chalk . green ( 'hello world' ) + ' hey!' , 5 , { hard : false } ) ;
40
+ const lines = result . split ( '\n' ) ;
41
41
t . true ( hasAnsi ( lines [ 0 ] ) ) ;
42
42
t . true ( hasAnsi ( lines [ 1 ] ) ) ;
43
43
t . false ( hasAnsi ( lines [ 2 ] ) ) ;
44
44
} ) ;
45
45
46
46
test ( 'does not prepend newline if first string is greater than "cols"' , t => {
47
- const res = m ( chalk . green ( 'hello' ) + '-world' , 5 , { hard : false } ) ;
48
- t . is ( res . split ( '\n' ) . length , 1 ) ;
47
+ const result = wrapAnsi ( chalk . green ( 'hello' ) + '-world' , 5 , { hard : false } ) ;
48
+ t . is ( result . split ( '\n' ) . length , 1 ) ;
49
49
} ) ;
50
50
51
51
// When "hard" is true
52
52
53
53
test ( 'breaks strings longer than "cols" characters' , t => {
54
- const res5 = m ( fixture , 5 , { hard : true } ) ;
54
+ const result = wrapAnsi ( fixture , 5 , { hard : true } ) ;
55
55
56
- t . is ( res5 , 'The\nquick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m' ) ;
57
- t . true ( stripAnsi ( res5 ) . split ( '\n' ) . every ( x => x . length <= 5 ) ) ;
56
+ t . is ( result , 'The\nquick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m' ) ;
57
+ t . true ( stripAnsi ( result ) . split ( '\n' ) . every ( line => line . length <= 5 ) ) ;
58
58
} ) ;
59
59
60
60
test ( 'removes last row if it contained only ansi escape codes' , t => {
61
- const res = m ( chalk . green ( 'helloworld' ) , 2 , { hard : true } ) ;
62
- t . true ( stripAnsi ( res ) . split ( '\n' ) . every ( x => x . length === 2 ) ) ;
61
+ const result = wrapAnsi ( chalk . green ( 'helloworld' ) , 2 , { hard : true } ) ;
62
+ t . true ( stripAnsi ( result ) . split ( '\n' ) . every ( x => x . length === 2 ) ) ;
63
63
} ) ;
64
64
65
65
test ( 'does not prepend newline if first word is split' , t => {
66
- const res = m ( chalk . green ( 'hello' ) + 'world' , 5 , { hard : true } ) ;
67
- t . is ( res . split ( '\n' ) . length , 2 ) ;
66
+ const result = wrapAnsi ( chalk . green ( 'hello' ) + 'world' , 5 , { hard : true } ) ;
67
+ t . is ( result . split ( '\n' ) . length , 2 ) ;
68
68
} ) ;
69
69
70
70
test ( 'takes into account line returns inside input' , t => {
71
- const res20 = m ( fixture2 , 10 , { hard : true } ) ;
72
- t . is ( res20 , '12345678\n9012345678\n90' ) ;
71
+ t . is ( wrapAnsi ( fixture2 , 10 , { hard : true } ) , '12345678\n9012345678\n90' ) ;
73
72
} ) ;
74
73
75
74
test ( 'word wrapping' , t => {
76
- const res = m ( fixture3 , 15 ) ;
77
- t . is ( res , '12345678\n901234567890\n12345' ) ;
75
+ t . is ( wrapAnsi ( fixture3 , 15 ) , '12345678\n901234567890\n12345' ) ;
78
76
} ) ;
79
77
80
78
test ( 'no word-wrapping' , t => {
81
- const res = m ( fixture3 , 15 , { wordWrap : false } ) ;
82
- t . is ( res , '12345678\n901234567890 12\n345' ) ;
79
+ const result = wrapAnsi ( fixture3 , 15 , { wordWrap : false } ) ;
80
+ t . is ( result , '12345678\n901234567890 12\n345' ) ;
83
81
84
- const res2 = m ( fixture3 , 5 , { wordWrap : false } ) ;
85
- t . is ( res2 , '12345\n678\n90123\n45678\n90 12\n345' ) ;
82
+ const result2 = wrapAnsi ( fixture3 , 5 , { wordWrap : false } ) ;
83
+ t . is ( result2 , '12345\n678\n90123\n45678\n90 12\n345' ) ;
86
84
87
- const res3 = m ( fixture5 , 5 , { wordWrap : false } ) ;
88
- t . is ( res3 , '12345\n678\n' ) ;
85
+ const rsult3 = wrapAnsi ( fixture5 , 5 , { wordWrap : false } ) ;
86
+ t . is ( rsult3 , '12345\n678\n' ) ;
89
87
90
- const res4 = m ( fixture , 5 , { wordWrap : false } ) ;
91
- t . is ( res4 , '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' ) ;
88
+ const result4 = wrapAnsi ( fixture , 5 , { wordWrap : false } ) ;
89
+ t . is ( result4 , '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' ) ;
92
90
} ) ;
93
91
94
92
test ( 'no word-wrapping and no trimming' , t => {
95
- const res = m ( fixture3 , 13 , { wordWrap : false , trim : false } ) ;
96
- t . is ( res , '12345678\n901234567890 \n12345' ) ;
93
+ const result = wrapAnsi ( fixture3 , 13 , { wordWrap : false , trim : false } ) ;
94
+ t . is ( result , '12345678\n901234567890 \n12345' ) ;
97
95
98
- const res2 = m ( fixture4 , 5 , { wordWrap : false , trim : false } ) ;
99
- t . is ( res2 , '12345\n678\n' ) ;
96
+ const result2 = wrapAnsi ( fixture4 , 5 , { wordWrap : false , trim : false } ) ;
97
+ t . is ( result2 , '12345\n678\n' ) ;
100
98
101
- const res3 = m ( fixture5 , 5 , { wordWrap : false , trim : false } ) ;
102
- t . is ( res3 , '12345\n678\n ' ) ;
99
+ const result3 = wrapAnsi ( fixture5 , 5 , { wordWrap : false , trim : false } ) ;
100
+ t . is ( result3 , '12345\n678\n ' ) ;
103
101
104
- const res4 = m ( fixture , 5 , { wordWrap : false , trim : false } ) ;
105
- t . is ( res4 , 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\n[31mjumpe[39m\n[31md ove[39m\n[31mr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\n[32mand t[39m\n[32mhen r[39m\n[32man aw[39m\n[32may wi[39m\n[32mth th[39m\n[32me uni[39m\n[32mcorn.\u001B[39m' ) ;
102
+ const result4 = wrapAnsi ( fixture , 5 , { wordWrap : false , trim : false } ) ;
103
+ t . is ( result4 , 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\n[31mjumpe[39m\n[31md ove[39m\n[31mr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\n[32mand t[39m\n[32mhen r[39m\n[32man aw[39m\n[32may wi[39m\n[32mth th[39m\n[32me uni[39m\n[32mcorn.\u001B[39m' ) ;
106
104
} ) ;
107
105
108
106
test ( 'supports fullwidth characters' , t => {
109
- t . is ( m ( '안녕하세' , 4 , { hard : true } ) , '안녕\n하세' ) ;
107
+ t . is ( wrapAnsi ( '안녕하세' , 4 , { hard : true } ) , '안녕\n하세' ) ;
110
108
} ) ;
111
109
112
110
test ( 'supports unicode surrogate pairs' , t => {
113
- t . is ( m ( 'a\uD83C\uDE00bc' , 2 , { hard : true } ) , 'a\n\uD83C\uDE00\nbc' ) ;
114
- 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' ) ;
111
+ t . is ( wrapAnsi ( 'a\uD83C\uDE00bc' , 2 , { hard : true } ) , 'a\n\uD83C\uDE00\nbc' ) ;
112
+ t . is ( wrapAnsi ( 'a\uD83C\uDE00bc\uD83C\uDE00d\uD83C\uDE00' , 2 , { hard : true } ) , 'a\n\uD83C\uDE00\nbc\n\uD83C\uDE00\nd\n\uD83C\uDE00' ) ;
115
113
} ) ;
116
114
117
115
test ( '#23, properly wraps whitespace with no trimming' , t => {
118
- t . is ( m ( ' ' , 2 , { trim : false } ) , ' \n ' ) ;
119
- t . is ( m ( ' ' , 2 , { trim : false , hard : true } ) , ' \n ' ) ;
116
+ t . is ( wrapAnsi ( ' ' , 2 , { trim : false } ) , ' \n ' ) ;
117
+ t . is ( wrapAnsi ( ' ' , 2 , { trim : false , hard : true } ) , ' \n ' ) ;
120
118
} ) ;
121
119
122
120
test ( '#24, trims leading and trailing whitespace only on actual wrapped lines and only with trimming' , t => {
123
- t . is ( m ( ' foo bar ' , 6 ) , 'foo\nbar' ) ;
124
- t . is ( m ( ' foo bar ' , 42 ) , 'foo bar' ) ;
125
- t . is ( m ( ' foo bar ' , 42 , { trim : false } ) , ' foo bar ' ) ;
121
+ t . is ( wrapAnsi ( ' foo bar ' , 6 ) , 'foo\nbar' ) ;
122
+ t . is ( wrapAnsi ( ' foo bar ' , 42 ) , 'foo bar' ) ;
123
+ t . is ( wrapAnsi ( ' foo bar ' , 42 , { trim : false } ) , ' foo bar ' ) ;
126
124
} ) ;
127
125
128
126
test ( '#25, properly wraps whitespace between words with no trimming' , t => {
129
- t . is ( m ( 'foo bar' , 3 ) , 'foo\nbar' ) ;
130
- t . is ( m ( 'foo bar' , 3 , { hard : true } ) , 'foo\nbar' ) ;
131
- t . is ( m ( 'foo bar' , 3 , { trim : false } ) , 'foo\n \nbar' ) ;
132
- t . is ( m ( 'foo bar' , 3 , { trim : false , hard : true } ) , 'foo\n \nbar' ) ;
127
+ t . is ( wrapAnsi ( 'foo bar' , 3 ) , 'foo\nbar' ) ;
128
+ t . is ( wrapAnsi ( 'foo bar' , 3 , { hard : true } ) , 'foo\nbar' ) ;
129
+ t . is ( wrapAnsi ( 'foo bar' , 3 , { trim : false } ) , 'foo\n \nbar' ) ;
130
+ t . is ( wrapAnsi ( 'foo bar' , 3 , { trim : false , hard : true } ) , 'foo\n \nbar' ) ;
133
131
} ) ;
134
132
135
133
test ( '#26, does not multiplicate leading spaces with no trimming' , t => {
136
- t . is ( m ( ' a ' , 10 , { trim : false } ) , ' a ' ) ;
137
- t . is ( m ( ' a ' , 10 , { trim : false } ) , ' a ' ) ;
134
+ t . is ( wrapAnsi ( ' a ' , 10 , { trim : false } ) , ' a ' ) ;
135
+ t . is ( wrapAnsi ( ' a ' , 10 , { trim : false } ) , ' a ' ) ;
138
136
} ) ;
139
137
140
138
test ( '#27, does not remove spaces in line with ansi escapes when no trimming' , t => {
141
- t . is ( m ( chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) , 100 , { trim : false } ) , chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) ) ;
142
- t . is ( m ( chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) , 100 , { trim : false } ) , chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) ) ;
143
- t . is ( m ( chalk . bgGreen ( ' hello ' ) , 10 , { hard : true , trim : false } ) , chalk . bgGreen ( ' hello ' ) ) ;
139
+ t . is ( wrapAnsi ( chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) , 100 , { trim : false } ) , chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) ) ;
140
+ t . is ( wrapAnsi ( chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) , 100 , { trim : false } ) , chalk . bgGreen ( ` ${ chalk . black ( 'OK' ) } ` ) ) ;
141
+ t . is ( wrapAnsi ( chalk . bgGreen ( ' hello ' ) , 10 , { hard : true , trim : false } ) , chalk . bgGreen ( ' hello ' ) ) ;
144
142
} ) ;
0 commit comments