From 18cddbab28849e1ad817c67bf69ddf0d7c5f8187 Mon Sep 17 00:00:00 2001 From: Teascade Date: Mon, 24 Aug 2020 02:40:05 +0300 Subject: [PATCH] Make printing a bit neater --- src/api.rs | 6 +----- src/main.rs | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/api.rs b/src/api.rs index 9b6b387..1d84b18 100644 --- a/src/api.rs +++ b/src/api.rs @@ -67,18 +67,14 @@ impl Timestamped for LocationModel { } pub struct API { - config: Config, - pub last_response_ping: u32, - + pub config: Config, location_req_que: Vec, } impl API { pub fn new(config: Config) -> API { API { - last_response_ping: config.throttle, config: config, - location_req_que: Vec::new(), } } diff --git a/src/main.rs b/src/main.rs index 2f6da32..a8820dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use errors::{GenericError, MessagedError}; use std::fs::File; use std::io::prelude::*; use std::sync::mpsc::TryRecvError; +use std::time::Duration; fn main() { let env: EnvOpt = argh::from_env(); @@ -72,13 +73,16 @@ fn run(config: &Config, from: Option, to: Option) -> Result<(), Ok(res) => match res { Ok(loc) => { counter += 1; - println!( - "Currently: {:#.2}%", - counter as f32 / states.len() as f32 * 100. + let remaining = exp_time(&api, states.len() as u64 - counter as u64); + print!( + "Done: {:<5.2}% Remaining: {:<5.2} seconds\r", + counter as f32 / states.len() as f32 * 100., + remaining.as_secs_f32() ); + std::io::stdout().lock().flush().ok(); match loc { Ok(mut loc) => locations.append(&mut loc), - Err(e) => println!( + Err(e) => eprintln!( "Error fetching location data: {}", e.message.unwrap_or(String::new()) ), @@ -93,14 +97,15 @@ fn run(config: &Config, from: Option, to: Option) -> Result<(), } } } + println!("{:<100}", "Done!"); locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp)); let locs = API::get_between(&locations, from, to, false, &config); - dbg!( + /*dbg!( &locs.iter().map(|loc| loc.0).collect::>(), locs.len(), locations.len() - ); + );*/ Ok(()) } @@ -115,6 +120,7 @@ fn get_opt(option: Option>) -> Result, B> { } } -fn exp_time(api: &API, reqs_left: u32) -> f32 { - (reqs_left * api.last_response_ping) as f32 / 1000. +fn exp_time(api: &API, reqs_left: u64) -> Duration { + let interval = 1_000 / api.config.throttle as u64; + Duration::from_millis(interval * reqs_left) }