Skip to content

Commit 81ccfd7

Browse files
schultetwin1casey
authored andcommitted
Add a num_cpus() function
This is useful for when you are calling `make` from Just and want to pass a `-j` argument
1 parent e6b39df commit 81ccfd7

6 files changed

Lines changed: 32 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ heck = "0.4.0"
3131
lexiclean = "0.0.1"
3232
libc = "0.2.0"
3333
log = "0.4.4"
34+
num_cpus = "1.15.0"
3435
regex = "1.5.4"
3536
serde = { version = "1.0.130", features = ["derive", "rc"] }
3637
serde_json = "1.0.68"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ Done!
10791079
#### System Information
10801080

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

src/function.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
3838
"kebabcase" => Unary(kebabcase),
3939
"lowercamelcase" => Unary(lowercamelcase),
4040
"lowercase" => Unary(lowercase),
41+
"num_cpus" =>Nullary(num_cpus),
4142
"os" => Nullary(os),
4243
"os_family" => Nullary(os_family),
4344
"parent_directory" => Unary(parent_directory),
@@ -270,6 +271,11 @@ fn lowercase(_context: &FunctionContext, s: &str) -> Result<String, String> {
270271
Ok(s.to_lowercase())
271272
}
272273

274+
fn num_cpus(_context: &FunctionContext) -> Result<String, String> {
275+
let num = num_cpus::get();
276+
Ok(num.to_string())
277+
}
278+
273279
fn os(_context: &FunctionContext) -> Result<String, String> {
274280
Ok(target::os().to_owned())
275281
}

src/justfile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,11 +930,11 @@ c := a + b + a + b",
930930
x := arch()
931931
932932
a:
933-
{{os()}} {{os_family()}}",
933+
{{os()}} {{os_family()}} {{num_cpus()}}",
934934
"x := arch()
935935
936936
a:
937-
{{ os() }} {{ os_family() }}",
937+
{{ os() }} {{ os_family() }} {{ num_cpus() }}",
938938
}
939939

940940
test! {

tests/functions.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ test! {
44
name: test_os_arch_functions_in_interpolation,
55
justfile: r#"
66
foo:
7-
echo {{arch()}} {{os()}} {{os_family()}}
7+
echo {{arch()}} {{os()}} {{os_family()}} {{num_cpus()}}
88
"#,
9-
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
10-
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
9+
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
10+
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
1111
}
1212

1313
test! {
@@ -16,12 +16,13 @@ test! {
1616
a := arch()
1717
o := os()
1818
f := os_family()
19+
n := num_cpus()
1920
2021
foo:
21-
echo {{a}} {{o}} {{f}}
22+
echo {{a}} {{o}} {{f}} {{n}}
2223
"#,
23-
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
24-
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
24+
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
25+
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
2526
}
2627

2728
#[cfg(not(windows))]
@@ -246,11 +247,11 @@ test! {
246247
test! {
247248
name: test_os_arch_functions_in_default,
248249
justfile: r#"
249-
foo a=arch() o=os() f=os_family():
250-
echo {{a}} {{o}} {{f}}
250+
foo a=arch() o=os() f=os_family() n=num_cpus():
251+
echo {{a}} {{o}} {{f}} {{n}}
251252
"#,
252-
stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
253-
stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(),
253+
stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
254+
stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(),
254255
}
255256

256257
test! {

0 commit comments

Comments
 (0)