Make printing a bit neater

This commit is contained in:
Sofia 2020-08-24 02:40:05 +03:00
parent 89460011eb
commit 18cddbab28
2 changed files with 15 additions and 13 deletions

View File

@ -67,18 +67,14 @@ impl Timestamped for LocationModel {
} }
pub struct API { pub struct API {
config: Config, pub config: Config,
pub last_response_ping: u32,
location_req_que: Vec<String>, location_req_que: Vec<String>,
} }
impl API { impl API {
pub fn new(config: Config) -> API { pub fn new(config: Config) -> API {
API { API {
last_response_ping: config.throttle,
config: config, config: config,
location_req_que: Vec::new(), location_req_que: Vec::new(),
} }
} }

View File

@ -11,6 +11,7 @@ use errors::{GenericError, MessagedError};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::sync::mpsc::TryRecvError; use std::sync::mpsc::TryRecvError;
use std::time::Duration;
fn main() { fn main() {
let env: EnvOpt = argh::from_env(); let env: EnvOpt = argh::from_env();
@ -72,13 +73,16 @@ fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(),
Ok(res) => match res { Ok(res) => match res {
Ok(loc) => { Ok(loc) => {
counter += 1; counter += 1;
println!( let remaining = exp_time(&api, states.len() as u64 - counter as u64);
"Currently: {:#.2}%", print!(
counter as f32 / states.len() as f32 * 100. "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 { match loc {
Ok(mut loc) => locations.append(&mut loc), Ok(mut loc) => locations.append(&mut loc),
Err(e) => println!( Err(e) => eprintln!(
"Error fetching location data: {}", "Error fetching location data: {}",
e.message.unwrap_or(String::new()) e.message.unwrap_or(String::new())
), ),
@ -93,14 +97,15 @@ fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(),
} }
} }
} }
println!("{:<100}", "Done!");
locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp)); locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp));
let locs = API::get_between(&locations, from, to, false, &config); let locs = API::get_between(&locations, from, to, false, &config);
dbg!( /*dbg!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(), &locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(), locs.len(),
locations.len() locations.len()
); );*/
Ok(()) Ok(())
} }
@ -115,6 +120,7 @@ fn get_opt<A, B>(option: Option<Result<A, B>>) -> Result<Option<A>, B> {
} }
} }
fn exp_time(api: &API, reqs_left: u32) -> f32 { fn exp_time(api: &API, reqs_left: u64) -> Duration {
(reqs_left * api.last_response_ping) as f32 / 1000. let interval = 1_000 / api.config.throttle as u64;
Duration::from_millis(interval * reqs_left)
} }