дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
fix(terminal): track working directories whose names hold #, ? or % (#8190)
The shell integration printed the working directory raw in the OSC 7 file:// URL, and both the backend and the terminal view parsed it as a URL, so a folder named C#, what? or 100% lost everything from the # or ? on, or failed to decode, and the terminal's directory (new tabs, links, Run in terminal) went wrong. The zsh and bash integrations now percent-encode every byte outside [/._~A-Za-z0-9-] (fish uses string escape --style=url), and both parsers take everything after the host as the path, decode %XX escapes and keep any other %, so shells and tools that print the path raw still work. Co-authored-by: ntbang0901 <[email protected]>
Цей коміт міститься в:
1 джерело
1102822981
коміт
4ab9f02dc2
5 files changed
+142
-21
No files matched your search
@@ -38,7 +38,19 @@ ZDOTDIR=$OPENIDEA_USER_ZDOTDIR
|
||||
[[ -f $ZDOTDIR/.zshrc ]] && . $ZDOTDIR/.zshrc
|
||||
OPENIDEA_USER_ZDOTDIR=$ZDOTDIR
|
||||
ZDOTDIR=$_openidea_dir
|
||||
_openidea_osc7() { printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$?" "${HOST}" "${PWD}" "${VIRTUAL_ENV-}" "${CONDA_PREFIX-}" "${PATH}" }
|
||||
# $PWD as a URL path in _openidea_url: every byte outside [/._~A-Za-z0-9-] percent-encoded, so
|
||||
# `#`, `?` and `%` in a folder's name stay part of the path.
|
||||
_openidea_url_path() {
|
||||
emulate -L zsh
|
||||
local LC_ALL=C i c
|
||||
_openidea_url=
|
||||
for (( i = 1; i <= ${#PWD}; ++i )); do
|
||||
c=$PWD[i]
|
||||
if [[ $c == [/._~A-Za-z0-9-] ]]; then _openidea_url+=$c
|
||||
else printf -v c '%%%02X' "'$c"; _openidea_url+=$c; fi
|
||||
done
|
||||
}
|
||||
_openidea_osc7() { local _openidea_status=$?; _openidea_url_path; printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$_openidea_status" "${HOST}" "${_openidea_url}" "${VIRTUAL_ENV-}" "${CONDA_PREFIX-}" "${PATH}" }
|
||||
_openidea_preexec() { printf '\e]133;C\a' }
|
||||
# First, so the command counts as finished before other hooks set the prompt's title.
|
||||
precmd_functions=(_openidea_osc7 ${precmd_functions:#_openidea_osc7})
|
||||
@@ -93,7 +105,21 @@ else
|
||||
__openidea_where() { __openidea_dir=$PWD; __openidea_path=$PATH; }
|
||||
fi
|
||||
unset __openidea_root
|
||||
__openidea_osc7() { local __openidea_status=$?; __openidea_where; printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$__openidea_status" "${HOSTNAME}" "${__openidea_dir}" "${VIRTUAL_ENV-}" "${CONDA_PREFIX-}" "${__openidea_path}"; }
|
||||
# __openidea_dir as a URL path in __openidea_url: every byte outside [/._~A-Za-z0-9-] percent-encoded,
|
||||
# so `#`, `?` and `%` in a folder's name stay part of the path. In the C locale a character is a
|
||||
# byte; bash 3.2 reads one above 127 as a negative number, hence the `& 255`.
|
||||
__openidea_url_path() {
|
||||
local LC_ALL=C i c
|
||||
__openidea_url=
|
||||
for (( i = 0; i < ${#__openidea_dir}; i++ )); do
|
||||
c=${__openidea_dir:i:1}
|
||||
case $c in
|
||||
[/._~A-Za-z0-9-]) __openidea_url+=$c ;;
|
||||
*) printf -v c '%d' "'$c"; printf -v c '%%%02X' $((c & 255)); __openidea_url+=$c ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
__openidea_osc7() { local __openidea_status=$?; __openidea_where; __openidea_url_path; printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$__openidea_status" "${HOSTNAME}" "${__openidea_url}" "${VIRTUAL_ENV-}" "${CONDA_PREFIX-}" "${__openidea_path}"; }
|
||||
case ";${PROMPT_COMMAND:-};" in
|
||||
*";__openidea_osc7;"*) ;;
|
||||
*) PROMPT_COMMAND="__openidea_osc7${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
|
||||
@@ -135,7 +161,7 @@ fi
|
||||
"#;
|
||||
|
||||
const FISH_INIT: &str = concat!(
|
||||
r#"function __openidea_osc7 --on-event fish_prompt; printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$status" "$hostname" $PWD "$VIRTUAL_ENV" "$CONDA_PREFIX" (string join : $PATH); end; "#,
|
||||
r#"function __openidea_osc7 --on-event fish_prompt; printf '\e]133;D;%s\a\e]7;file://%s%s\a\e]133;OpenIDEA;Env;VIRTUAL_ENV=%s\a\e]133;OpenIDEA;Env;CONDA_PREFIX=%s\a\e]133;OpenIDEA;Env;PATH=%s\a' "$status" "$hostname" (string escape --style=url -- $PWD) "$VIRTUAL_ENV" "$CONDA_PREFIX" (string join : $PATH); end; "#,
|
||||
r#"function __openidea_preexec --on-event fish_preexec; printf '\e]133;C\a'; end; "#,
|
||||
r#"if functions -q fish_prompt; functions -c fish_prompt __openidea_fish_prompt; "#,
|
||||
r#"function fish_prompt; __openidea_fish_prompt; printf '\e]133;B\a'; end; end; "#,
|
||||
@@ -264,7 +290,7 @@ mod tests {
|
||||
// a finished long-running command can be told from a failed one.
|
||||
// zsh and bash read `$?`, fish `$status`, each at hook entry before any
|
||||
// other command runs.
|
||||
assert!(ZSHRC.contains(r"\e]133;D;%s\a") && ZSHRC.contains("\"$?\""));
|
||||
assert!(ZSHRC.contains(r"\e]133;D;%s\a") && ZSHRC.contains("_openidea_osc7() { local _openidea_status=$?;"));
|
||||
assert!(
|
||||
BASHRC.contains(r"\e]133;D;%s\a") && BASHRC.contains("__openidea_osc7() { local __openidea_status=$?;")
|
||||
);
|
||||
@@ -284,6 +310,12 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fish_percent_encodes_the_directory() {
|
||||
// zsh and bash run with `cd` into such folders in lib.rs; fish encodes with its own escape.
|
||||
assert!(FISH_INIT.contains(r"\e]7;file://%s%s\a") && FISH_INIT.contains("(string escape --style=url -- $PWD)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn powershell_clear_integration_only_changes_plain_windows_starts() {
|
||||
for shell in ["pwsh", "powershell", "PwSh"] {
|
||||
|
||||
@@ -1474,6 +1474,55 @@ mod tests {
|
||||
assert!(format!("{out}{rest}").contains("\x1b]7;file://"), "{out:?}");
|
||||
}
|
||||
|
||||
/// Real shells `cd` into folders whose names hold URL syntax (`#`, `?`, `%`), a space or
|
||||
/// non-ASCII letters: the integration prints them percent-encoded in the OSC 7 URL, and the
|
||||
/// session's working directory is the folder.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn integration_reports_folders_named_with_url_characters() {
|
||||
let root = std::env::temp_dir().join(format!("openidea-osc7-test-{}", std::process::id()));
|
||||
let home = root.join("home");
|
||||
std::fs::create_dir_all(&home).unwrap();
|
||||
let folders = [
|
||||
("C#/Demo", "/C%23/Demo"),
|
||||
("what?", "/what%3F"),
|
||||
("100% done", "/100%25%20done"),
|
||||
("caf\u{e9} \u{4e2d}\u{6587}", "/caf%C3%A9%20%E4%B8%AD%E6%96%87"),
|
||||
];
|
||||
for (name, _) in folders {
|
||||
std::fs::create_dir_all(root.join(name)).unwrap();
|
||||
}
|
||||
let mut wrong = Vec::new();
|
||||
for shell in ["/bin/zsh", "/bin/bash"] {
|
||||
if !Path::new(shell).exists() {
|
||||
continue;
|
||||
}
|
||||
let h = harness();
|
||||
let mut opts = sh("");
|
||||
opts.shell = Some(shell.into());
|
||||
opts.args = Vec::new();
|
||||
opts.shell_integration = true;
|
||||
opts.env.insert("HOME".into(), home.to_string_lossy().into_owned());
|
||||
opts.env.insert("ZDOTDIR".into(), home.to_string_lossy().into_owned());
|
||||
let (info, rx) = spawn_collect(&h, opts);
|
||||
read_until(&rx, "\x1b]133;B\x07");
|
||||
for (name, encoded) in folders {
|
||||
let dir = root.join(name).to_string_lossy().into_owned();
|
||||
h.manager.write(info.id, &format!("cd '{dir}'\n")).unwrap();
|
||||
let out = read_until(&rx, "\x1b]133;B\x07");
|
||||
let url = out.rsplit_once("\x1b]7;").and_then(|(_, rest)| rest.split_once('\x07')).map(|(url, _)| url);
|
||||
let cwd = h.manager.list_key("test").first().map(|s| s.cwd.clone());
|
||||
if !url.is_some_and(|url| url.ends_with(encoded)) || cwd.as_deref() != Some(dir.as_str()) {
|
||||
wrong.push(format!("{shell} {name:?}: url {url:?}, session cwd {cwd:?}"));
|
||||
}
|
||||
}
|
||||
h.manager.write(info.id, "exit\n").unwrap();
|
||||
let _ = wait_exit(&h, info.id);
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
assert!(wrong.is_empty(), "{}", wrong.join("\n"));
|
||||
}
|
||||
|
||||
/// Shell integration in a real shell: OSC 133 B after the prompt, the Python environment at
|
||||
/// each prompt, and the key Run Highlighted Command clears the line with, in emacs and vi
|
||||
/// command mode (where Ctrl-E Ctrl-U would not), so the next Enter runs nothing.
|
||||
|
||||
@@ -272,19 +272,22 @@ fn windows_drive_path(path: &str) -> Option<&str> {
|
||||
.then_some(rest)
|
||||
}
|
||||
|
||||
/// `%XX` escapes decoded; any other `%` is kept, for shells and tools that print the path raw.
|
||||
fn percent_decode(input: &str) -> Option<String> {
|
||||
let bytes = input.as_bytes();
|
||||
let hex = |i: usize| bytes.get(i).and_then(|&b| (b as char).to_digit(16));
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
if bytes[i] == b'%' && i + 2 < bytes.len() {
|
||||
let hi = (bytes[i + 1] as char).to_digit(16)?;
|
||||
let lo = (bytes[i + 2] as char).to_digit(16)?;
|
||||
out.push(((hi << 4) | lo) as u8);
|
||||
i += 3;
|
||||
} else {
|
||||
out.push(bytes[i]);
|
||||
i += 1;
|
||||
match (bytes[i], hex(i + 1), hex(i + 2)) {
|
||||
(b'%', Some(hi), Some(lo)) => {
|
||||
out.push(((hi << 4) | lo) as u8);
|
||||
i += 3;
|
||||
}
|
||||
(byte, ..) => {
|
||||
out.push(byte);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
String::from_utf8(out).ok()
|
||||
@@ -306,6 +309,29 @@ mod tests {
|
||||
assert_eq!(sb.cwd, "/CD:/x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn directory_names_keep_url_characters() {
|
||||
let mut sb = Scrollback::new("/initial".into());
|
||||
for (url, cwd) in [
|
||||
// As the shell integration prints them.
|
||||
("file://host/code/C%23/Demo", "/code/C#/Demo"),
|
||||
("file://host/code/100%25%20done", "/code/100% done"),
|
||||
("file://host/code/caf%C3%A9%20%E4%B8%AD%E6%96%87", "/code/caf\u{e9} \u{4e2d}\u{6587}"),
|
||||
// As shells and tools that don't encode print them.
|
||||
("file://host/code/C#/Demo", "/code/C#/Demo"),
|
||||
("file://host/code/what?", "/code/what?"),
|
||||
("file://host/code/100% done", "/code/100% done"),
|
||||
("file://host/code/50%", "/code/50%"),
|
||||
("file://host/code/caf\u{e9}", "/code/caf\u{e9}"),
|
||||
] {
|
||||
sb.push(&format!("\x1b]7;{url}\x07"));
|
||||
assert_eq!(sb.cwd, cwd, "{url}");
|
||||
}
|
||||
// Bytes that are no UTF-8 text leave the directory as it was.
|
||||
sb.push("\x1b]7;file://host/code/%FF\x07");
|
||||
assert_eq!(sb.cwd, "/code/caf\u{e9}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracks_osc_cwd_title_and_command_across_split_chunks() {
|
||||
let mut sb = Scrollback::new("/initial".into());
|
||||
|
||||
@@ -12,6 +12,19 @@ describe("osc7Directory", () => {
|
||||
expect(osc7Directory("file://host/CD:/x")).toBe("/CD:/x");
|
||||
});
|
||||
|
||||
it("keeps #, ? and % in directory names", () => {
|
||||
// As the shell integration prints them.
|
||||
expect(osc7Directory("file://host/code/C%23/Demo")).toBe("/code/C#/Demo");
|
||||
expect(osc7Directory("file://host/code/100%25%20done")).toBe("/code/100% done");
|
||||
expect(osc7Directory("file://host/C%3A/Users/me/C%23")).toBe("C:/Users/me/C#");
|
||||
// As shells and tools that don't encode print them.
|
||||
expect(osc7Directory("file://host/code/C#/Demo")).toBe("/code/C#/Demo");
|
||||
expect(osc7Directory("file://host/code/what?")).toBe("/code/what?");
|
||||
expect(osc7Directory("file://host/code/100% done")).toBe("/code/100% done");
|
||||
expect(osc7Directory("file://host/code/50%")).toBe("/code/50%");
|
||||
expect(osc7Directory("file:///code/café 中文")).toBe("/code/café 中文");
|
||||
});
|
||||
|
||||
it("ignores other URLs and malformed ones", () => {
|
||||
expect(osc7Directory("https://example.com/x")).toBeNull();
|
||||
expect(osc7Directory("not a url")).toBeNull();
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
const FILE_URL = "file://";
|
||||
|
||||
/**
|
||||
* OSC 7 (`file://host/path`): the directory a shell reports at each prompt. A Windows
|
||||
* shell (Git Bash, PowerShell) reports `file://host/C:/dir`, whose URL path `/C:/dir` is
|
||||
* no Windows directory: the drive path is `C:/dir`.
|
||||
* no Windows directory: the drive path is `C:/dir`. Shells and tools that don't
|
||||
* percent-encode the path print `#`, `?` and `%` as they are, so everything after the host
|
||||
* is the path, and a `%` that starts no `%XX` escape is kept.
|
||||
*/
|
||||
export function osc7Directory(data: string): string | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(data);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== "file:") return null;
|
||||
const url = data.trim();
|
||||
if (url.slice(0, FILE_URL.length).toLowerCase() !== FILE_URL) return null;
|
||||
const slash = url.indexOf("/", FILE_URL.length);
|
||||
if (slash < 0) return null;
|
||||
let path: string;
|
||||
try {
|
||||
path = decodeURIComponent(url.pathname);
|
||||
path = url.slice(slash).replace(/(%[0-9A-Fa-f]{2})+/g, (escapes) => decodeURIComponent(escapes));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача