diff --git a/src/host/_stream.cpp b/src/host/_stream.cpp index e3b668464..f69c5b180 100644 --- a/src/host/_stream.cpp +++ b/src/host/_stream.cpp @@ -746,7 +746,6 @@ constexpr unsigned int LOCAL_BUFFER_SIZE = 100; if (screenInfo.InVTMode()) { const COORD cCursorOld = cursor.GetPosition(); - // Get Forward tab handles tabbing past the end of the buffer CursorPosition = screenInfo.GetForwardTab(cCursorOld); } else diff --git a/src/host/getset.cpp b/src/host/getset.cpp index 500d9c418..ca07a6ff1 100644 --- a/src/host/getset.cpp +++ b/src/host/getset.cpp @@ -1567,19 +1567,15 @@ void DoSrvPrivateUseMainScreenBuffer(SCREEN_INFORMATION& screenInfo) { CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation(); SCREEN_INFORMATION& _screenBuffer = gci.GetActiveOutputBuffer().GetActiveBuffer(); + COORD cursorPos = _screenBuffer.GetTextBuffer().GetCursor().GetPosition(); - NTSTATUS Status = STATUS_SUCCESS; FAIL_FAST_IF(!(sNumTabs >= 0)); - for (SHORT sTabsExecuted = 0; sTabsExecuted < sNumTabs && NT_SUCCESS(Status); sTabsExecuted++) + for (SHORT sTabsExecuted = 0; sTabsExecuted < sNumTabs; sTabsExecuted++) { - const COORD cursorPos = _screenBuffer.GetTextBuffer().GetCursor().GetPosition(); - COORD cNewPos = (fForward) ? _screenBuffer.GetForwardTab(cursorPos) : _screenBuffer.GetReverseTab(cursorPos); - // GetForwardTab is smart enough to move the cursor to the next line if - // it's at the end of the current one already. AdjustCursorPos shouldn't - // to be doing anything funny, just moving the cursor to the location GetForwardTab returns - Status = AdjustCursorPosition(_screenBuffer, cNewPos, TRUE, nullptr); + cursorPos = (fForward) ? _screenBuffer.GetForwardTab(cursorPos) : _screenBuffer.GetReverseTab(cursorPos); } - return Status; + + return AdjustCursorPosition(_screenBuffer, cursorPos, TRUE, nullptr); } // Routine Description: diff --git a/src/host/screenInfo.cpp b/src/host/screenInfo.cpp index 62ccadc55..e65666fb2 100644 --- a/src/host/screenInfo.cpp +++ b/src/host/screenInfo.cpp @@ -2211,12 +2211,7 @@ COORD SCREEN_INFORMATION::GetForwardTab(const COORD cCurrCursorPos) const noexce { COORD cNewCursorPos = cCurrCursorPos; SHORT sWidth = GetBufferSize().RightInclusive(); - if (cCurrCursorPos.X == sWidth) - { - cNewCursorPos.X = 0; - cNewCursorPos.Y += 1; - } - else if (_tabStops.empty() || cCurrCursorPos.X >= _tabStops.back()) + if (_tabStops.empty() || cCurrCursorPos.X >= _tabStops.back()) { cNewCursorPos.X = sWidth; } @@ -2227,7 +2222,8 @@ COORD SCREEN_INFORMATION::GetForwardTab(const COORD cCurrCursorPos) const noexce { if (*it > cCurrCursorPos.X) { - cNewCursorPos.X = *it; + // make sure we don't exceed the width of the buffer + cNewCursorPos.X = std::min(*it, sWidth); break; } } diff --git a/src/host/ut_host/ScreenBufferTests.cpp b/src/host/ut_host/ScreenBufferTests.cpp index 562fab328..25c266208 100644 --- a/src/host/ut_host/ScreenBufferTests.cpp +++ b/src/host/ut_host/ScreenBufferTests.cpp @@ -599,6 +599,19 @@ void ScreenBufferTests::TestGetForwardTab() L"Cursor advanced to end of screen buffer."); } + Log::Comment(L"Find next tab from rightmost column."); + { + coordCursor.X = coordScreenBufferSize.X - 1; + + COORD coordCursorExpected; + coordCursorExpected = coordCursor; + + COORD const coordCursorResult = si.GetForwardTab(coordCursor); + VERIFY_ARE_EQUAL(coordCursorExpected, + coordCursorResult, + L"Cursor remains in rightmost column."); + } + si._tabStops.clear(); }