From d3ec3dc66be24a443a32a71ed81017365526f2e8 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:38:30 -0600 Subject: [PATCH] allow vscode-specific ansi escape sequence to set path (#10990) # Description This change allows the vscode-specific ansi escape sequence of 633;P;Cwd= to be run when nushell detects that it's running inside of vscode's terminal. Otherwise the standard OSC7 will run. This is helpful with ctrl+g inside of vscode terminal as well. closed #10989 /cc @CAD97 # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-cli/src/repl.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 483f76f314..c05ad95add 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -626,19 +626,29 @@ pub fn evaluate_repl( if let Some(cwd) = stack.get_env_var(engine_state, "PWD") { let path = cwd.as_string()?; - // Communicate the path as OSC 7 (often used for spawning new tabs in the same dir) - run_ansi_sequence(&format!( - "\x1b]7;file://{}{}{}\x1b\\", - percent_encoding::utf8_percent_encode( - &hostname.unwrap_or_else(|| "localhost".to_string()), - percent_encoding::CONTROLS - ), - if path.starts_with('/') { "" } else { "/" }, - percent_encoding::utf8_percent_encode( - &path, - percent_encoding::CONTROLS - ) - ))?; + // Supported escape sequences of Microsoft's Visual Studio Code (vscode) + // https://code.visualstudio.com/docs/terminal/shell-integration#_supported-escape-sequences + if stack.get_env_var(engine_state, "TERM_PROGRAM") + == Some(Value::test_string("vscode")) + { + // If we're in vscode, run their specific ansi escape sequence. + // This is helpful for ctrl+g to change directories in the terminal. + run_ansi_sequence(&format!("\x1b]633;P;Cwd={}\x1b\\", path))?; + } else { + // Otherwise, communicate the path as OSC 7 (often used for spawning new tabs in the same dir) + run_ansi_sequence(&format!( + "\x1b]7;file://{}{}{}\x1b\\", + percent_encoding::utf8_percent_encode( + &hostname.unwrap_or_else(|| "localhost".to_string()), + percent_encoding::CONTROLS + ), + if path.starts_with('/') { "" } else { "/" }, + percent_encoding::utf8_percent_encode( + &path, + percent_encoding::CONTROLS + ) + ))?; + } // Try to abbreviate string for windows title let maybe_abbrev_path = if let Some(p) = nu_path::home_dir() {