Skip to content

Commit 3023d39

Browse files
committed
install-crate: tty feature to make crossterm and user output optional
1 parent 7eeae0f commit 3023d39

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

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, default-features = false, 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
@@ -10,6 +10,7 @@ pub use spirv_builder;
1010

1111
/// Central function to write to the user.
1212
#[macro_export]
13+
#[cfg(feature = "tty")]
1314
macro_rules! user_output {
1415
($($args: tt)*) => { {
1516
#[allow(
@@ -32,6 +33,13 @@ macro_rules! user_output {
3233
} }
3334
}
3435

36+
/// Central function to write to the user.
37+
#[macro_export]
38+
#[cfg(not(feature = "tty"))]
39+
macro_rules! user_output {
40+
($($args: tt)*) => {{}};
41+
}
42+
3543
/// The central cache directory of cargo gpu
3644
///
3745
/// # 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)