From 029c586717cf335ce87f187de8820942a7380a38 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Sun, 17 Nov 2024 05:47:09 -0600 Subject: [PATCH] fix ansi bleed over on right prompt (#14357) # Description In certain situations, we had ansi bleed on the right prompt. This PR fixes that by prefixing the right prompt with an ansi reset `\x1b[0m`. This PR also adds some --log-level warn logging so we can see the ansi escapes that form the prompts. Closes https://github.com/nushell/nushell/issues/14268 --- crates/nu-cli/src/prompt_update.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/prompt_update.rs b/crates/nu-cli/src/prompt_update.rs index 63dd9ce19d..c033475586 100644 --- a/crates/nu-cli/src/prompt_update.rs +++ b/crates/nu-cli/src/prompt_update.rs @@ -1,5 +1,5 @@ use crate::NushellPrompt; -use log::trace; +use log::{trace, warn}; use nu_engine::ClosureEvalOnce; use nu_protocol::{ engine::{EngineState, Stack}, @@ -80,8 +80,13 @@ fn get_prompt_string( }) .and_then(|pipeline_data| { let output = pipeline_data.collect_string("", config).ok(); + let ansi_output = output.map(|mut x| { + // Always reset the color at the start of the right prompt + // to ensure there is no ansi bleed over + if x.is_empty() && prompt == PROMPT_COMMAND_RIGHT { + x.insert_str(0, "\x1b[0m") + }; - output.map(|mut x| { // Just remove the very last newline. if x.ends_with('\n') { x.pop(); @@ -91,7 +96,11 @@ fn get_prompt_string( x.pop(); } x - }) + }); + // Let's keep this for debugging purposes with nu --log-level warn + warn!("{}:{}:{} {:?}", file!(), line!(), column!(), ansi_output); + + ansi_output }) }