feat: add the sleep command

This commit is contained in:
Teesh 2025-03-25 10:36:24 +02:00
parent f362a01e0c
commit 8fccfd25c0
3 changed files with 38 additions and 0 deletions

View file

@ -16,3 +16,4 @@ command!(r#false::False);
command!(test::Test);
command!(yes::Yes);
command!(pwd::Pwd);
command!(sleep::Sleep);

View file

@ -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::<i32>().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::<i32>().unwrap() * multiplier;
}
}
if sleep_for == 0 {
println!("Usage: sleep [N]...");
return;
}
std::thread::sleep(std::time::Duration::from_secs(sleep_for as u64));
}
}

View file

@ -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
});