From 71e1eec274eb1f3359241380a199e89818072b0a Mon Sep 17 00:00:00 2001 From: Liang Date: Sat, 26 Sep 2026 16:11:32 +0800 Subject: [PATCH] fix(cli): preserve setup colors when stdout is captured --- crates/vp_cli_help/src/lib.rs | 14 ++++++- .../fixtures/download_progress/snapshots.toml | 12 +++++- .../snapshots/installation_progress_ci.md | 20 +++++----- .../installation_progress_no_color.md | 39 +++++++++++++++++++ 4 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_no_color.md diff --git a/crates/vp_cli_help/src/lib.rs b/crates/vp_cli_help/src/lib.rs index 23f9f79b86..243c0309c6 100644 --- a/crates/vp_cli_help/src/lib.rs +++ b/crates/vp_cli_help/src/lib.rs @@ -9,7 +9,7 @@ use std::{borrow::Cow, fmt::Write as _, io::Write as _}; use clap::{Arg, Command}; -use console::style; +use console::StyledObject; use terminal_size::{Width, terminal_size_of}; const HELP_RIGHT_MARGIN: usize = 4; @@ -157,7 +157,17 @@ pub fn accent_command(command: &str) -> String { } pub fn should_style_help() -> bool { - console::colors_enabled() + if vp_shared::output::user_output_to_stderr() { + console::colors_enabled_stderr() + } else { + console::colors_enabled() + } +} + +// Self-setup captures stdout for shell assignments and routes its diagnostics to stderr. +fn style(value: D) -> StyledObject { + let styled = console::style(value); + if vp_shared::output::user_output_to_stderr() { styled.for_stderr() } else { styled } } fn terminal_content_width() -> usize { diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots.toml index aea82015da..b48c82159c 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots.toml @@ -53,10 +53,18 @@ steps = [{ argv = ["node", "install.mjs", "interactive", "failure"], interaction name = "installation_progress_non_tty" vp = "global" skip-platforms = ["windows"] -steps = [{ argv = ["node", "install.mjs"], tty = false }] +steps = [{ argv = ["node", "install.mjs"], tty = false, formatted-snapshot = true }] [[case]] name = "installation_progress_ci" vp = "global" skip-platforms = ["windows"] -steps = [{ argv = ["node", "install.mjs"], envs = [["CI", "1"]] }] +# The installer captures stdout while setup diagnostics retain the terminal on stderr. +steps = [{ argv = ["node", "install.mjs"], envs = [["CI", "1"]], formatted-snapshot = true }] + +[[case]] +name = "installation_progress_no_color" +vp = "global" +skip-platforms = ["windows"] +env = { CI = "1", NO_COLOR = "1" } +steps = [{ argv = ["node", "install.mjs"], formatted-snapshot = true }] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_ci.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_ci.md index 2062528895..aa76996109 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_ci.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_ci.md @@ -4,14 +4,14 @@ ``` Before installation: preserve this output. -info: installing vite-plus@... -info: Preparing Node.js and pnpm... -info: Installing dependencies... -✓ Dependencies installed. -Setup: +\x1b[94;1minfo: installing vite-plus@... +\x1b[94;1minfo: Preparing Node.js and pnpm... +\x1b[94;1minfo: Installing dependencies... +\x1b[32m\xe2\x9c\x93 Dependencies installed. +\x1b[94;1mSetup: Preparing vite-plus environment. -Created Shims: +\x1b[94;1mCreated Shims: /home/fallback-bin/node /home/bin/npm /home/bin/npx @@ -26,14 +26,14 @@ Created Shims: /home/bin/vpx /home/bin/vpr -Next Steps: +\x1b[94;1mNext Steps: Activate Vite+ in this terminal: - . "/home/env" + . \"/home/env\" Add the command to your .zshrc file to activate future Zsh terminals. - Restart an already-running IDE to load its environment. Run `vp env doctor` to verify. -✓ Vite+ setup complete. + Restart an already-running IDE to load its environment. Run `\x1b[94mvp env doctor` to verify. +\x1b[32m\xe2\x9c\x93 Vite+ setup complete. Bootstrap stdout contains only shell assignments. After installation. ``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_no_color.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_no_color.md new file mode 100644 index 0000000000..0d0319c272 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/download_progress/snapshots/installation_progress_no_color.md @@ -0,0 +1,39 @@ +# installation_progress_no_color + +## `node install.mjs` + +``` +Before installation: preserve this output. +info: installing vite-plus@... +info: Preparing Node.js and pnpm... +info: Installing dependencies... +\xe2\x9c\x93 Dependencies installed. +Setup: + Preparing vite-plus environment. + +Created Shims: + /home/fallback-bin/node + /home/bin/npm + /home/bin/npx + /home/bin/pnpm + /home/bin/pnpx + /home/bin/pn + /home/bin/pnx + /home/bin/yarn + /home/bin/yarnpkg + /home/bin/bun + /home/bin/bunx + /home/bin/vpx + /home/bin/vpr + +Next Steps: + Activate Vite+ in this terminal: + . \"/home/env\" + + Add the command to your .zshrc file to activate future Zsh terminals. + + Restart an already-running IDE to load its environment. Run `vp env doctor` to verify. +\xe2\x9c\x93 Vite+ setup complete. +Bootstrap stdout contains only shell assignments. +After installation. +```