@@ -96,8 +96,9 @@ const wrapWord = (
9696
9797 if ( visible + characterLength > columns && visible > 0 ) {
9898 rows . push ( [ ] ) ;
99+
99100 currentLine = rows . at ( - 1 ) ! ;
100- visible = 0 ;
101+ visible = styledCharsWidth ( currentLine ) ;
101102 }
102103
103104 currentLine . push ( character ) ;
@@ -114,12 +115,20 @@ export const wrapStyledChars = (
114115 let currentWord : StyledChar [ ] = [ ] ;
115116
116117 for ( const char of styledChars ) {
117- if ( char . value === ' ' ) {
118+ if ( char . value === '\n ' ) {
118119 if ( currentWord . length > 0 ) {
119120 words . push ( currentWord ) ;
120121 }
121122
122123 currentWord = [ ] ;
124+ words . push ( [ char ] ) ;
125+ } else if ( char . value === ' ' ) {
126+ if ( currentWord . length > 0 ) {
127+ words . push ( currentWord ) ;
128+ }
129+
130+ currentWord = [ ] ;
131+ words . push ( [ char ] ) ;
123132 } else {
124133 currentWord . push ( char ) ;
125134 }
@@ -129,44 +138,57 @@ export const wrapStyledChars = (
129138 words . push ( currentWord ) ;
130139 }
131140
132- const space : StyledChar = {
133- type : 'char' ,
134- value : ' ' ,
135- fullWidth : false ,
136- styles : [ ] ,
137- } ;
141+ let isAtStartOfLogicalLine = true ;
138142
139- for ( const [ index , word ] of words . entries ( ) ) {
140- const wordWidth = styledCharsWidth ( word ) ;
141- let rowWidth = styledCharsWidth ( rows . at ( - 1 ) ! ) ;
143+ for ( const word of words ) {
144+ if ( word . length === 0 ) {
145+ continue ;
146+ }
142147
143- if ( index > 0 ) {
144- rows . at ( - 1 ) ! . push ( space ) ;
145- rowWidth ++ ;
148+ if ( word [ 0 ] ! . value === '\n' ) {
149+ rows . push ( [ ] ) ;
150+ isAtStartOfLogicalLine = true ;
151+ continue ;
146152 }
147153
148- if ( wordWidth > columns ) {
149- if ( index > 0 ) {
150- rows [ rows . length - 1 ] = rows . at ( - 1 ) ! . slice ( 0 , - 1 ) ;
154+ const wordWidth = styledCharsWidth ( word ) ;
155+ const rowWidth = styledCharsWidth ( rows . at ( - 1 ) ! ) ;
156+
157+ if ( rowWidth + wordWidth > columns ) {
158+ if (
159+ ! isAtStartOfLogicalLine &&
160+ word [ 0 ] ! . value === ' ' &&
161+ word . length === 1
162+ ) {
163+ continue ;
164+ }
151165
152- if ( rows . at ( - 1 ) ! . length > 0 ) {
153- rows . push ( [ ] ) ;
166+ if ( ! isAtStartOfLogicalLine ) {
167+ while ( rows . at ( - 1 ) ! . length > 0 && rows . at ( - 1 ) ! . at ( - 1 ) ! . value === ' ' ) {
168+ rows . at ( - 1 ) ! . pop ( ) ;
154169 }
155170 }
156171
157- wrapWord ( rows , word , columns ) ;
158- continue ;
159- }
172+ if ( wordWidth > columns ) {
173+ if ( rowWidth > 0 ) {
174+ rows . push ( [ ] ) ;
175+ }
160176
161- if ( rowWidth + wordWidth > columns && rowWidth > 0 ) {
162- if ( index > 0 ) {
163- rows [ rows . length - 1 ] = rows . at ( - 1 ) ! . slice ( 0 , - 1 ) ;
177+ wrapWord ( rows , word , columns ) ;
178+ } else {
179+ rows . push ( [ ] ) ;
180+ rows . at ( - 1 ) ! . push ( ...word ) ;
164181 }
165-
166- rows . push ( word ) ;
167182 } else {
168183 rows . at ( - 1 ) ! . push ( ...word ) ;
169184 }
185+
186+ if (
187+ isAtStartOfLogicalLine &&
188+ ! ( word [ 0 ] ! . value === ' ' && word . length === 1 )
189+ ) {
190+ isAtStartOfLogicalLine = false ;
191+ }
170192 }
171193
172194 return rows ;
0 commit comments