Skip to content

Commit 8c3e6f5

Browse files
committed
fix(installer): respect NO_COLOR env var and handle NULL console handle
- Check NO_COLOR at startup and disable owo_colors globally if set, matching the no-color.org spec and the global CLI's behavior - Check for NULL handle (0) from GetStdHandle in addition to INVALID_HANDLE_VALUE (-1) for robustness when no console is attached - Enable owo-colors supports-colors feature for set_override support
1 parent 9af5ac7 commit 8c3e6f5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

crates/vite_installer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ path = "src/main.rs"
1414
[dependencies]
1515
clap = { workspace = true, features = ["derive"] }
1616
indicatif = { workspace = true }
17-
owo-colors = { workspace = true }
17+
owo-colors = { workspace = true, features = ["supports-colors"] }
1818
tokio = { workspace = true, features = ["full"] }
1919
vite_install = { workspace = true }
2020
vite_path = { workspace = true }

crates/vite_installer/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ fn init_dll_security() {}
4545
/// output, legacy console), we disable colors globally via owo_colors.
4646
#[cfg(windows)]
4747
fn init_colors() {
48+
// Respect NO_COLOR (https://no-color.org/)
49+
if std::env::var_os("NO_COLOR").is_some() {
50+
owo_colors::set_override(false);
51+
return;
52+
}
53+
4854
unsafe extern "system" {
4955
fn GetStdHandle(nStdHandle: u32) -> isize;
5056
fn GetConsoleMode(hConsoleHandle: isize, lpMode: *mut u32) -> i32;
@@ -57,7 +63,8 @@ fn init_colors() {
5763
let enable_vt = |std_handle: u32| -> bool {
5864
unsafe {
5965
let handle = GetStdHandle(std_handle);
60-
if handle == -1_isize {
66+
// INVALID_HANDLE_VALUE (-1) or NULL (0, no console attached)
67+
if handle == -1_isize || handle == 0 {
6168
return false;
6269
}
6370
let mut mode: u32 = 0;
@@ -69,7 +76,6 @@ fn init_colors() {
6976
}
7077
};
7178

72-
// Enable on both stdout (interactive menu) and stderr (info/warn/error)
7379
let stdout_ok = enable_vt(STD_OUTPUT_HANDLE);
7480
let stderr_ok = enable_vt(STD_ERROR_HANDLE);
7581

@@ -79,7 +85,11 @@ fn init_colors() {
7985
}
8086

8187
#[cfg(not(windows))]
82-
fn init_colors() {}
88+
fn init_colors() {
89+
if std::env::var_os("NO_COLOR").is_some() {
90+
owo_colors::set_override(false);
91+
}
92+
}
8393

8494
fn main() {
8595
init_dll_security();

0 commit comments

Comments
 (0)