diff --git a/Cargo.lock b/Cargo.lock index 104bb30..647a8cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,6 +172,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi 0.2.6", + "libc", +] + [[package]] name = "os_str_bytes" version = "6.4.1" @@ -210,6 +220,7 @@ dependencies = [ "data-encoding", "hostname", "libc", + "num_cpus", "termcolor", ] diff --git a/Cargo.toml b/Cargo.toml index 8c672c5..e442f1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ clap_mangen = "0.2.5" data-encoding = "2.3.3" hostname = { version = "0.3", features = ["set"] } libc = "0.2.139" +num_cpus = "1.15.0" termcolor = "1.1.3" [profile.release] diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 3c21862..d708b9b 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -21,6 +21,7 @@ mod ls; pub mod mountpoint; mod mv; pub mod nologin; +pub mod nproc; mod pwd; pub mod rev; mod rm; @@ -34,7 +35,7 @@ pub mod yes; pub use { self::hostname::Hostname, base32::Base32, base64::Base64, bootstrap::Bootstrap, dirname::Dirname, echo::Echo, factor::Factor, head::Head, mountpoint::Mountpoint, - nologin::Nologin, r#false::False, r#true::True, rev::Rev, shitbox::Shitbox, sleep::Sleep, + nologin::Nologin, nproc::Nproc, r#false::False, r#true::True, rev::Rev, shitbox::Shitbox, sleep::Sleep, yes::Yes, }; @@ -57,6 +58,7 @@ pub fn get(name: &str) -> Option> { "head" => Some(Box::new(Head::default())), "mountpoint" => Some(Box::new(Mountpoint::default())), "nologin" => Some(Box::new(Nologin::default())), + "nproc" => Some(Box::new(Nproc::default())), "rev" => Some(Box::new(Rev::default())), "shitbox" => Some(Box::new(Shitbox::default())), "sleep" => Some(Box::new(Sleep::default())), @@ -66,7 +68,7 @@ pub fn get(name: &str) -> Option> { } } -pub static COMMANDS: [&'static str; 16] = [ +pub static COMMANDS: [&'static str; 17] = [ "base32", "base64", "bootstrap", @@ -78,6 +80,7 @@ pub static COMMANDS: [&'static str; 16] = [ "hostname", "mountpoint", "nologin", + "nproc", "rev", "sleep", "shitbox", diff --git a/src/cmd/nproc/mod.rs b/src/cmd/nproc/mod.rs new file mode 100644 index 0000000..a640926 --- /dev/null +++ b/src/cmd/nproc/mod.rs @@ -0,0 +1,53 @@ +use clap::{Arg, Command, ArgAction}; +use std::io; +use super::Cmd; + +#[derive(Debug)] +pub struct Nproc { + name: &'static str, + path: Option, +} + +impl Default for Nproc { + fn default() -> Self { + Self { + name: "nproc", + path: Some(crate::Path::UsrBin), + } + } +} + +impl Cmd for Nproc { + fn name(&self) -> &str { + self.name + } + + fn cli(&self) -> clap::Command { + Command::new(self.name) + .author("Nathan Fisher") + .about("Print the number of processing units available") + .arg( + Arg::new("ALL") + .help("Print the number of installed processors") + .short('a') + .long("all") + .action(ArgAction::SetTrue), + ) + } + + fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + let Some(matches) = matches else { + return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); + }; + if matches.get_flag("ALL") { + println!("{}", num_cpus::get()); + } else { + println!("{}", num_cpus::get_physical()); + } + Ok(()) + } + + fn path(&self) -> Option { + self.path + } +}