Skip to content

Commit

Permalink
Add num_cpus() function (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
schultetwin1 committed Aug 2, 2023
1 parent e6b39df commit 63ed00f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ heck = "0.4.0"
lexiclean = "0.0.1"
libc = "0.2.0"
log = "0.4.4"
num_cpus = "1.15.0"
regex = "1.5.4"
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = "1.0.68"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ Done!
#### System Information

- `arch()` — Instruction set architecture. Possible values are: `"aarch64"`, `"arm"`, `"asmjs"`, `"hexagon"`, `"mips"`, `"msp430"`, `"powerpc"`, `"powerpc64"`, `"s390x"`, `"sparc"`, `"wasm32"`, `"x86"`, `"x86_64"`, and `"xcore"`.
- `num_cpus()` - Number of logical CPUs.
- `os()` — Operating system. Possible values are: `"android"`, `"bitrig"`, `"dragonfly"`, `"emscripten"`, `"freebsd"`, `"haiku"`, `"ios"`, `"linux"`, `"macos"`, `"netbsd"`, `"openbsd"`, `"solaris"`, and `"windows"`.
- `os_family()` — Operating system family; possible values are: `"unix"` and `"windows"`.

Expand Down
6 changes: 6 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"kebabcase" => Unary(kebabcase),
"lowercamelcase" => Unary(lowercamelcase),
"lowercase" => Unary(lowercase),
"num_cpus" => Nullary(num_cpus),
"os" => Nullary(os),
"os_family" => Nullary(os_family),
"parent_directory" => Unary(parent_directory),
Expand Down Expand Up @@ -270,6 +271,11 @@ fn lowercase(_context: &FunctionContext, s: &str) -> Result<String, String> {
Ok(s.to_lowercase())
}

fn num_cpus(_context: &FunctionContext) -> Result<String, String> {
let num = num_cpus::get();
Ok(num.to_string())
}

fn os(_context: &FunctionContext) -> Result<String, String> {
Ok(target::os().to_owned())
}
Expand Down
4 changes: 2 additions & 2 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,11 @@ c := a + b + a + b",
x := arch()
a:
{{os()}} {{os_family()}}",
{{os()}} {{os_family()}} {{num_cpus()}}",
"x := arch()
a:
{{ os() }} {{ os_family() }}",
{{ os() }} {{ os_family() }} {{ num_cpus() }}",
}

test! {
Expand Down
21 changes: 11 additions & 10 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ test! {
name: test_os_arch_functions_in_interpolation,
justfile: r#"
foo:
echo {{arch()}} {{os()}} {{os_family()}}
echo {{arch()}} {{os()}} {{os_family()}} {{num_cpus()}}
"#,
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
}

test! {
Expand All @@ -16,12 +16,13 @@ test! {
a := arch()
o := os()
f := os_family()
n := num_cpus()
foo:
echo {{a}} {{o}} {{f}}
echo {{a}} {{o}} {{f}} {{n}}
"#,
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
}

#[cfg(not(windows))]
Expand Down Expand Up @@ -246,11 +247,11 @@ test! {
test! {
name: test_os_arch_functions_in_default,
justfile: r#"
foo a=arch() o=os() f=os_family():
echo {{a}} {{o}} {{f}}
foo a=arch() o=os() f=os_family() n=num_cpus():
echo {{a}} {{o}} {{f}} {{n}}
"#,
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
}

test! {
Expand Down

0 comments on commit 63ed00f

Please sign in to comment.