Skip to content
Open
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
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class Microsoft::Terminal::Core::Terminal final :
void ReturnAnswerback() override;
void WarningBell() override;
void SetWindowTitle(const std::wstring_view title) override;
std::wstring_view GetWindowTitle() const noexcept override;
std::pair<CursorType, bool> GetUserDefaultCursorStyle() const noexcept override;
bool ResizeWindow(const til::CoordType width, const til::CoordType height) override;
void SetCodePage(const unsigned int codepage) noexcept override;
Expand Down
13 changes: 13 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ void Terminal::SetWindowTitle(const std::wstring_view title)
}
}

// Returns the title set by the application, or an empty string if there is
// none. Setting an empty title restores the starting title, so a title that
// is saved and later restored round-trips correctly.
std::wstring_view Terminal::GetWindowTitle() const noexcept
{
_assertLocked();
if (_title)
{
return *_title;
}
return {};
}

std::pair<CursorType, bool> Terminal::GetUserDefaultCursorStyle() const noexcept
{
_assertLocked();
Expand Down
31 changes: 31 additions & 0 deletions src/cascadia/UnitTests_TerminalCore/TerminalApiTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace TerminalCoreUnitTests
TEST_METHOD(CursorBlinkingChangesDisabled);
TEST_METHOD(CursorBlinkingCompatibilityReload);
TEST_METHOD(RepeatedAltBufferEntryKeepsLiveSize);

TEST_METHOD(WindowTitleStack);
};
};

Expand Down Expand Up @@ -615,3 +617,32 @@ void TerminalCoreUnitTests::TerminalApiTest::SetWorkingDirectory()
stateMachine.ProcessString(L"\x1b]9;9;D:\\中文\x1b\\");
VERIFY_ARE_EQUAL(term.GetWorkingDirectory(), L"D:\\中文");
}

void TerminalApiTest::WindowTitleStack()
{
Terminal term{ Terminal::TestDummyMarker{} };
DummyRenderer renderer{ &term };
term.Create({ 100, 100 }, 0, renderer);
// SetWindowTitle calls the title changed callback, so give it one.
term.SetTitleChangedCallback([](std::wstring_view) {});

auto& stateMachine = *(term._stateMachine);

Log::Comment(L"Case B: no app title set before the push. The pop must restore \"no app title\".");
VERIFY_IS_FALSE(term._title.has_value());
stateMachine.ProcessString(L"\x1b[22t");
stateMachine.ProcessString(L"\x1b]2;Vim\x1b\\");
VERIFY_IS_TRUE(term._title.has_value());
VERIFY_ARE_EQUAL(std::wstring_view{ L"Vim" }, term.GetConsoleTitle());
stateMachine.ProcessString(L"\x1b[23t");
VERIFY_IS_FALSE(term._title.has_value());

Log::Comment(L"Case A: an app title set with OSC 2 is saved by CSI 22;0t and restored by CSI 23;0t.");
stateMachine.ProcessString(L"\x1b]2;App\x1b\\");
VERIFY_ARE_EQUAL(std::wstring_view{ L"App" }, term.GetConsoleTitle());
stateMachine.ProcessString(L"\x1b[22;0t");
stateMachine.ProcessString(L"\x1b]2;Vim\x1b\\");
VERIFY_ARE_EQUAL(std::wstring_view{ L"Vim" }, term.GetConsoleTitle());
stateMachine.ProcessString(L"\x1b[23;0t");
VERIFY_ARE_EQUAL(std::wstring_view{ L"App" }, term.GetConsoleTitle());
}
12 changes: 12 additions & 0 deletions src/host/outputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ void ConhostInternalGetSet::SetWindowTitle(std::wstring_view title)
gci.SetTitle(title.empty() ? gci.GetOriginalTitle() : title);
}

// Routine Description:
// - Gets the title of the console window.
// Arguments:
// - <none>
// Return Value:
// - The current window title, without any prefix like "Select".
std::wstring_view ConhostInternalGetSet::GetWindowTitle() const
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
return gci.GetTitle();
}

// Routine Description:
// - Swaps to the alternate screen buffer. In virtual terminals, there exists both a "main"
// screen buffer and an alternate. This creates a new alternate, and switches to it.
Expand Down
1 change: 1 addition & 0 deletions src/host/outputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ConhostInternalGetSet final : public Microsoft::Console::VirtualTerminal::
void WarningBell() override;

void SetWindowTitle(const std::wstring_view title) override;
std::wstring_view GetWindowTitle() const override;

void UseAlternateScreenBuffer(const TextAttribute& attrs) override;

Expand Down
28 changes: 28 additions & 0 deletions src/host/ut_host/ScreenBufferTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class ScreenBufferTests
TEST_METHOD(SimpleMarkCommand);
TEST_METHOD(SimpleWrappedCommand);
TEST_METHOD(SimplePromptRegions);

TEST_METHOD(WindowTitleStack);
};

void ScreenBufferTests::SingleAlternateBufferCreationTest()
Expand Down Expand Up @@ -8816,3 +8818,29 @@ void ScreenBufferTests::SimplePromptRegions()
VERIFY_IS_FALSE(mark.outputEnd.has_value());
}
}

void ScreenBufferTests::WindowTitleStack()
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto& si = gci.GetActiveOutputBuffer();
auto& stateMachine = si.GetStateMachine();

// Put the title back the way we found it, so other tests are not affected.
const std::wstring originalTitle{ gci.GetTitle() };
auto restoreTitle = wil::scope_exit([&] { gci.SetTitle(originalTitle); });

Log::Comment(L"Set the title through the console API, as SetConsoleTitle would.");
gci.SetTitle(L"From API");
VERIFY_ARE_EQUAL(std::wstring_view{ L"From API" }, gci.GetTitle());

Log::Comment(L"Push the title with XTWINOPS 22 (CSI 22 t).");
stateMachine.ProcessString(L"\x1b[22t");

Log::Comment(L"Change the title with OSC 2.");
stateMachine.ProcessString(L"\x1b]2;Vim\x1b\\");
VERIFY_ARE_EQUAL(std::wstring_view{ L"Vim" }, gci.GetTitle());

Log::Comment(L"Pop the title with XTWINOPS 23 (CSI 23 t). The API title should be restored.");
stateMachine.ProcessString(L"\x1b[23t");
VERIFY_ARE_EQUAL(std::wstring_view{ L"From API" }, gci.GetTitle());
}
4 changes: 3 additions & 1 deletion src/terminal/adapter/DispatchTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ namespace Microsoft::Console::VirtualTerminal::DispatchTypes
ResizeWindowInCharacters = 8,
ReportTextSizeInPixels = 14,
ReportCharacterCellSize = 16,
ReportTextSizeInCharacters = 18
ReportTextSizeInCharacters = 18,
PushWindowTitle = 22,
PopWindowTitle = 23
};

enum class CursorStyle : VTInt
Expand Down
1 change: 1 addition & 0 deletions src/terminal/adapter/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Microsoft::Console::VirtualTerminal
virtual void ReturnAnswerback() = 0;
virtual void WarningBell() = 0;
virtual void SetWindowTitle(const std::wstring_view title) = 0;
virtual std::wstring_view GetWindowTitle() const = 0;
virtual void UseAlternateScreenBuffer(const TextAttribute& attrs) = 0;
virtual void UseMainScreenBuffer() = 0;

Expand Down
20 changes: 20 additions & 0 deletions src/terminal/adapter/adaptDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3530,6 +3530,26 @@ void AdaptDispatch::WindowManipulation(const DispatchTypes::WindowManipulationTy
case DispatchTypes::WindowManipulationType::ReportCharacterCellSize:
reportSize(SixelParser::CellSizeForLevel());
break;
case DispatchTypes::WindowManipulationType::PushWindowTitle:
// The second parameter selects the icon title (1), the window
// title (2), or both (0 or omitted). We only have one title,
// and OSC 1 already sets it, so all three save the same value.
if (parameter1.value_or(0) <= 2)
{
if (_titleStack.size() >= _maxTitleStackSize)
{
_titleStack.erase(_titleStack.begin());
}
_titleStack.emplace_back(_api.GetWindowTitle());
}
break;
case DispatchTypes::WindowManipulationType::PopWindowTitle:
if (parameter1.value_or(0) <= 2 && !_titleStack.empty())
{
_api.SetWindowTitle(_titleStack.back());
_titleStack.pop_back();
}
break;
default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/adapter/adaptDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ namespace Microsoft::Console::VirtualTerminal

SgrStack _sgrStack;

// XTWINOPS 22/23 title stack. Like the XTPUSHSGR stack, it keeps at most 10 entries.
static constexpr size_t _maxTitleStackSize = 10;
std::vector<std::wstring> _titleStack;

void _SetUnderlineStyleHelper(const VTParameter option, TextAttribute& attr) noexcept;
size_t _SetRgbColorsHelper(const VTParameters options,
TextAttribute& attr,
Expand Down
111 changes: 111 additions & 0 deletions src/terminal/adapter/ut_adapter/adapterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class TestGetSet final : public ITerminalApi
{
Log::Comment(L"SetWindowTitle MOCK called...");

_windowTitle = title;
_setWindowTitleCount++;

if (_setWindowTitleResult)
{
// Put into WEX strings for rich logging when they don't compare.
Expand All @@ -140,6 +143,11 @@ class TestGetSet final : public ITerminalApi
}
}

std::wstring_view GetWindowTitle() const override
{
return _windowTitle;
}

void UseAlternateScreenBuffer(const TextAttribute& /*attr*/) override
{
Log::Comment(L"UseAlternateScreenBuffer MOCK called...");
Expand Down Expand Up @@ -403,6 +411,8 @@ class TestGetSet final : public ITerminalApi

bool _setWindowTitleResult = false;
std::wstring_view _expectedWindowTitle{};
std::wstring _windowTitle{};
size_t _setWindowTitleCount = 0;
bool _setCodePageResult = false;
bool _expectedShowWindow = false;
bool _resizeWindowCalled = false;
Expand Down Expand Up @@ -3048,6 +3058,107 @@ class AdapterTest
_pDispatch->SetWindowTitle({});
}

TEST_METHOD(WindowTitleStackTest)
{
const auto setTitle = [&](const std::wstring_view title) {
_stateMachine->ProcessString(L"\x1b]2;" + std::wstring{ title } + L"\x1b\\");
};
const auto verifyPopRestores = [&](const std::wstring_view popSequence, const std::wstring_view expectedTitle) {
_testGetSet->_setWindowTitleCount = 0;
_stateMachine->ProcessString(popSequence);
VERIFY_ARE_EQUAL(size_t{ 1 }, _testGetSet->_setWindowTitleCount);
VERIFY_ARE_EQUAL(expectedTitle, _testGetSet->GetWindowTitle());
};
const auto verifyPopIgnored = [&](const std::wstring_view popSequence, const std::wstring_view expectedTitle) {
_testGetSet->_setWindowTitleCount = 0;
_stateMachine->ProcessString(popSequence);
VERIFY_ARE_EQUAL(size_t{ 0 }, _testGetSet->_setWindowTitleCount);
VERIFY_ARE_EQUAL(expectedTitle, _testGetSet->GetWindowTitle());
};

Log::Comment(L"Test 1: pushes and pops are last in, first out, and can be nested");
_testGetSet->_windowTitle = L"Original";
_stateMachine->ProcessString(L"\x1b[22;0t");
setTitle(L"One");
_stateMachine->ProcessString(L"\x1b[22;0t");
setTitle(L"Two");
VERIFY_ARE_EQUAL(std::wstring_view{ L"Two" }, _testGetSet->GetWindowTitle());
verifyPopRestores(L"\x1b[23;0t", L"One");
verifyPopRestores(L"\x1b[23;0t", L"Original");

Log::Comment(L"Test 2: Ps omitted, 0, 1 and 2 all push and pop the title");
const std::array<std::pair<std::wstring_view, std::wstring_view>, 4> pushPopSequences{ {
{ L"\x1b[22t", L"\x1b[23t" },
{ L"\x1b[22;0t", L"\x1b[23;0t" },
{ L"\x1b[22;1t", L"\x1b[23;1t" },
{ L"\x1b[22;2t", L"\x1b[23;2t" },
} };
for (const auto& [pushSequence, popSequence] : pushPopSequences)
{
setTitle(L"Saved");
_stateMachine->ProcessString(pushSequence);
setTitle(L"Other");
verifyPopRestores(popSequence, L"Saved");
}

Log::Comment(L"Test 3: Ps = 3 is ignored, for both push and pop");
setTitle(L"Before");
_stateMachine->ProcessString(L"\x1b[22;3t");
setTitle(L"After");
verifyPopIgnored(L"\x1b[23;0t", L"After");
setTitle(L"Kept");
_stateMachine->ProcessString(L"\x1b[22;0t");
setTitle(L"Changed");
verifyPopIgnored(L"\x1b[23;3t", L"Changed");
verifyPopRestores(L"\x1b[23;0t", L"Kept");

Log::Comment(L"Test 4: a pop on an empty stack does not set the title");
setTitle(L"Unchanged");
verifyPopIgnored(L"\x1b[23;0t", L"Unchanged");
verifyPopIgnored(L"\x1b[23t", L"Unchanged");

Log::Comment(L"Test 5: vim's order (22;2, 22;1, then 23;2, 23;1) restores the original title");
setTitle(L"Shell");
_stateMachine->ProcessString(L"\x1b[22;2t");
_stateMachine->ProcessString(L"\x1b[22;1t");
setTitle(L"Vim");
_stateMachine->ProcessString(L"\x1b[23;2t");
_stateMachine->ProcessString(L"\x1b[23;1t");
VERIFY_ARE_EQUAL(std::wstring_view{ L"Shell" }, _testGetSet->GetWindowTitle());
}

TEST_METHOD(WindowTitleStackLimitTest)
{
const auto titleFor = [](const size_t i) {
return std::wstring{ L"Title " } + std::to_wstring(i);
};

Log::Comment(L"Push 11 different titles");
for (size_t i = 1; i <= 11; i++)
{
_stateMachine->ProcessString(L"\x1b]2;" + titleFor(i) + L"\x1b\\");
_stateMachine->ProcessString(L"\x1b[22;0t");
}
_stateMachine->ProcessString(L"\x1b]2;Final\x1b\\");

Log::Comment(L"The first 10 pops restore the 10 newest titles in reverse order");
for (size_t i = 11; i >= 2; i--)
{
const auto expectedTitle = titleFor(i);
_testGetSet->_setWindowTitleCount = 0;
_stateMachine->ProcessString(L"\x1b[23;0t");
VERIFY_ARE_EQUAL(size_t{ 1 }, _testGetSet->_setWindowTitleCount);
VERIFY_ARE_EQUAL(std::wstring_view{ expectedTitle }, _testGetSet->GetWindowTitle());
}

Log::Comment(L"The 11th pop finds an empty stack, so the oldest title was dropped");
_testGetSet->_setWindowTitleCount = 0;
_stateMachine->ProcessString(L"\x1b[23;0t");
VERIFY_ARE_EQUAL(size_t{ 0 }, _testGetSet->_setWindowTitleCount);
const auto oldestKeptTitle = titleFor(2);
VERIFY_ARE_EQUAL(std::wstring_view{ oldestKeptTitle }, _testGetSet->GetWindowTitle());
}

TEST_METHOD(TestMouseModes)
{
Log::Comment(L"Starting test...");
Expand Down