From 8fccfd25c0761590a61641b225cc1bdf62ba7779 Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Tue, 25 Mar 2025 10:36:24 +0200 Subject: [PATCH] feat: add the sleep command --- coreutils/src/commands/mod.rs | 1 + coreutils/src/commands/sleep.rs | 36 +++++++++++++++++++++++++++++++++ src/registry.rs | 1 + 3 files changed, 38 insertions(+) create mode 100644 coreutils/src/commands/sleep.rs diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index 1450631..fac4439 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -16,3 +16,4 @@ command!(r#false::False); command!(test::Test); command!(yes::Yes); command!(pwd::Pwd); +command!(sleep::Sleep); diff --git a/coreutils/src/commands/sleep.rs b/coreutils/src/commands/sleep.rs new file mode 100644 index 0000000..413c545 --- /dev/null +++ b/coreutils/src/commands/sleep.rs @@ -0,0 +1,36 @@ +use boxutils::{args::ArgParser, commands::Command}; + +pub struct Sleep; + +impl Command for Sleep { + fn execute(&self) { + let args = ArgParser::builder().parse_args("sleep"); + let mut sleep_for = 0; + + for arg in args.get_normal_args() { + if arg.chars().last().unwrap().is_numeric() { + sleep_for += arg.parse::().unwrap(); + } else { + let multiplier = match arg.chars().last().unwrap() { + 's' => 1, + 'm' => 60, + 'h' => 3600, + 'd' => 86400, + _ => { + println!("Invalid time interval '{}'", arg); + return; + } + }; + + sleep_for += arg[..arg.len() - 1].parse::().unwrap() * multiplier; + } + } + + if sleep_for == 0 { + println!("Usage: sleep [N]..."); + return; + } + + std::thread::sleep(std::time::Duration::from_secs(sleep_for as u64)); + } +} diff --git a/src/registry.rs b/src/registry.rs index bea03a2..a3ae5e4 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -26,6 +26,7 @@ pub fn get_registry() -> CommandRegistry { "[" => coreutils::commands::Test::with_bracket(), "yes" => coreutils::commands::Yes, "pwd" => coreutils::commands::Pwd, + "sleep" => coreutils::commands::Sleep, "box" => Boxcmd });