Skip to content

Commit

Permalink
FIX update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Helsley committed Jun 27, 2023
1 parent 8ba9dfb commit b8a2f80
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
10 changes: 6 additions & 4 deletions examples/cwd.rs
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 4 additions & 0 deletions examples/hyperlink.rs
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 8 additions & 3 deletions examples/icon.rs
Original file line number Diff line number Diff line change
@@ -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);
}
40 changes: 40 additions & 0 deletions examples/may_sleep/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub fn parse_cmd_args() -> Option<u16> {
let mut sleep_ms: Option<u16> = 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::<u16>()
.ok()
.and_then(|parsed| {
skip_next = true;
Some(parsed)
});
}
_ => {}
}
}

sleep_ms
}

pub fn sleep(sleep_ms: Option<u16>) {
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(())
}*/
11 changes: 8 additions & 3 deletions examples/title.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b8a2f80

Please sign in to comment.