Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor monitor's raw mode thing #526

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,28 @@ pub struct Monitor {
pub stats_success_breakdown: bool,
}

impl Monitor {
pub async fn monitor(self) -> Result<ResultData, std::io::Error> {
struct IntoRawMode;

impl IntoRawMode {
pub fn new() -> Result<Self, std::io::Error> {
crossterm::terminal::enable_raw_mode()?;
io::stdout().execute(crossterm::terminal::EnterAlternateScreen)?;
io::stdout().execute(crossterm::cursor::Hide)?;
Ok(IntoRawMode)
}
}

impl Drop for IntoRawMode {
fn drop(&mut self) {
let _ = crossterm::terminal::disable_raw_mode();
let _ = io::stdout().execute(crossterm::terminal::LeaveAlternateScreen);
let _ = io::stdout().execute(crossterm::cursor::Show);
}
}

impl Monitor {
pub async fn monitor(self) -> Result<ResultData, std::io::Error> {
let raw_mode = IntoRawMode::new()?;

let mut terminal = {
let backend = CrosstermBackend::new(io::stdout());
Expand Down Expand Up @@ -414,9 +431,6 @@ impl Monitor {
modifiers: KeyModifiers::CONTROL,
..
}) => {
std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?;
crossterm::terminal::disable_raw_mode()?;
std::io::stdout().execute(crossterm::cursor::Show)?;
let _ = crate::printer::print_result(
&mut std::io::stdout(),
self.print_mode,
Expand All @@ -426,6 +440,7 @@ impl Monitor {
self.disable_color,
self.stats_success_breakdown,
);
drop(raw_mode);
std::process::exit(libc::EXIT_SUCCESS);
}
_ => (),
Expand All @@ -438,10 +453,6 @@ impl Monitor {
tokio::time::sleep(per_frame - elapsed).await;
}
}

std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?;
crossterm::terminal::disable_raw_mode()?;
std::io::stdout().execute(crossterm::cursor::Show)?;
Ok(all)
}
}