@@ -12,10 +12,6 @@ use crate::{
12
12
use az:: { SaturatingAs , SaturatingCast } ;
13
13
use embedded_graphics:: { pixelcolor:: Rgb888 , prelude:: PixelColor } ;
14
14
15
- use crate :: plugin:: ansi:: utils:: try_parse_sgr;
16
- use ansi_parser:: AnsiSequence ;
17
- use as_slice:: AsSlice ;
18
-
19
15
/// Parser to break down a line into primitive elements used by measurement and rendering.
20
16
#[ derive( Debug ) ]
21
17
#[ must_use]
@@ -120,7 +116,7 @@ where
120
116
break ' lookahead;
121
117
}
122
118
123
- Some ( Token :: EscapeSequence ( _) ) => { }
119
+ Some ( Token :: ChangeTextStyle ( _) ) | Some ( Token :: MoveCursor { .. } ) => { }
124
120
125
121
_ => break ' lookahead,
126
122
}
@@ -184,15 +180,11 @@ where
184
180
Some ( Token :: Whitespace ( n, _) ) => spaces. consume ( n) . saturating_as ( ) ,
185
181
Some ( Token :: Tab ) => cursor. next_tab_width ( ) . saturating_as ( ) ,
186
182
187
- Some ( Token :: EscapeSequence ( AnsiSequence :: CursorForward ( by) ) ) => {
188
- ( by * handler. measure ( " " ) ) . saturating_as ( )
189
- }
190
-
191
- Some ( Token :: EscapeSequence ( AnsiSequence :: CursorBackward ( by) ) ) => {
192
- -( by * handler. measure ( " " ) ) . saturating_as :: < i32 > ( )
183
+ Some ( Token :: MoveCursor { chars, .. } ) => {
184
+ chars * handler. measure ( " " ) . saturating_as :: < i32 > ( )
193
185
}
194
186
195
- Some ( Token :: EscapeSequence ( _) ) => 0 ,
187
+ Some ( Token :: ChangeTextStyle ( _) ) => 0 ,
196
188
197
189
_ => return false ,
198
190
} ;
@@ -374,46 +366,39 @@ where
374
366
}
375
367
}
376
368
377
- Token :: EscapeSequence ( seq) => {
378
- match seq {
379
- AnsiSequence :: SetGraphicsMode ( vec) => {
380
- if let Some ( sgr) = try_parse_sgr ( vec. as_slice ( ) ) {
381
- handler. change_text_style ( sgr. into ( ) ) ?;
382
- }
383
- }
384
-
385
- AnsiSequence :: CursorForward ( n) => {
386
- // Cursor movement can't rely on the text, as it's permitted
387
- // to move the cursor outside of the current line.
388
- // Example:
389
- // (| denotes the cursor, [ and ] are the limits of the line):
390
- // [Some text| ]
391
- // Cursor forward 2 characters
392
- // [Some text | ]
393
- let delta = ( n * handler. measure ( " " ) ) . saturating_as ( ) ;
394
- match self . move_cursor ( delta) {
395
- Ok ( delta) | Err ( delta) => {
396
- handler. whitespace ( "" , 1 , delta. saturating_as ( ) ) ?;
397
- }
398
- }
399
- }
400
-
401
- AnsiSequence :: CursorBackward ( n) => {
402
- // The above poses an issue with variable-width fonts.
403
- // If cursor movement ignores the variable width, the cursor
404
- // will be placed in positions other than glyph boundaries.
405
- let delta = -( n * handler. measure ( " " ) ) . saturating_as :: < i32 > ( ) ;
406
- match self . move_cursor ( delta) {
407
- Ok ( delta) | Err ( delta) => {
408
- handler. move_cursor ( delta) ?;
409
- handler. whitespace ( "" , 1 , delta. abs ( ) . saturating_as ( ) ) ?;
410
- handler. move_cursor ( delta) ?;
411
- }
369
+ // Cursor movement can't rely on the text, as it's permitted
370
+ // to move the cursor outside of the current line.
371
+ // Example:
372
+ // (| denotes the cursor, [ and ] are the limits of the line):
373
+ // [Some text| ]
374
+ // Cursor forward 2 characters
375
+ // [Some text | ]
376
+ Token :: MoveCursor {
377
+ chars,
378
+ draw_background : true ,
379
+ } => {
380
+ let delta = chars * handler. measure ( " " ) . saturating_as :: < i32 > ( ) ;
381
+ match self . move_cursor ( delta) {
382
+ Ok ( delta) | Err ( delta) => {
383
+ if chars > 0 {
384
+ handler. whitespace ( "" , 1 , delta. saturating_as ( ) ) ?;
385
+ } else {
386
+ handler. move_cursor ( delta) ?;
387
+ handler. whitespace ( "" , 1 , delta. abs ( ) . saturating_as ( ) ) ?;
388
+ handler. move_cursor ( delta) ?;
412
389
}
413
390
}
391
+ }
392
+ }
414
393
415
- _ => {
416
- // ignore for now
394
+ Token :: MoveCursor {
395
+ chars,
396
+ draw_background : false ,
397
+ } => {
398
+ let delta = chars * handler. measure ( " " ) . saturating_as :: < i32 > ( ) ;
399
+ match self . move_cursor ( delta) {
400
+ Ok ( delta) | Err ( delta) => {
401
+ handler. move_cursor ( delta) ?;
417
402
}
418
403
}
419
404
}
0 commit comments