Skip to content

Commit

Permalink
added time estimate to SMM
Browse files Browse the repository at this point in the history
  • Loading branch information
epi052 committed Jun 15, 2024
1 parent d2e917d commit b3cf6b5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/event_handlers/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::time::Duration;

use reqwest::StatusCode;
use tokio::sync::oneshot::Sender;
Expand Down Expand Up @@ -88,4 +89,7 @@ pub enum Command {

/// inform the Stats object about which targets are being scanned
UpdateTargets(Vec<String>),

/// query the Stats handler about the position of the overall progress bar
QueryOverallBarEta(Sender<Duration>),
}
3 changes: 3 additions & 0 deletions src/event_handlers/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ impl StatsHandler {
Command::Sync(sender) => {
sender.send(true).unwrap_or_default();
}
Command::QueryOverallBarEta(sender) => {
sender.send(self.bar.eta()).unwrap_or_default();
}
Command::UpdateTargets(targets) => {
self.stats.update_targets(targets);
}
Expand Down
4 changes: 1 addition & 3 deletions src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::time::Duration;

use indicatif::{HumanDuration, MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use lazy_static::lazy_static;

lazy_static! {
Expand Down
17 changes: 15 additions & 2 deletions src/scan_manager/menu.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::time::Duration;

use crate::filters::filter_lookup;
use crate::progress::PROGRESS_BAR;
use crate::traits::FeroxFilter;
use console::{measure_text_width, pad_str, style, Alignment, Term};
use indicatif::ProgressDrawTarget;
use indicatif::{HumanDuration, ProgressDrawTarget};
use regex::Regex;

/// Data container for a command entered by the user interactively
Expand Down Expand Up @@ -43,6 +45,9 @@ pub(super) struct Menu {
/// footer: instructions surrounded by separators
footer: String,

/// length of longest displayed line (suitable for ascii/unicode)
longest: usize,

/// unicode line border, matched to longest displayed line
border: String,

Expand Down Expand Up @@ -110,7 +115,7 @@ impl Menu {
commands.push_str(&valid_filters);
commands.push_str(&rm_filter_cmd);

let longest = measure_text_width(&canx_cmd).max(measure_text_width(&name));
let longest = measure_text_width(&canx_cmd).max(measure_text_width(&name)) + 1;

let border = separator.repeat(longest);

Expand All @@ -123,6 +128,7 @@ impl Menu {
header,
footer,
border,
longest,
term: Term::stderr(),
}
}
Expand All @@ -142,6 +148,13 @@ impl Menu {
self.println(&self.footer);
}

/// print menu footer
pub(super) fn print_eta(&self, eta: Duration) {
let inner = format!("⏳ {} remaining ⏳", HumanDuration(eta));
let padded_eta = pad_str(&inner, self.longest, Alignment::Center, None);
self.println(&format!("{padded_eta}\n{}", self.border));
}

/// set PROGRESS_BAR bar target to hidden
pub(super) fn hide_progress_bars(&self) {
PROGRESS_BAR.set_draw_target(ProgressDrawTarget::hidden());
Expand Down
8 changes: 8 additions & 0 deletions src/scan_manager/scan_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::{
},
thread::sleep,
};
use tokio::sync::oneshot;
use tokio::time::{self, Duration};

/// Single atomic number that gets incremented once, used to track first thread to interact with
Expand Down Expand Up @@ -430,6 +431,13 @@ impl FeroxScans {
self.menu.hide_progress_bars();
self.menu.clear_screen();
self.menu.print_header();
let (tx, rx) = oneshot::channel::<Duration>();
if handles.stats.send(Command::QueryOverallBarEta(tx)).is_ok() {
if let Ok(y) = rx.await {
self.menu.print_eta(y);
}
}

self.display_scans().await;
self.display_filters(handles.clone());
self.menu.print_footer();
Expand Down

0 comments on commit b3cf6b5

Please sign in to comment.