1
1
use std:: {
2
2
cmp,
3
3
fmt:: { self , Display , Write } ,
4
+ iter:: once,
4
5
} ;
5
6
6
7
pub mod style;
@@ -217,19 +218,30 @@ impl<'a> DisplayList<'a> {
217
218
} else {
218
219
false
219
220
} ;
221
+ // Specifies that it will end on the next character, so it will return
222
+ // until the next one to the final condition.
223
+ let mut ended = false ;
220
224
let range = text
221
225
. char_indices ( )
222
226
. skip ( left)
227
+ // Complete char iterator with final character
228
+ . chain ( once ( ( text. len ( ) , '\0' ) ) )
229
+ // Take until the next one to the final condition
223
230
. take_while ( |( _, ch) | {
231
+ // Fast return to iterate over final byte position
232
+ if ended {
233
+ return false ;
234
+ }
224
235
// Make sure that the trimming on the right will fall within the terminal width.
225
236
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is.
226
237
// For now, just accept that sometimes the code line will be longer than desired.
227
238
taken += unicode_width:: UnicodeWidthChar :: width ( * ch) . unwrap_or ( 1 ) ;
228
239
if taken > right - left {
229
- return false ;
240
+ ended = true ;
230
241
}
231
242
true
232
243
} )
244
+ // Reduce to start and end byte position
233
245
. fold ( ( None , 0 ) , |acc, ( i, _) | {
234
246
if acc. 0 . is_some ( ) {
235
247
( acc. 0 , i)
@@ -238,7 +250,8 @@ impl<'a> DisplayList<'a> {
238
250
}
239
251
} ) ;
240
252
241
- text[ range. 0 . expect ( "One character at line" ) ..=range. 1 ] . fmt ( f) ?;
253
+ // Format text with margins
254
+ text[ range. 0 . expect ( "One character at line" ) ..range. 1 ] . fmt ( f) ?;
242
255
243
256
if cut_right {
244
257
// We have stripped some code after the right-most span end, make it clear we did so.
0 commit comments