Skip to content

Commit

Permalink
Implement ConEmu's OSC 9;9 to set the CWD (microsoft#8330)
Browse files Browse the repository at this point in the history
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
## Summary of the Pull Request

This PR implement the OSC 9;9 

|Sequence|Descriptoin|
| :------------- | :----------: |
|ESC ] 9 ; 9 ; “cwd” ST | Inform ConEmu about shell current working directory.|


<!-- Other than the issue solved, is this relevant to any other issues/existing PRs? --> 
## References

microsoft#8214

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [X] Closes microsoft#8166
* [X] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [ ] Tests added/passed
* [ ] Documentation updated. If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/terminal) and link it here: #xxx
* [ ] Schema updated.
* [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx

<!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
  • Loading branch information
skyline75489 authored and mpela81 committed Jan 28, 2021
1 parent 4fd557c commit cd7063e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
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 @@ -2460,6 +2460,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

0 comments on commit cd7063e

Please sign in to comment.