Skip to content

Commit

Permalink
Drop implementations cleanup (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka authored and TimonPost committed Sep 19, 2019
1 parent 41de9a6 commit 06c7917
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crossterm_screen/src/screen/alternate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ impl AlternateScreen {
impl Drop for AlternateScreen {
/// This will switch back to the main screen on drop.
fn drop(&mut self) {
self.to_main().unwrap();
let _ = self.to_main();
}
}
18 changes: 11 additions & 7 deletions crossterm_screen/src/screen/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::sys;
///
/// Please note that if this type drops, the raw screen will be undone. To prevent this behaviour call `disable_drop`.
pub struct RawScreen {
drop: bool,
disable_raw_mode_on_drop: bool,
}

impl RawScreen {
Expand All @@ -37,7 +37,9 @@ impl RawScreen {

command.enable()?;

Ok(RawScreen { drop: true })
Ok(RawScreen {
disable_raw_mode_on_drop: true,
})
}

/// Put terminal back in original modes.
Expand All @@ -52,8 +54,8 @@ impl RawScreen {
}

/// This will disable the drop logic of this type, which means that the rawscreen will not be disabled when this instance goes out of scope.
pub fn disable_drop(&mut self) {
self.drop = false;
pub fn disable_raw_mode_on_drop(&mut self) {
self.disable_raw_mode_on_drop = false;
}
}

Expand All @@ -76,14 +78,16 @@ impl IntoRawMode for Stdout {
fn into_raw_mode(self) -> Result<RawScreen> {
RawScreen::into_raw_mode()?;
// this make's sure that raw screen will be disabled when it goes out of scope.
Ok(RawScreen { drop: true })
Ok(RawScreen {
disable_raw_mode_on_drop: true,
})
}
}

impl Drop for RawScreen {
fn drop(&mut self) {
if self.drop == true {
RawScreen::disable_raw_mode().unwrap();
if self.disable_raw_mode_on_drop {
let _ = RawScreen::disable_raw_mode();
}
}
}

0 comments on commit 06c7917

Please sign in to comment.