Skip to content

Commit 9135943

Browse files
committed
Hook up the new path translation logic
1 parent b81bd95 commit 9135943

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

src/cascadia/TerminalControl/TermControl.cpp

+53-37
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,54 @@ static Microsoft::Console::TSF::Handle& GetTSFHandle()
5858

5959
namespace winrt::Microsoft::Terminal::Control::implementation
6060
{
61+
static void _translatePathInPlace(std::wstring& fullPath, PathTranslationMode translationMode)
62+
{
63+
static constexpr wil::zwstring_view s_pathPrefixes[] = {
64+
{},
65+
/* WSL */ L"/mnt/",
66+
/* Cygwin */ L"/cygdrive/",
67+
/* MSYS2 */ L"/",
68+
};
69+
70+
if (translationMode == PathTranslationMode::None)
71+
{
72+
return;
73+
}
74+
75+
// All of the other path translation modes current result in /-delimited paths
76+
std::replace(fullPath.begin(), fullPath.end(), L'\\', L'/');
77+
78+
if (fullPath.size() >= 2 && fullPath.at(1) == L':')
79+
{
80+
// C:/foo/bar -> Cc/foo/bar
81+
fullPath.at(1) = til::tolower_ascii(fullPath.at(0));
82+
// Cc/foo/bar -> PREFIXc/foo/bar
83+
fullPath.replace(0, 1, s_pathPrefixes[static_cast<int>(translationMode)]);
84+
}
85+
else if (translationMode == PathTranslationMode::WSL)
86+
{
87+
// Stripping the UNC name and distribution prefix only applies to WSL.
88+
static constexpr std::wstring_view wslPathPrefixes[] = { L"//wsl.localhost/", L"//wsl$/" };
89+
for (auto prefix : wslPathPrefixes)
90+
{
91+
if (til::starts_with(fullPath, prefix))
92+
{
93+
if (const auto idx = fullPath.find(L'/', prefix.size()); idx != std::wstring::npos)
94+
{
95+
// //wsl.localhost/Ubuntu-18.04/foo/bar -> /foo/bar
96+
fullPath.erase(0, idx);
97+
}
98+
else
99+
{
100+
// //wsl.localhost/Ubuntu-18.04 -> /
101+
fullPath = L"/";
102+
}
103+
break;
104+
}
105+
}
106+
}
107+
}
108+
61109
TsfDataProvider::TsfDataProvider(TermControl* termControl) noexcept :
62110
_termControl{ termControl }
63111
{
@@ -3233,44 +3281,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
32333281
allPathsString += L" ";
32343282
}
32353283

3236-
const auto isWSL = false /* TODO PATH TRANSLATION MODE */;
3237-
3238-
if (isWSL)
3239-
{
3240-
std::replace(fullPath.begin(), fullPath.end(), L'\\', L'/');
3241-
3242-
if (fullPath.size() >= 2 && fullPath.at(1) == L':')
3243-
{
3244-
// C:/foo/bar -> Cc/foo/bar
3245-
fullPath.at(1) = til::tolower_ascii(fullPath.at(0));
3246-
// Cc/foo/bar -> /mnt/c/foo/bar
3247-
fullPath.replace(0, 1, L"/mnt/");
3248-
}
3249-
else
3250-
{
3251-
static constexpr std::wstring_view wslPathPrefixes[] = { L"//wsl.localhost/", L"//wsl$/" };
3252-
for (auto prefix : wslPathPrefixes)
3253-
{
3254-
if (til::starts_with(fullPath, prefix))
3255-
{
3256-
if (const auto idx = fullPath.find(L'/', prefix.size()); idx != std::wstring::npos)
3257-
{
3258-
// //wsl.localhost/Ubuntu-18.04/foo/bar -> /foo/bar
3259-
fullPath.erase(0, idx);
3260-
}
3261-
else
3262-
{
3263-
// //wsl.localhost/Ubuntu-18.04 -> /
3264-
fullPath = L"/";
3265-
}
3266-
break;
3267-
}
3268-
}
3269-
}
3270-
}
3284+
const auto translationMode{ _core.Settings().PathTranslationMode() };
3285+
_translatePathInPlace(fullPath, translationMode);
32713286

3272-
const auto quotesNeeded = isWSL || fullPath.find(L' ') != std::wstring::npos;
3273-
const auto quotesChar = isWSL ? L'\'' : L'"';
3287+
// All translated paths get quotes, and all strings spaces get quotes; all translated paths get single quotes
3288+
const auto quotesNeeded = translationMode != PathTranslationMode::None || fullPath.find(L' ') != std::wstring::npos;
3289+
const auto quotesChar = translationMode != PathTranslationMode::None ? L'\'' : L'"';
32743290

32753291
// Append fullPath and also wrap it in quotes if needed
32763292
if (quotesNeeded)

src/cascadia/TerminalSettingsModel/WslDistroGenerator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static winrt::com_ptr<implementation::Profile> makeProfile(const std::wstring& d
6666
WSLDistro->StartingDirectory(winrt::hstring{ DEFAULT_STARTING_DIRECTORY });
6767
}
6868
WSLDistro->Icon(L"ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png");
69+
WSLDistro->PathTranslationMode(winrt::Microsoft::Terminal::Control::PathTranslationMode::WSL);
6970
return WSLDistro;
7071
}
7172

0 commit comments

Comments
 (0)