diff options
| author | ache <ache@ache.one> | 2026-08-04 18:27:58 +0200 |
|---|---|---|
| committer | ache <ache@ache.one> | 2026-08-04 18:27:58 +0200 |
| commit | b6ce2ea9948cee959e31013221e483eceb9b4a47 (patch) | |
| tree | 727c6df94b340385664372e8cf8e8e80f62b459b /src/main.rs | |
| parent | Remove dead code (diff) | |
Linting and format
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs index 5f4f1e0..d5e1588 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod state; mod stats; use args::Args; use ip::parse_ip; -use pinger::{spawn_ping, PingResult}; +use pinger::{PingResult, spawn_ping}; use print::{print_legende_line, print_ping_line, print_stats}; use state::State; use stats::compute_stats; @@ -36,31 +36,36 @@ async fn main() { let mut states: Vec<State> = Vec::new(); let mut packet_id: u32 = 0; - print_legende_line(); // Save the current cursor position - if !std::process::Command::new("tput").arg("sc").status().map(|s| s.success()).unwrap_or(false) { + if !std::process::Command::new("tput") + .arg("sc") + .status() + .map(|s| s.success()) + .unwrap_or(false) + { println!("Process failed"); std::process::exit(1); } - // Should be quit with Ctrl + C loop { - let current_time = State::current_time_ms(); for state in states.iter_mut() { if let State::NotYetReceived { time_sent } = state { // TODO: 2000ms Should be a configurable variable - if current_time - *time_sent > 2000 { // 2s = 2000ms + if current_time - *time_sent > 2000 { + // 2s = 2000ms *state = State::Lost; } } } // Send the ping - states.push(State::NotYetReceived { time_sent: current_time }); + states.push(State::NotYetReceived { + time_sent: current_time, + }); spawn_ping(target, tx.clone(), packet_id); packet_id += 1; @@ -68,18 +73,16 @@ async fn main() { loop { match rx.try_recv() { // Update the state of the received packet - Ok(result) => { - match result { - PingResult::Ping { rtt, ttl, id } => { - if (id as usize) < states.len() { - states[id as usize] = State::Received { rtt: rtt, ttl }; - } - } - PingResult::NoPingErr { id } => { - states[id as usize] = State::SendError; + Ok(result) => match result { + PingResult::Ping { rtt, ttl, id } => { + if (id as usize) < states.len() { + states[id as usize] = State::Received { rtt, ttl }; } } - } + PingResult::NoPingErr { id } => { + states[id as usize] = State::SendError; + } + }, Err(TryRecvError::Disconnected) => { eprintln!("Critical error, pinger thread killed"); std::process::exit(2); @@ -92,7 +95,12 @@ async fn main() { } // Put the cursor back up - if !std::process::Command::new("tput").arg("rc").status().map(|s| s.success()).unwrap_or(false) { + if !std::process::Command::new("tput") + .arg("rc") + .status() + .map(|s| s.success()) + .unwrap_or(false) + { println!("Process failed too"); std::process::exit(1); } @@ -116,7 +124,6 @@ async fn main() { } } - // Sleep for 1 second before the next ping // FEAT: Should be configurable via CLI sleep(tokio::time::Duration::from_millis(1000)).await; |