diff --git a/.gitignore b/.gitignore index ea8c4bf7f..6b39d31a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5516d977e..f22b92ce0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,9 +82,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.1.1" +version = "4.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ec7a4128863c188deefe750ac1d1dfe66c236909f845af04beed823638dc1b2" +checksum = "f13b9c79b5d1dd500d20ef541215a6423c75829ef43117e1b4d17fd8af0b5d76" dependencies = [ "bitflags", "clap_derive", @@ -186,9 +186,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579" +checksum = "322296e2f2e5af4270b54df9e85a02ff037e271af20ba3e7fe1575515dc840b8" dependencies = [ "cc", "cxxbridge-flags", @@ -198,9 +198,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70" +checksum = "017a1385b05d631e7875b1f151c9f012d37b53491e2a87f65bff5c262b2111d8" dependencies = [ "cc", "codespan-reporting", @@ -213,15 +213,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c" +checksum = "c26bbb078acf09bc1ecda02d4223f03bdd28bd4874edcb0379138efc499ce971" [[package]] name = "cxxbridge-macro" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5" +checksum = "357f40d1f06a24b60ae1fe122542c1fb05d28d32acb2aed064e84bc2ad1e252e" dependencies = [ "proc-macro2", "quote", @@ -242,9 +242,9 @@ dependencies = [ [[package]] name = "either" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" [[package]] name = "errno" @@ -430,9 +430,9 @@ checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] name = "libgit2-sys" -version = "0.14.1+1.5.0" +version = "0.14.2+1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a07fb2692bc3593bda59de45a502bb3071659f2c515e28c71e728306b038e17" +checksum = "7f3d95f6b51075fe9810a7ae22c7095f12b98005ab364d8544797a825ce946a4" dependencies = [ "cc", "libc", @@ -679,9 +679,9 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "rustix" -version = "0.36.6" +version = "0.36.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" +checksum = "d4fdebc4b395b7fbb9ab11e462e20ed9051e7b16e42d24042c776eca0ac81b03" dependencies = [ "bitflags", "errno", @@ -923,9 +923,9 @@ dependencies = [ [[package]] name = "unicode-bidi" -version = "0.3.8" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58" [[package]] name = "unicode-ident" diff --git a/Cargo.toml b/Cargo.toml index 0ffb00e4d..71146e25c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,5 @@ members = [ ] [profile.release] -# From https://github.com/johnthagen/min-sized-rust lto = true -# Optimize for size -opt-level = "z" codegen-units = 1 -strip = "debuginfo" diff --git a/gping/src/main.rs b/gping/src/main.rs index f3ad1538a..f645423b0 100644 --- a/gping/src/main.rs +++ b/gping/src/main.rs @@ -2,7 +2,7 @@ use crate::plot_data::PlotData; use anyhow::{anyhow, Result}; use chrono::prelude::*; use clap::Parser; -use crossterm::event::{KeyEvent, KeyModifiers}; +use crossterm::event::KeyModifiers; use crossterm::{ event::{self, Event as CEvent, KeyCode}, execute, @@ -11,6 +11,7 @@ use crossterm::{ use dns_lookup::lookup_host; use pinger::{ping_with_interval, PingResult}; use std::io; +use std::io::BufWriter; use std::iter; use std::net::IpAddr; use std::ops::Add; @@ -19,7 +20,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::Sender; use std::sync::{mpsc, Arc}; use std::thread; -use std::thread::JoinHandle; +use std::thread::{sleep, JoinHandle}; use std::time::{Duration, Instant}; use tui::backend::{Backend, CrosstermBackend}; use tui::layout::{Constraint, Direction, Layout}; @@ -224,7 +225,21 @@ impl From for Update { #[derive(Debug)] enum Event { Update(usize, Update), - Input(KeyEvent), + Terminate, + Render, +} + +fn start_render_thread( + kill_event: Arc, + cmd_tx: Sender, +) -> JoinHandle> { + thread::spawn(move || { + while !kill_event.load(Ordering::Acquire) { + sleep(Duration::from_millis(250)); + cmd_tx.send(Event::Render)?; + } + Ok(()) + }) } fn start_cmd_thread( @@ -260,7 +275,7 @@ fn start_cmd_thread( Update::Timeout }; cmd_tx.send(Event::Update(host_id, update))?; - thread::sleep(interval); + sleep(interval); } Ok(()) }) @@ -358,7 +373,7 @@ fn main() -> Result<()> { let mut threads = vec![]; - let killed = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let killed = Arc::new(AtomicBool::new(false)); for (host_id, host_or_cmd) in hosts_or_commands.iter().cloned().enumerate() { if args.cmd { @@ -381,11 +396,15 @@ fn main() -> Result<()> { )?); } } + threads.push(start_render_thread( + std::sync::Arc::clone(&killed), + key_tx.clone(), + )); let mut app = App::new(data, args.buffer); enable_raw_mode()?; let stdout = io::stdout(); - let mut backend = CrosstermBackend::new(stdout); + let mut backend = CrosstermBackend::new(BufWriter::with_capacity(1024 * 1024 * 4, stdout)); let rect = backend.size()?; execute!(backend, SetSize(rect.width, rect.height),)?; @@ -394,17 +413,26 @@ fn main() -> Result<()> { // Pump keyboard messages into the queue let killed_thread = std::sync::Arc::clone(&killed); - let key_thread = thread::spawn(move || -> Result<()> { + thread::spawn(move || -> Result<()> { while !killed_thread.load(Ordering::Acquire) { - if event::poll(Duration::from_millis(100))? { + if event::poll(Duration::from_secs(5))? { if let CEvent::Key(key) = event::read()? { - key_tx.send(Event::Input(key))?; + match key.code { + KeyCode::Char('q') | KeyCode::Esc => { + key_tx.send(Event::Terminate)?; + break; + } + KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => { + key_tx.send(Event::Terminate)?; + break; + } + _ => {} + } } } } Ok(()) }); - threads.push(key_thread); loop { match rx.recv()? { @@ -421,8 +449,9 @@ fn main() -> Result<()> { break; } }; + } + Event::Render => { terminal.draw(|f| { - // Split our let chunks = Layout::default() .direction(Direction::Vertical) .vertical_margin(args.vertical_margin) @@ -438,8 +467,8 @@ fn main() -> Result<()> { let total_chunks = chunks.len(); - let header_chunks = chunks[0..total_chunks - 1].to_owned(); - let chart_chunk = chunks[total_chunks - 1].to_owned(); + let header_chunks = &chunks[0..total_chunks - 1]; + let chart_chunk = &chunks[total_chunks - 1]; for (plot_data, chunk) in app.data.iter().zip(header_chunks) { let header_layout = Layout::default() @@ -457,7 +486,7 @@ fn main() -> Result<()> { ] .as_ref(), ) - .split(chunk); + .split(*chunk); for (area, paragraph) in header_layout.into_iter().zip(plot_data.header_stats()) @@ -486,20 +515,13 @@ fn main() -> Result<()> { .labels(app.y_axis_labels(y_axis_bounds)), ); - f.render_widget(chart, chart_chunk) + f.render_widget(chart, *chart_chunk) })?; } - Event::Input(input) => match input.code { - KeyCode::Char('q') | KeyCode::Esc => { - killed.store(true, Ordering::Release); - break; - } - KeyCode::Char('c') if input.modifiers == KeyModifiers::CONTROL => { - killed.store(true, Ordering::Release); - break; - } - _ => {} - }, + Event::Terminate => { + killed.store(true, Ordering::Release); + break; + } } } killed.store(true, Ordering::Relaxed);