diff --git a/src/main.rs b/src/main.rs index 95d0a26..108c719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,10 @@ mod util; use {cfg::*, err::*, util::*}; fn main() -> Result<()> { - let mut client = BaseClient::setup()?; + let mut client = dbg("Initializing client", || BaseClient::setup())?; loop { - match routine(&mut client) { + match dbg("Doing routine", || routine(&mut client)) { Ok(patch) => { if let Some((old, new)) = patch { println!("PATCH: {} => {}", old, new) @@ -17,6 +17,6 @@ fn main() -> Result<()> { } Err(e) => eprintln!("{}", e), } - delay() + dbg("Delaying", || Ok(client.delay()))?; } } diff --git a/src/util.rs b/src/util.rs index 3ce7ad4..a4ff8b2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,10 +1,11 @@ use { crate::{BaseClient, Result}, - std::{net::IpAddr, thread::sleep, time::Duration}, + std::{ + io::{stdout, Write}, + net::IpAddr, + }, }; -pub const DELAY: Duration = Duration::from_secs(300); - pub fn routine(client: &mut BaseClient) -> Result> { let current = client.public_ip()?; let record = client.get()?; @@ -17,6 +18,18 @@ pub fn routine(client: &mut BaseClient) -> Result> { }) } -pub fn delay() { - sleep(DELAY) +pub fn dbg Result>(msg: &'static str, f: F) -> Result { + let mut stdout = stdout(); + stdout.write_fmt(format_args!("{}...", msg))?; + stdout.flush()?; + + let result = f(); + + stdout.write_fmt(format_args!( + " {}\n", + if result.is_ok() { "Done" } else { "Failed" } + ))?; + + stdout.flush()?; + result }