prevent leaking moved cursor position on quit

This commit is contained in:
markichnich 2023-08-25 17:37:27 +02:00
parent d5678967e3
commit e8b47f31ac

View File

@ -4,7 +4,10 @@ use std::cmp::Ordering::*;
use std::io; use std::io;
use crossterm::{ use crossterm::{
cursor::{MoveDown, MoveLeft, MoveRight, MoveTo, MoveToColumn, MoveUp, SetCursorStyle}, cursor::{
MoveDown, MoveLeft, MoveRight, MoveTo, MoveToColumn, MoveUp, RestorePosition, SavePosition,
SetCursorStyle,
},
execute, queue, execute, queue,
style::{Color, SetBackgroundColor, SetForegroundColor}, style::{Color, SetBackgroundColor, SetForegroundColor},
terminal::{ terminal::{
@ -43,15 +46,20 @@ where
height, 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."); 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 screen
} }
pub fn deinit(&mut self) -> io::Result<()> { pub fn deinit(&mut self) -> io::Result<()> {
disable_raw_mode()?; disable_raw_mode()?;
execute!(self.ostream, Clear(All), LeaveAlternateScreen)?; execute!(self.ostream, LeaveAlternateScreen, RestorePosition)?;
Ok(()) Ok(())
} }
@ -219,10 +227,7 @@ where
fn draw_cursor(&mut self, state: &State) -> io::Result<()> { fn draw_cursor(&mut self, state: &State) -> io::Result<()> {
let (row, col) = (state.cur_row, state.cur_col); let (row, col) = (state.cur_row, state.cur_col);
let (x, y) = ( let (x, y) = ((self.width / 2 - 14) as u16, (self.height / 2 - 6) as u16);
(self.width / 2 - 14) as u16,
(self.height / 2 - 6) as u16,
);
let (x, y) = (x + 2, y + 1); let (x, y) = (x + 2, y + 1);
let (x, y) = (x + 2 * col as u16, y + row as u16); let (x, y) = (x + 2 * col as u16, y + row as u16);
let x = match col { let x = match col {