Skip to content

Commit 355930c

Browse files
committed
install-crate: tty feature to make crossterm and user output optional
1 parent 816851f commit 355930c

File tree

6 files changed

+41
-50
lines changed

6 files changed

+41
-50
lines changed

Cargo.lock

Lines changed: 6 additions & 43 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "823b0c275
2828
cargo-gpu-install = { path = "./crates/cargo-gpu-install" }
2929
anyhow = "1.0.98"
3030
clap = { version = "4.5.41", features = ["derive"] }
31-
crossterm = "0.29.0"
31+
crossterm = { version = "0.29.0", default-features = false, features = ["events", "windows"] }
3232
directories = "6.0.0"
3333
env_logger = "0.11.8"
3434
log = "0.4"

crates/cargo-gpu-install/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ license.workspace = true
1212
clap = ["dep:clap", "spirv-builder/clap"]
1313
watch = ["spirv-builder/watch"]
1414
test = ["dep:tempfile"]
15+
tty = ["dep:crossterm"]
1516

1617
[dependencies]
1718
cargo_metadata.workspace = true
@@ -21,7 +22,7 @@ clap = { workspace = true, optional = true }
2122
directories.workspace = true
2223
log.workspace = true
2324
serde.workspace = true
24-
crossterm.workspace = true
25+
crossterm = { workspace = true, optional = true }
2526
tempfile = { workspace = true, optional = true }
2627

2728
[dev-dependencies]

crates/cargo-gpu-install/src/install_toolchain.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! toolchain installation logic
22
33
use anyhow::Context as _;
4+
#[cfg(feature = "tty")]
45
use crossterm::tty::IsTty as _;
56

67
use crate::user_output;
@@ -98,6 +99,20 @@ pub fn ensure_toolchain_and_components_exist(
9899
Ok(())
99100
}
100101

102+
#[cfg(not(feature = "tty"))]
103+
/// Prompt user if they want to install a new Rust toolchain.
104+
fn get_consent_for_toolchain_install(
105+
_prompt: &str,
106+
skip_toolchain_install_consent: bool,
107+
) -> anyhow::Result<()> {
108+
if skip_toolchain_install_consent {
109+
Ok(())
110+
} else {
111+
no_tty()
112+
}
113+
}
114+
115+
#[cfg(feature = "tty")]
101116
/// Prompt user if they want to install a new Rust toolchain.
102117
fn get_consent_for_toolchain_install(
103118
prompt: &str,
@@ -108,10 +123,7 @@ fn get_consent_for_toolchain_install(
108123
}
109124

110125
if !std::io::stdout().is_tty() {
111-
user_output!("No TTY detected so can't ask for consent to install Rust toolchain.");
112-
log::error!("Attempted to ask for consent when there's no TTY");
113-
#[expect(clippy::exit, reason = "can't ask for user consent if there's no TTY")]
114-
std::process::exit(1);
126+
no_tty()
115127
}
116128

117129
log::debug!("asking for consent to install the required toolchain");
@@ -143,3 +155,10 @@ fn get_consent_for_toolchain_install(
143155
std::process::exit(0);
144156
}
145157
}
158+
159+
fn no_tty() -> ! {
160+
user_output!("No TTY detected so can't ask for consent to install Rust toolchain.");
161+
log::error!("Attempted to ask for consent when there's no TTY");
162+
#[expect(clippy::exit, reason = "can't ask for user consent if there's no TTY")]
163+
std::process::exit(1);
164+
}

crates/cargo-gpu-install/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use spirv_builder;
1515

1616
/// Central function to write to the user.
1717
#[macro_export]
18+
#[cfg(feature = "tty")]
1819
macro_rules! user_output {
1920
($($args: tt)*) => { {
2021
#[allow(
@@ -37,6 +38,13 @@ macro_rules! user_output {
3738
} }
3839
}
3940

41+
/// Central function to write to the user.
42+
#[macro_export]
43+
#[cfg(not(feature = "tty"))]
44+
macro_rules! user_output {
45+
($($args: tt)*) => {{}};
46+
}
47+
4048
/// The central cache directory of cargo gpu
4149
///
4250
/// # Errors

crates/cargo-gpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ default-run = "cargo-gpu"
1414
cargo_metadata.workspace = true
1515
anyhow.workspace = true
1616
spirv-builder = { workspace = true, features = ["clap", "watch"] }
17-
cargo-gpu-install = { workspace = true, features = ["clap", "watch"] }
17+
cargo-gpu-install = { workspace = true, features = ["clap", "watch", "tty"] }
1818
clap.workspace = true
1919
env_logger.workspace = true
2020
log.workspace = true

0 commit comments

Comments
 (0)