diff --git a/examples/cwd.rs b/examples/cwd.rs index d7cc720..823b0fb 100644 --- a/examples/cwd.rs +++ b/examples/cwd.rs @@ -1,18 +1,20 @@ use nu_ansi_term::AnsiGenericString; +mod may_sleep; +use may_sleep::{parse_cmd_args, sleep}; fn main() { #[cfg(windows)] nu_ansi_term::enable_ansi_support().unwrap(); + let sleep_ms = parse_cmd_args(); let path = std::path::Path::new("./for_cwd/").to_str().unwrap(); let cwd = AnsiGenericString::cwd(path); println!( - "{}Terminal working directory set for the next 5 seconds.", - cwd + "{}Terminal working directory set for the next {:?} milliseconds", + cwd, sleep_ms ); // sleep because often prompts change this before you can see // the results - let delay_ms = std::time::Duration::from_millis(5000); - std::thread::sleep(delay_ms); + sleep(sleep_ms); } diff --git a/examples/hyperlink.rs b/examples/hyperlink.rs index b48fc21..3c17b76 100644 --- a/examples/hyperlink.rs +++ b/examples/hyperlink.rs @@ -1,11 +1,15 @@ use nu_ansi_term::Color; +mod may_sleep; +use may_sleep::{parse_cmd_args, sleep}; fn main() { #[cfg(windows)] nu_ansi_term::enable_ansi_support().unwrap(); + let sleep_ms = parse_cmd_args(); let mut link = Color::Blue.underline().paint("Link to example.com"); link.hyperlink("https://example.com"); println!("{}", link); + sleep(sleep_ms); } diff --git a/examples/icon.rs b/examples/icon.rs index 526aa2d..6d9338e 100644 --- a/examples/icon.rs +++ b/examples/icon.rs @@ -1,16 +1,21 @@ use nu_ansi_term::AnsiGenericString; +mod may_sleep; +use may_sleep::{parse_cmd_args, sleep}; fn main() { #[cfg(windows)] nu_ansi_term::enable_ansi_support().unwrap(); + let sleep_ms = parse_cmd_args(); // TODO add foo.icn foo.bmp or whatever is most portable let path = std::path::Path::new("./foo.icn").to_str().unwrap(); let icon = AnsiGenericString::icon(path); - println!("{}Terminal icon title set for the next 5 seconds.", icon); + println!( + "{}Terminal icon title set for the next {:?} milliseconds", + icon, sleep_ms + ); // sleep because often prompts change this before you can see // the results - let delay_ms = std::time::Duration::from_millis(5000); - std::thread::sleep(delay_ms); + sleep(sleep_ms); } diff --git a/examples/may_sleep/mod.rs b/examples/may_sleep/mod.rs new file mode 100644 index 0000000..f0155c8 --- /dev/null +++ b/examples/may_sleep/mod.rs @@ -0,0 +1,40 @@ +pub fn parse_cmd_args() -> Option { + let mut sleep_ms: Option = None; + let mut skip_next = false; + + for (i, arg) in std::env::args().skip(1).enumerate() { + if skip_next { + skip_next = false; + continue; + } + + match &arg[..] { + "-s" | "--sleep" => { + sleep_ms = std::env::args() + .nth(i + 2) // next is +2 because .skip(1) + .unwrap_or(String::from("5000u16")) + .parse::() + .ok() + .and_then(|parsed| { + skip_next = true; + Some(parsed) + }); + } + _ => {} + } + } + + sleep_ms +} + +pub fn sleep(sleep_ms: Option) { + if let Some(sleep_ms) = sleep_ms { + let sleep_ms = std::time::Duration::from_millis(sleep_ms as u64); + std::thread::sleep(sleep_ms); + } +} + +/*#[allow(unused)] +fn main() -> Result<(),()> { + Err(()) +}*/ diff --git a/examples/title.rs b/examples/title.rs index 101c5a8..c043d87 100644 --- a/examples/title.rs +++ b/examples/title.rs @@ -1,14 +1,19 @@ use nu_ansi_term::AnsiGenericString; +mod may_sleep; +use may_sleep::{parse_cmd_args, sleep}; fn main() { #[cfg(windows)] nu_ansi_term::enable_ansi_support().unwrap(); + let sleep_ms = parse_cmd_args(); let title = AnsiGenericString::title("My Title"); - println!("{}Terminal title set for the next 5 seconds.", title); + println!( + "{}Terminal title set for the next {:?} milliseconds", + title, sleep_ms + ); // sleep because often prompts change this before you can see // the results - let delay_ms = std::time::Duration::from_millis(5000); - std::thread::sleep(delay_ms); + sleep(sleep_ms); }