Skip to content

Commit

Permalink
Merge pull request #705 from hatoo/use-ratatui-fn
Browse files Browse the repository at this point in the history
use raratui::{init, restore}
  • Loading branch information
hatoo authored Feb 22, 2025
2 parents 5a26fe1 + d0fc5e2 commit 07f489f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
12 changes: 0 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,18 +621,6 @@ pub async fn run(mut opts: Opts) -> anyhow::Result<()> {
}
};

// When panics, reset terminal mode and exit immediately.
std::panic::set_hook(Box::new(move |info| {
if !no_tui {
use crossterm::ExecutableCommand;
let _ = std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen);
let _ = crossterm::terminal::disable_raw_mode();
let _ = std::io::stdout().execute(crossterm::cursor::Show);
}
eprintln!("{info}");
std::process::exit(libc::EXIT_FAILURE);
}));

let start = std::time::Instant::now();

let data_collect_future: Pin<Box<dyn std::future::Future<Output = (ResultData, PrintConfig)>>> =
Expand Down
31 changes: 9 additions & 22 deletions src/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use byte_unit::Byte;
use crossterm::{
ExecutableCommand,
event::{Event, KeyCode, KeyEvent, KeyModifiers},
};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use hyper::http;
use ratatui::crossterm;
use ratatui::{DefaultTerminal, crossterm};
use ratatui::{
Terminal,
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
style::{Color, Style},
text::{Line, Span},
widgets::{BarChart, Block, Borders, Gauge, Paragraph},
};
use std::{collections::BTreeMap, io};
use std::collections::BTreeMap;

use crate::{
client::{ClientError, RequestResult},
Expand Down Expand Up @@ -67,30 +62,21 @@ pub struct Monitor {
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)
pub fn new() -> Result<(Self, DefaultTerminal), std::io::Error> {
let terminal = ratatui::try_init()?;
Ok((Self, terminal))
}
}

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);
ratatui::restore();
}
}

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

let mut terminal = {
let backend = CrosstermBackend::new(io::stdout());
Terminal::new(backend)?
};
let (raw_mode, mut terminal) = IntoRawMode::new()?;

// Return this when ends to application print summary
// We must not read all data from this due to computational cost.
Expand Down Expand Up @@ -431,6 +417,7 @@ impl Monitor {
modifiers: KeyModifiers::CONTROL,
..
}) => {
drop(terminal);
drop(raw_mode);
let _ = crate::printer::print_result(
self.print_config,
Expand Down

0 comments on commit 07f489f

Please sign in to comment.