From e8b47f31acde9f4da9783d135b90665771a481c3 Mon Sep 17 00:00:00 2001 From: markichnich Date: Fri, 25 Aug 2023 17:37:27 +0200 Subject: [PATCH] prevent leaking moved cursor position on quit --- src/ui.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index a6ad632..ebb689b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -4,7 +4,10 @@ use std::cmp::Ordering::*; use std::io; use crossterm::{ - cursor::{MoveDown, MoveLeft, MoveRight, MoveTo, MoveToColumn, MoveUp, SetCursorStyle}, + cursor::{ + MoveDown, MoveLeft, MoveRight, MoveTo, MoveToColumn, MoveUp, RestorePosition, SavePosition, + SetCursorStyle, + }, execute, queue, style::{Color, SetBackgroundColor, SetForegroundColor}, terminal::{ @@ -43,15 +46,20 @@ where height, }; + queue!( + screen.ostream, + SavePosition, + EnterAlternateScreen, + Clear(All) + ) + .expect("[-]: Error: ui::init: Failed to enter alternate screen."); enable_raw_mode().expect("[-]: Error: ui::init: Failed to enable raw mode."); - queue!(screen.ostream, Clear(All), EnterAlternateScreen) - .expect("[-]: Error: ui::init: Failed to enter alternate screen."); screen } pub fn deinit(&mut self) -> io::Result<()> { disable_raw_mode()?; - execute!(self.ostream, Clear(All), LeaveAlternateScreen)?; + execute!(self.ostream, LeaveAlternateScreen, RestorePosition)?; Ok(()) } @@ -219,10 +227,7 @@ where fn draw_cursor(&mut self, state: &State) -> io::Result<()> { let (row, col) = (state.cur_row, state.cur_col); - let (x, y) = ( - (self.width / 2 - 14) as u16, - (self.height / 2 - 6) as u16, - ); + let (x, y) = ((self.width / 2 - 14) as u16, (self.height / 2 - 6) as u16); let (x, y) = (x + 2, y + 1); let (x, y) = (x + 2 * col as u16, y + row as u16); let x = match col {