11use std:: {
22 cmp,
33 fmt:: { self , Display , Write } ,
4+ iter:: once,
45} ;
56
67pub mod style;
@@ -217,19 +218,30 @@ impl<'a> DisplayList<'a> {
217218 } else {
218219 false
219220 } ;
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 ;
220224 let range = text
221225 . char_indices ( )
222226 . 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
223230 . take_while ( |( _, ch) | {
231+ // Fast return to iterate over final byte position
232+ if ended {
233+ return false ;
234+ }
224235 // Make sure that the trimming on the right will fall within the terminal width.
225236 // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is.
226237 // For now, just accept that sometimes the code line will be longer than desired.
227238 taken += unicode_width:: UnicodeWidthChar :: width ( * ch) . unwrap_or ( 1 ) ;
228239 if taken > right - left {
229- return false ;
240+ ended = true ;
230241 }
231242 true
232243 } )
244+ // Reduce to start and end byte position
233245 . fold ( ( None , 0 ) , |acc, ( i, _) | {
234246 if acc. 0 . is_some ( ) {
235247 ( acc. 0 , i)
@@ -238,7 +250,8 @@ impl<'a> DisplayList<'a> {
238250 }
239251 } ) ;
240252
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) ?;
242255
243256 if cut_right {
244257 // We have stripped some code after the right-most span end, make it clear we did so.
0 commit comments