Skip to content

Commit

Permalink
Beautiful status line with indicatif
Browse files Browse the repository at this point in the history
  • Loading branch information
mersinvald committed Apr 26, 2017
1 parent 0d0c08e commit 499d1ca
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 30 deletions.
76 changes: 75 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "batch_resolve_cli"
description = "Fast asynchronous batch DNS resolver built on top of Tokio and TRust-DNS"
version = "0.3.1"
version = "0.3.2"
authors = ["Mike Lubinets <[email protected]>"]
homepage = "http://github.com/mersinvald/batch_resolve"
repository = "http://github.com/mersinvald/batch_resolve"
Expand All @@ -26,6 +26,7 @@ serde_derive = "0.9"
toml = "0.3"
crossbeam = "0.2"
num_cpus = "1.3.0"
indicatif = "0.1.0"

[dependencies.trust-dns]
version = "0.10"
Expand Down
55 changes: 27 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate trust_dns;
extern crate tokio_core;
extern crate crossbeam;
extern crate num_cpus;
extern crate indicatif;

#[macro_use]
mod macros;
Expand All @@ -24,19 +25,19 @@ use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::io::{self, Read, Write};
use std::fs::File;
use std::sync::mpsc;
use std::thread;
use std::time::{Instant, Duration};
use std::time::Duration;

use std::sync::{Arc, Mutex};
use std::env;
use std::io::stdout;

use clap::{Arg, App};

use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;

use indicatif::{ProgressBar, ProgressStyle};

fn process_args() -> (Vec<String>, Vec<String>, Vec<QueryType>) {
let app = App::new("Batch Resolve")
.about("Fast asynchronous DNS batch resolver")
Expand Down Expand Up @@ -196,36 +197,34 @@ fn main() {
}

// Create status output thread and register status callback
let (status_tx, status_rx) = mpsc::channel::<Status>();

let status = Arc::new(Mutex::new(Status::default()));
let status_clone = status.clone();

thread::spawn(move || {
let pb = ProgressBar::new(overall_count as u64);
pb.set_style(ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed}] [{wide_bar:.cyan/blue}] {pos}/{len} ({eta}) | {msg} {spinner:.green}")
.progress_chars("#>-"));

// Print every 100ms
let mut instant = Instant::now();
for status in status_rx.iter() {
if instant.elapsed() > Duration::from_millis(100) {
let running = format!("{:6} running", status.running);
let done = format!("{:6}/{} done", status.done, overall_count);
let success = format!("{:6}/{} succeded", status.success, overall_count);
let fail = format!("{:6}/{} failed", status.fail, overall_count);
let error = format!("{:6} errored", status.errored);

print!("{} {} {} {} {}\r",
running,
done,
success,
fail,
error
);

stdout().flush().unwrap();

instant = Instant::now();
}
let mut status;

while {
status = status_clone.lock().unwrap().clone();
status.done < overall_count as u64
} {
let message = format!("{} running | {} failed", status.running, status.fail);
pb.set_position(status.done);
pb.set_message(&message);
thread::sleep(Duration::from_millis(30));
}

pb.finish_with_message("done");
});

batch.register_status_callback(Box::new(move |status: Status| {
status_tx.send(status).unwrap();

batch.register_status_callback(Box::new(move |s: Status| {
*status.lock().unwrap() = s;
}));

// Execute batch job
Expand Down

0 comments on commit 499d1ca

Please sign in to comment.