Add nproc applet

This commit is contained in:
Nathan Fisher 2023-01-07 18:50:18 -05:00
parent 991e19bcbc
commit f70244ea49
4 changed files with 70 additions and 2 deletions

11
Cargo.lock generated
View File

@ -172,6 +172,16 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" 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]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "6.4.1" version = "6.4.1"
@ -210,6 +220,7 @@ dependencies = [
"data-encoding", "data-encoding",
"hostname", "hostname",
"libc", "libc",
"num_cpus",
"termcolor", "termcolor",
] ]

View File

@ -14,6 +14,7 @@ clap_mangen = "0.2.5"
data-encoding = "2.3.3" data-encoding = "2.3.3"
hostname = { version = "0.3", features = ["set"] } hostname = { version = "0.3", features = ["set"] }
libc = "0.2.139" libc = "0.2.139"
num_cpus = "1.15.0"
termcolor = "1.1.3" termcolor = "1.1.3"
[profile.release] [profile.release]

View File

@ -21,6 +21,7 @@ mod ls;
pub mod mountpoint; pub mod mountpoint;
mod mv; mod mv;
pub mod nologin; pub mod nologin;
pub mod nproc;
mod pwd; mod pwd;
pub mod rev; pub mod rev;
mod rm; mod rm;
@ -34,7 +35,7 @@ pub mod yes;
pub use { pub use {
self::hostname::Hostname, base32::Base32, base64::Base64, bootstrap::Bootstrap, self::hostname::Hostname, base32::Base32, base64::Base64, bootstrap::Bootstrap,
dirname::Dirname, echo::Echo, factor::Factor, head::Head, mountpoint::Mountpoint, 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, yes::Yes,
}; };
@ -57,6 +58,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"head" => Some(Box::new(Head::default())), "head" => Some(Box::new(Head::default())),
"mountpoint" => Some(Box::new(Mountpoint::default())), "mountpoint" => Some(Box::new(Mountpoint::default())),
"nologin" => Some(Box::new(Nologin::default())), "nologin" => Some(Box::new(Nologin::default())),
"nproc" => Some(Box::new(Nproc::default())),
"rev" => Some(Box::new(Rev::default())), "rev" => Some(Box::new(Rev::default())),
"shitbox" => Some(Box::new(Shitbox::default())), "shitbox" => Some(Box::new(Shitbox::default())),
"sleep" => Some(Box::new(Sleep::default())), "sleep" => Some(Box::new(Sleep::default())),
@ -66,7 +68,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
} }
} }
pub static COMMANDS: [&'static str; 16] = [ pub static COMMANDS: [&'static str; 17] = [
"base32", "base32",
"base64", "base64",
"bootstrap", "bootstrap",
@ -78,6 +80,7 @@ pub static COMMANDS: [&'static str; 16] = [
"hostname", "hostname",
"mountpoint", "mountpoint",
"nologin", "nologin",
"nproc",
"rev", "rev",
"sleep", "sleep",
"shitbox", "shitbox",

53
src/cmd/nproc/mod.rs Normal file
View File

@ -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<crate::Path>,
}
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<dyn std::error::Error>> {
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<crate::Path> {
self.path
}
}