File tree Expand file tree Collapse file tree 3 files changed +16
-10
lines changed Expand file tree Collapse file tree 3 files changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ use std::{
9
9
} ;
10
10
use unicode_width:: UnicodeWidthChar ;
11
11
12
- #[ derive( Clone ) ]
12
+ #[ derive( Clone , Debug , PartialEq ) ]
13
13
struct Character {
14
14
value : char ,
15
15
style : CanvasTextStyle ,
@@ -28,7 +28,7 @@ pub struct CanvasTextStyle {
28
28
pub underline : bool ,
29
29
}
30
30
31
- #[ derive( Clone , Default ) ]
31
+ #[ derive( Clone , Default , PartialEq ) ]
32
32
struct Cell {
33
33
background_color : Option < Color > ,
34
34
character : Option < Character > ,
@@ -43,6 +43,7 @@ impl Cell {
43
43
/// Canvas is a low-level abstraction for rendering output. Most users of the library will not need
44
44
/// to use it directly. However, it is used by low level component implementations and can be used
45
45
/// to store and copy their output.
46
+ #[ derive( Clone , PartialEq ) ]
46
47
pub struct Canvas {
47
48
width : usize ,
48
49
cells : Vec < Vec < Cell > > ,
Original file line number Diff line number Diff line change @@ -354,18 +354,19 @@ impl<'a> Tree<'a> {
354
354
W : Write ,
355
355
{
356
356
let mut terminal = Terminal :: new ( ) ?;
357
- let mut lines_to_rewind_to_clear = 0 ;
357
+ let mut prev_canvas : Option < Canvas > = None ;
358
358
loop {
359
359
let width = terminal. width ( ) . ok ( ) . map ( |w| w as usize ) ;
360
360
execute ! ( w, terminal:: BeginSynchronizedUpdate , ) ?;
361
+ let lines_to_rewind_to_clear = prev_canvas. as_ref ( ) . map_or ( 0 , |c| c. height ( ) ) ;
361
362
let output = self . render ( width, Some ( & mut terminal) , lines_to_rewind_to_clear) ;
362
- if !output. did_clear_terminal_output && lines_to_rewind_to_clear > 0 {
363
- terminal. rewind_lines ( lines_to_rewind_to_clear as _ ) ?;
363
+ if output. did_clear_terminal_output || prev_canvas. as_ref ( ) != Some ( & output. canvas ) {
364
+ if !output. did_clear_terminal_output {
365
+ terminal. rewind_lines ( lines_to_rewind_to_clear as _ ) ?;
366
+ }
367
+ output. canvas . write_ansi ( & mut w) ?;
364
368
}
365
- // TODO: if we wanted to be efficient and the terminal wasn't cleared, we could
366
- // only write the diff
367
- output. canvas . write_ansi ( & mut w) ?;
368
- lines_to_rewind_to_clear = output. canvas . height ( ) ;
369
+ prev_canvas = Some ( output. canvas ) ;
369
370
execute ! ( w, terminal:: EndSynchronizedUpdate ) ?;
370
371
if self . system_context . should_exit ( ) || terminal. received_ctrl_c ( ) {
371
372
break ;
Original file line number Diff line number Diff line change @@ -181,7 +181,11 @@ impl Terminal {
181
181
}
182
182
183
183
pub fn rewind_lines ( & mut self , lines : u16 ) -> io:: Result < ( ) > {
184
- self . inner . rewind_lines ( lines)
184
+ if lines > 0 {
185
+ self . inner . rewind_lines ( lines)
186
+ } else {
187
+ Ok ( ( ) )
188
+ }
185
189
}
186
190
187
191
pub fn received_ctrl_c ( & self ) -> bool {
You can’t perform that action at this time.
0 commit comments