@@ -330,8 +330,8 @@ impl ConsoleRenderer {
330330 } ) ?)
331331 }
332332
333- fn set_cursor_visible ( & mut self , visible : BOOL ) -> Result < ( ) > {
334- set_cursor_visible ( self . conout , visible)
333+ fn set_cursor_visibility ( & mut self , visible : bool ) -> Result < Option < ConsoleCursorGuard > > {
334+ set_cursor_visibility ( self . conout , visible)
335335 }
336336
337337 // You can't have both ENABLE_WRAP_AT_EOL_OUTPUT and
@@ -374,16 +374,28 @@ impl ConsoleRenderer {
374374 }
375375}
376376
377- fn set_cursor_visible ( handle : HANDLE , visible : BOOL ) -> Result < ( ) > {
377+ pub struct ConsoleCursorGuard ( HANDLE ) ;
378+
379+ impl Drop for ConsoleCursorGuard {
380+ fn drop ( & mut self ) {
381+ let _ = set_cursor_visibility ( self . 0 , true ) ;
382+ }
383+ }
384+
385+ fn set_cursor_visibility ( handle : HANDLE , visible : bool ) -> Result < Option < ConsoleCursorGuard > > {
378386 let mut info = unsafe { mem:: zeroed ( ) } ;
379387 check ( unsafe { wincon:: GetConsoleCursorInfo ( handle, & mut info) } ) ?;
380- if info. bVisible == visible {
381- return Ok ( ( ) ) ;
388+ let b = if visible { TRUE } else { FALSE } ;
389+ if info. bVisible == b {
390+ return Ok ( None ) ;
382391 }
383- info. bVisible = visible;
384- Ok ( check ( unsafe {
385- wincon:: SetConsoleCursorInfo ( handle, & info)
386- } ) ?)
392+ info. bVisible = b;
393+ check ( unsafe { wincon:: SetConsoleCursorInfo ( handle, & info) } ) ?;
394+ Ok ( if visible {
395+ None
396+ } else {
397+ Some ( ConsoleCursorGuard ( handle) )
398+ } )
387399}
388400
389401impl Renderer for ConsoleRenderer {
@@ -449,11 +461,8 @@ impl Renderer for ConsoleRenderer {
449461 }
450462 }
451463 let info = self . get_console_screen_buffer_info ( ) ?;
452- self . set_cursor_visible ( FALSE ) ?; // just to avoid flickering
453- let handle = self . conout ;
454- scopeguard:: defer! {
455- let _ = set_cursor_visible( handle, TRUE ) ;
456- }
464+ // just to avoid flickering
465+ let mut guard = self . set_cursor_visibility ( false ) ?;
457466 // position at the start of the prompt, clear to end of previous input
458467 self . clear_old_rows ( & info, old_layout) ?;
459468 // display prompt, input line and hint
@@ -465,7 +474,7 @@ impl Renderer for ConsoleRenderer {
465474 coord. X = cursor. col as i16 ;
466475 coord. Y -= ( end_pos. row - cursor. row ) as i16 ;
467476 self . set_console_cursor_position ( coord, info. dwSize ) ?;
468-
477+ guard . take ( ) ;
469478 Ok ( ( ) )
470479 }
471480
@@ -628,6 +637,7 @@ impl Console {
628637}
629638
630639impl Term for Console {
640+ type CursorGuard = ConsoleCursorGuard ;
631641 type ExternalPrinter = ExternalPrinter ;
632642 type KeyMap = ConsoleKeyMap ;
633643 type Mode = ConsoleMode ;
@@ -831,6 +841,14 @@ impl Term for Console {
831841 conout : self . conout ,
832842 } )
833843 }
844+
845+ fn set_cursor_visibility ( & mut self , visible : bool ) -> Result < Option < ConsoleCursorGuard > > {
846+ if self . conout_isatty {
847+ set_cursor_visibility ( self . conout , visible)
848+ } else {
849+ Ok ( None )
850+ }
851+ }
834852}
835853
836854impl Drop for Console {
0 commit comments