From cb9be598af6947c04a7e95b2ec77ada6e2ee655b Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Wed, 31 Jan 2024 12:09:27 -0800 Subject: [PATCH] feat: enable vt processing on windows (#7158) ### Description If we're on windows and are attempting to emit colors, help our users by setting [`ENABLE_VIRTUAL_TERMINAL_PROCESSING`](https://learn.microsoft.com/en-us/windows/console/setconsolemode) which allows ANSI color escape sequences to be respected by the Windows console. This might already be set to true by some users, but it is not enabled by default by Windows. ### Testing Instructions Look at the pretty colors on Windows! ![cmdp](https://github.com/vercel/turbo/assets/4131117/382fa484-03be-4005-9b95-0d4ab7a7aa26) ![pwsh](https://github.com/vercel/turbo/assets/4131117/08175677-1014-4264-b58f-26c991067ec5) (My default powershell colors are horrible and end up making the `docs:build` prefix invisible) ![gitbash](https://github.com/vercel/turbo/assets/4131117/d119347b-7ecd-40e3-9102-c9e3c2e089cc) Closes TURBO-2186 --------- Co-authored-by: Chris Olszewski Co-authored-by: Mehul Kar --- Cargo.lock | 1 + crates/turborepo-lib/Cargo.toml | 1 + crates/turborepo-lib/src/shim.rs | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d8330834ab29f..38f498ff72c8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11694,6 +11694,7 @@ dependencies = [ "console", "const_format", "convert_case 0.6.0", + "crossterm 0.26.1", "ctrlc", "dialoguer", "directories 4.0.1", diff --git a/crates/turborepo-lib/Cargo.toml b/crates/turborepo-lib/Cargo.toml index 935b54d49538a..bf0bf0907c8fc 100644 --- a/crates/turborepo-lib/Cargo.toml +++ b/crates/turborepo-lib/Cargo.toml @@ -43,6 +43,7 @@ clap = { workspace = true, features = ["derive", "env"] } clap_complete = { workspace = true } command-group = { version = "2.1.0", features = ["with-tokio"] } console = { workspace = true } +crossterm = "0.26" ctrlc = { version = "3.4.0", features = ["termination"] } dialoguer = { workspace = true, features = ["fuzzy-select"] } directories = "4.0.1" diff --git a/crates/turborepo-lib/src/shim.rs b/crates/turborepo-lib/src/shim.rs index db1ac943765de..bc645ad18498c 100644 --- a/crates/turborepo-lib/src/shim.rs +++ b/crates/turborepo-lib/src/shim.rs @@ -290,11 +290,30 @@ impl ShimArgs { if self.no_color { UI::new(true) } else if self.color { + // Do our best to enable ansi colors, but even if the terminal doesn't support + // still emit ansi escape sequences. + Self::supports_ansi(); UI::new(false) - } else { + } else if Self::supports_ansi() { + // If the terminal supports ansi colors, then we can infer if we should emit + // colors UI::infer() + } else { + UI::new(true) } } + + #[cfg(windows)] + fn supports_ansi() -> bool { + // This call has the side effect of setting ENABLE_VIRTUAL_TERMINAL_PROCESSING + // to true. https://learn.microsoft.com/en-us/windows/console/setconsolemode + crossterm::ansi_support::supports_ansi() + } + + #[cfg(not(windows))] + fn supports_ansi() -> bool { + true + } } #[derive(Debug, Clone, Deserialize)]