From 8ae631c88ed4b08e3173e24453f1055581026167 Mon Sep 17 00:00:00 2001 From: James Pack Date: Sat, 14 Jun 2025 18:29:38 -0400 Subject: [PATCH 1/2] Enable Copy As Path/Paste with Path Translation in *nix profiles --- src/cascadia/TerminalControl/ControlCore.cpp | 20 ++++++++++++++++++++ src/cascadia/TerminalControl/ControlCore.h | 1 + src/cascadia/TerminalControl/TermControl.cpp | 2 +- src/cascadia/TerminalControl/TermControl.h | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp index a96adb7a1d2..ee0d7876a84 100644 --- a/src/cascadia/TerminalControl/ControlCore.cpp +++ b/src/cascadia/TerminalControl/ControlCore.cpp @@ -20,6 +20,8 @@ #include "../../renderer/uia/UiaRenderer.hpp" #include "../../types/inc/CodepointWidthDetector.hpp" +#include "TermControl.h" + #include "ControlCore.g.cpp" #include "SelectionColor.g.cpp" @@ -1441,6 +1443,24 @@ namespace winrt::Microsoft::Terminal::Control::implementation using namespace ::Microsoft::Console::Utils; auto filtered = FilterStringForPaste(hstr, CarriageReturnNewline | ControlCodes); + std::filesystem::path path{ filtered }; + + if (!path.empty() && _settings->PathTranslationStyle() != PathTranslationStyle::None) + { + filtered = path.wstring(); + // Explorer puts paths wrapped in double quotes on the clipboard. The translation routine expects it to be without quotes. + if (!filtered.empty() && filtered.front() == '\"') + { + filtered.erase(0, 1); + } + if (!filtered.empty() && filtered.back() == '\"') + { + filtered.pop_back(); + } + + TermControl::_translatePathInPlace(filtered, _settings->PathTranslationStyle()); + } + if (BracketedPasteEnabled()) { filtered.insert(0, L"\x1b[200~"); diff --git a/src/cascadia/TerminalControl/ControlCore.h b/src/cascadia/TerminalControl/ControlCore.h index 8e99e261559..a7ce3e497ca 100644 --- a/src/cascadia/TerminalControl/ControlCore.h +++ b/src/cascadia/TerminalControl/ControlCore.h @@ -449,6 +449,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation friend class ControlUnitTests::ControlCoreTests; friend class ControlUnitTests::ControlInteractivityTests; + bool _inUnitTests{ false }; }; } diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 7118c7a8689..37fc4e103f2 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -90,7 +90,7 @@ static Microsoft::Console::TSF::Handle& GetTSFHandle() namespace winrt::Microsoft::Terminal::Control::implementation { - static void _translatePathInPlace(std::wstring& fullPath, PathTranslationStyle translationStyle) + void TermControl::_translatePathInPlace(std::wstring& fullPath, PathTranslationStyle translationStyle) { static constexpr wil::zwstring_view s_pathPrefixes[] = { {}, diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index 2e8fadd8506..de7ece4045d 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -59,6 +59,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation uint64_t ContentId() const; hstring GetProfileName() const; + static void _translatePathInPlace(std::wstring& fullPath, PathTranslationStyle translationStyle); bool CopySelectionToClipboard(bool dismissSelection, bool singleLine, bool withControlSequences, const Windows::Foundation::IReference& formats); void PasteTextFromClipboard(); From 6709211dc7954b79752b9f71352431115f62caaa Mon Sep 17 00:00:00 2001 From: James Pack Date: Mon, 16 Jun 2025 09:47:17 -0400 Subject: [PATCH 2/2] Dont wrap filtered text in path. The translation routine already checks if its looking at a path --- src/cascadia/TerminalControl/ControlCore.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp index ee0d7876a84..0e0b5680efa 100644 --- a/src/cascadia/TerminalControl/ControlCore.cpp +++ b/src/cascadia/TerminalControl/ControlCore.cpp @@ -1443,11 +1443,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation using namespace ::Microsoft::Console::Utils; auto filtered = FilterStringForPaste(hstr, CarriageReturnNewline | ControlCodes); - std::filesystem::path path{ filtered }; - - if (!path.empty() && _settings->PathTranslationStyle() != PathTranslationStyle::None) + if (!filtered.empty() && _settings->PathTranslationStyle() != PathTranslationStyle::None) { - filtered = path.wstring(); // Explorer puts paths wrapped in double quotes on the clipboard. The translation routine expects it to be without quotes. if (!filtered.empty() && filtered.front() == '\"') {