shitbox/corebox/commands/sleep/mod.rs

44 lines
1.5 KiB
Rust
Raw Normal View History

2022-12-25 18:29:09 -05:00
use super::Cmd;
2022-12-20 18:35:45 -05:00
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
2022-12-25 18:29:09 -05:00
use std::{env, error::Error, thread, time::Duration};
2022-12-20 12:05:21 -05:00
#[derive(Debug, Default)]
pub struct Sleep;
2022-12-20 18:35:45 -05:00
2022-12-25 18:29:09 -05:00
impl Cmd for Sleep {
fn cli(&self) -> clap::Command {
Command::new("sleep")
2022-12-25 18:29:09 -05:00
.about("Suspend execution for an interval of time")
.long_about(
"The sleep utility suspends execution for a minimum of the specified number of seconds.\n\
This number must be positive and may contain a decimal fraction.\n\
sleep is commonly used to schedule the execution of other commands"
2022-12-20 18:35:45 -05:00
)
2022-12-25 18:29:09 -05:00
.author(env!("CARGO_PKG_AUTHORS"))
.arg(
Arg::new("seconds")
.help("The number of seconds to sleep")
.num_args(1)
.allow_negative_numbers(false)
.value_parser(value_parser!(f64))
.required(true)
.action(ArgAction::Set)
)
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
if let Some(raw) = matches.get_one::<f64>("seconds") {
2022-12-25 18:29:09 -05:00
let seconds = *raw as u64;
let nanos = ((raw % 1.0) * 10e-9) as u32;
let s = Duration::new(seconds, nanos);
thread::sleep(s);
}
Ok(())
}
2022-12-20 18:35:45 -05:00
fn path(&self) -> Option<shitbox::Path> {
Some(shitbox::Path::Bin)
2022-12-20 18:35:45 -05:00
}
}