Skip to content

Commit

Permalink
fix disabling/enabling raw mode in editor example (fix #44)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 6, 2023
1 parent 2740b89 commit 9ec2f32
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
12 changes: 3 additions & 9 deletions examples/editor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, EnterAlternateScreen,
LeaveAlternateScreen,
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use ratatui::backend::CrosstermBackend;
use ratatui::layout::{Constraint, Direction, Layout};
Expand Down Expand Up @@ -157,10 +156,8 @@ impl<'a> Editor<'a> {
return error!("USAGE: cargo run --example editor FILE1 [FILE2...]");
}
let mut stdout = io::stdout();
if !is_raw_mode_enabled()? {
enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
}
enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let term = Terminal::new(backend)?;
Ok(Self {
Expand Down Expand Up @@ -352,9 +349,6 @@ impl<'a> Editor<'a> {
impl<'a> Drop for Editor<'a> {
fn drop(&mut self) {
self.term.show_cursor().unwrap();
if !is_raw_mode_enabled().unwrap() {
return;
}
disable_raw_mode().unwrap();
crossterm::execute!(
self.term.backend_mut(),
Expand Down
12 changes: 3 additions & 9 deletions examples/tuirs_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crossterm_025 as crossterm;

use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, EnterAlternateScreen,
LeaveAlternateScreen,
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use std::borrow::Cow;
use std::env;
Expand Down Expand Up @@ -159,10 +158,8 @@ impl<'a> Editor<'a> {
return error!("USAGE: cargo run --example editor FILE1 [FILE2...]");
}
let mut stdout = io::stdout();
if !is_raw_mode_enabled()? {
enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
}
enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let term = Terminal::new(backend)?;
Ok(Self {
Expand Down Expand Up @@ -354,9 +351,6 @@ impl<'a> Editor<'a> {
impl<'a> Drop for Editor<'a> {
fn drop(&mut self) {
self.term.show_cursor().unwrap();
if !is_raw_mode_enabled().unwrap() {
return;
}
disable_raw_mode().unwrap();
crossterm::execute!(
self.term.backend_mut(),
Expand Down

0 comments on commit 9ec2f32

Please sign in to comment.