Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ConEmu's OSC 9;9 to set the CWD #8330

Merged
4 commits merged into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,13 @@ namespace winrt::TerminalApp::implementation
if (profileGuid.has_value())
{
const auto settings{ winrt::make<TerminalSettings>(_settings, profileGuid.value(), *_bindings) };
const auto workingDirectory = terminalTab->GetActiveTerminalControl().WorkingDirectory();
const auto validWorkingDirectory = !workingDirectory.empty();
if (validWorkingDirectory)
{
settings.StartingDirectory(workingDirectory);
}

_CreateNewTabFromSettings(profileGuid.value(), settings);
}
}
Expand Down Expand Up @@ -1638,6 +1645,12 @@ namespace winrt::TerminalApp::implementation
{
profileFound = true;
controlSettings = { winrt::make<TerminalSettings>(_settings, current_guid.value(), *_bindings) };
const auto workingDirectory = focusedTab->GetActiveTerminalControl().WorkingDirectory();
const auto validWorkingDirectory = !workingDirectory.empty();
if (validWorkingDirectory)
{
controlSettings.StartingDirectory(workingDirectory);
}
realGuid = current_guid.value();
}
// TODO: GH#5047 - In the future, we should get the Profile of
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,12 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return _settings.ProfileName();
}

hstring TermControl::WorkingDirectory() const
{
hstring hstr{ _terminal->GetWorkingDirectory() };
return hstr;
}

// Method Description:
// - Given a copy-able selection, get the selected text from the buffer and send it to the
// Windows Clipboard (CascadiaWin32:main.cpp).
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

hstring Title();
hstring GetProfileName() const;
hstring WorkingDirectory() const;

bool CopySelectionToClipboard(bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats);
void PasteTextFromClipboard();
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ namespace Microsoft.Terminal.TerminalControl
UInt64 TaskbarState { get; };
UInt64 TaskbarProgress { get; };

String WorkingDirectory { get; };

Windows.Foundation.IReference<Windows.UI.Color> TabColor { get; };
event Windows.Foundation.TypedEventHandler<Object, Object> TabColorChanged;
}
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace Microsoft::Terminal::Core

virtual bool SetTaskbarProgress(const size_t state, const size_t progress) noexcept = 0;

virtual bool SetWorkingDirectory(std::wstring_view uri) noexcept = 0;
virtual std::wstring_view GetWorkingDirectory() noexcept = 0;

protected:
ITerminalApi() = default;
};
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Microsoft::Terminal::Core::Terminal final :
bool EndHyperlink() noexcept override;

bool SetTaskbarProgress(const size_t state, const size_t progress) noexcept override;
bool SetWorkingDirectory(std::wstring_view uri) noexcept override;
std::wstring_view GetWorkingDirectory() noexcept override;
#pragma endregion

#pragma region ITerminalInput
Expand Down Expand Up @@ -253,6 +255,7 @@ class Microsoft::Terminal::Core::Terminal final :

size_t _hyperlinkPatternId;

std::wstring _workingDirectory;
#pragma region Text Selection
// a selection is represented as a range between two COORDs (start and end)
// the pivot is the COORD that remains selected when you extend a selection in any direction
Expand Down
11 changes: 11 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,14 @@ bool Terminal::SetTaskbarProgress(const size_t state, const size_t progress) noe
}
return true;
}

bool Terminal::SetWorkingDirectory(std::wstring_view uri) noexcept
{
_workingDirectory = uri;
return true;
}

std::wstring_view Terminal::GetWorkingDirectory() noexcept
{
return _workingDirectory;
}
58 changes: 35 additions & 23 deletions src/cascadia/TerminalCore/TerminalDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,43 +417,55 @@ bool TerminalDispatch::DoConEmuAction(const std::wstring_view string) noexcept
const auto parts = Utils::SplitString(string, L';');
unsigned int subParam = 0;

// For now, the only ConEmu action we support is setting the taskbar state/progress,
// which has a sub param value of 4
if (parts.size() < 1 || !Utils::StringToUint(til::at(parts, 0), subParam) || subParam != 4)
if (parts.size() < 1 || !Utils::StringToUint(til::at(parts, 0), subParam))
{
return false;
}

if (parts.size() >= 2)
// 4 is SetProgressBar, which sets the taskbar state/progress.
if (subParam == 4)
{
// A state parameter is defined, parse it out
const auto stateSuccess = Utils::StringToUint(til::at(parts, 1), state);
if (!stateSuccess)
if (parts.size() >= 2)
{
return false;
}
if (parts.size() >= 3)
{
// A progress parameter is also defined, parse it out
const auto progressSuccess = Utils::StringToUint(til::at(parts, 2), progress);
if (!progressSuccess)
// A state parameter is defined, parse it out
const auto stateSuccess = Utils::StringToUint(til::at(parts, 1), state);
if (!stateSuccess)
{
return false;
}
if (parts.size() >= 3)
{
// A progress parameter is also defined, parse it out
const auto progressSuccess = Utils::StringToUint(til::at(parts, 2), progress);
if (!progressSuccess)
{
return false;
}
}
}
}

if (state > TaskbarMaxState)
{
// state is out of bounds, return false
return false;
if (state > TaskbarMaxState)
{
// state is out of bounds, return false
return false;
}
if (progress > TaskbarMaxProgress)
{
// progress is greater than the maximum allowed value, clamp it to the max
progress = TaskbarMaxProgress;
}
return _terminalApi.SetTaskbarProgress(state, progress);
}
if (progress > TaskbarMaxProgress)
// 9 is SetWorkingDirectory, which informs the terminal about the current working directory.
else if (subParam == 9)
{
// progress is greater than the maximum allowed value, clamp it to the max
progress = TaskbarMaxProgress;
if (parts.size() >= 2)
{
return _terminalApi.SetWorkingDirectory(til::at(parts, 1));
}
}
return _terminalApi.SetTaskbarProgress(state, progress);

return false;
}

// Routine Description:
Expand Down