From 3259b1cb251dd60c024e9f870ab925fa8a7ca005 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Tue, 6 Feb 2024 09:29:34 +0100 Subject: [PATCH] Terminal: Fix Cwd escaping The shellintegration script escape the current working dir. We were missing the code to unescape the paths. This was particularly visible on Windows powershell where backslash was escaped by "\x5c". Change-Id: If697722986c72ebbc19d5ec8e3930321f3af7b6a Reviewed-by: Cristian Adam Reviewed-by: --- src/plugins/terminal/shellintegration.cpp | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/plugins/terminal/shellintegration.cpp b/src/plugins/terminal/shellintegration.cpp index 60b08de5917..2d3e8f986f7 100644 --- a/src/plugins/terminal/shellintegration.cpp +++ b/src/plugins/terminal/shellintegration.cpp @@ -84,6 +84,30 @@ bool ShellIntegration::canIntegrate(const Utils::CommandLine &cmdLine) return false; } +static QString unescape(QStringView str) +{ + QString result; + result.reserve(str.length()); + for (int i = 0; i < str.length(); ++i) { + if (str[i] == '\\' && str.length() > i + 1) { + if (str[i + 1] == '\\') { + // e.g. \\ -> "\" + result.append('\\'); + i++; + continue; + } else if (str[i + 1] == 'x' && str.length() > i + 3) { + // e.g.: \x5c -> \ (0x5c is the ASCII code for \) + result.append(QChar(str.sliced(i + 2, 2).toUShort(nullptr, 16))); + i += 3; + continue; + } + } + + result.append(str[i]); + } + return result; +} + void ShellIntegration::onOsc(int cmd, std::string_view str, bool initial, bool final) { if (initial) @@ -116,7 +140,7 @@ void ShellIntegration::onOsc(int cmd, std::string_view str, bool initial, bool f } else if (command[0] == 'P') { const auto [key, value] = Utils::splitAtFirst(data, '='); if (key == QStringView(u"Cwd")) - emit currentDirChanged(value.toString()); + emit currentDirChanged(unescape(value.toString())); } } }