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

VT sequence support for EraseInLine, EraseInDisplay, DeleteCharacter and InsertCharacter #2144

Merged
merged 5 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 20 additions & 5 deletions src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,30 +558,45 @@ bool TextBuffer::IncrementCircularBuffer()

//Routine Description:
// - Retrieves the position of the last non-space character on the final line of the text buffer.
// - By default, we search the entire buffer to find the last non-space character
//Arguments:
// - <none>
//Return Value:
// - Coordinate position in screen coordinates (offset coordinates, not array index coordinates).
COORD TextBuffer::GetLastNonSpaceCharacter() const
{
COORD coordEndOfText;
// Always search the whole buffer, by starting at the bottom.
coordEndOfText.Y = GetSize().BottomInclusive();
return GetLastNonSpaceCharacter(GetSize());
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
}

//Routine Description:
// - Retrieves the position of the last non-space character in the given viewport
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
// - This is basically an optimized version of GetLastNonSpaceCharacter(), and can be called when
// - we know the last character is within the given viewport (so we don't need to check the entire buffer)
//Arguments:
// - The viewport
//Return value:
// - Coordinate position (relative to the text buffer)
COORD TextBuffer::GetLastNonSpaceCharacter(const Microsoft::Console::Types::Viewport viewport) const
{
COORD coordEndOfText = { 0 };
// Search the given viewport by starting at the bottom.
coordEndOfText.Y = viewport.BottomInclusive();

const ROW* pCurrRow = &GetRowByOffset(coordEndOfText.Y);
// The X position of the end of the valid text is the Right draw boundary (which is one beyond the final valid character)
coordEndOfText.X = static_cast<short>(pCurrRow->GetCharRow().MeasureRight()) - 1;

// If the X coordinate turns out to be -1, the row was empty, we need to search backwards for the real end of text.
bool fDoBackUp = (coordEndOfText.X < 0 && coordEndOfText.Y > 0); // this row is empty, and we're not at the top
const auto viewportTop = viewport.Top();
bool fDoBackUp = (coordEndOfText.X < 0 && coordEndOfText.Y > viewportTop); // this row is empty, and we're not at the top
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
while (fDoBackUp)
{
coordEndOfText.Y--;
pCurrRow = &GetRowByOffset(coordEndOfText.Y);
// We need to back up to the previous row if this line is empty, AND there are more rows

coordEndOfText.X = static_cast<short>(pCurrRow->GetCharRow().MeasureRight()) - 1;
fDoBackUp = (coordEndOfText.X < 0 && coordEndOfText.Y > 0);
fDoBackUp = (coordEndOfText.X < 0 && coordEndOfText.Y > viewportTop);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, should this make sure that X is within viewportRight? Or something equivalent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like X will be -1 if there was no non-space text in the row (based on what MeasureRight) is doing, so I don't think its relative to where the viewport is

}

// don't allow negative results
Expand Down
1 change: 1 addition & 0 deletions src/buffer/out/textBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class TextBuffer final
bool IncrementCircularBuffer();

COORD GetLastNonSpaceCharacter() const;
COORD GetLastNonSpaceCharacter(const Microsoft::Console::Types::Viewport viewport) const;

Cursor& GetCursor();
const Cursor& GetCursor() const;
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ namespace Microsoft::Terminal::Core
virtual bool SetCursorPosition(short x, short y) = 0;
virtual COORD GetCursorPosition() = 0;

virtual bool DeleteCharacter(const unsigned int uiCount) = 0;
virtual bool InsertCharacter(const unsigned int uiCount) = 0;
virtual bool EraseCharacters(const unsigned int numChars) = 0;
virtual bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) = 0;
virtual bool EraseInDisplay(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) = 0;

virtual bool SetWindowTitle(std::wstring_view title) = 0;

Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class Microsoft::Terminal::Core::Terminal final :
bool ReverseText(bool reversed) override;
bool SetCursorPosition(short x, short y) override;
COORD GetCursorPosition() override;
bool DeleteCharacter(const unsigned int uiCount) override;
bool InsertCharacter(const unsigned int uiCount) override;
bool EraseCharacters(const unsigned int numChars) override;
bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) override;
bool EraseInDisplay(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) override;
bool SetWindowTitle(std::wstring_view title) override;
bool SetColorTableEntry(const size_t tableIndex, const COLORREF dwColor) override;
bool SetCursorStyle(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::CursorStyle cursorStyle) override;
Expand Down
227 changes: 226 additions & 1 deletion src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include "Terminal.hpp"
#include "../src/inc/unicode.hpp"

using namespace Microsoft::Terminal::Core;
using namespace Microsoft::Console::Types;
Expand Down Expand Up @@ -126,17 +127,241 @@ COORD Terminal::GetCursorPosition()
return newPos;
}

// Method Description:
// - deletes uiCount characters starting from the cursor's current position
// - it moves over the remaining text to 'replace' the deleted text
// - for example, if the buffer looks like this ('|' is the cursor): [abc|def]
// - calling DeleteCharacter(1) will change it to: [abc|ef],
// - i.e. the 'd' gets deleted and the 'ef' gets shifted over 1 space and **retain their previous text attributes**
// Arguments:
// - uiCount, the number of characters to delete
// Return value:
// - true if succeeded, false otherwise
bool Terminal::DeleteCharacter(const unsigned int uiCount)
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
SHORT dist;
if (!SUCCEEDED(UIntToShort(uiCount, &dist)))
{
return false;
}
const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto copyToPos = cursorPos;
const COORD copyFromPos{ cursorPos.X + dist, cursorPos.Y };
auto sourceWidth = _mutableViewport.RightExclusive() - copyFromPos.X;
SHORT width;
if (!SUCCEEDED(UIntToShort(sourceWidth, &width)))
{
return false;
}

// Get a rectangle of the source
auto source = Viewport::FromDimensions(copyFromPos, width, 1);

// Get a rectangle of the target
const auto target = Viewport::FromDimensions(copyToPos, source.Dimensions());
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
const auto walkDirection = Viewport::DetermineWalkDirection(source, target);

auto sourcePos = source.GetWalkOrigin(walkDirection);
auto targetPos = target.GetWalkOrigin(walkDirection);

// Iterate over the source cell data and copy it over to the target
do
{
const auto data = OutputCell(*(_buffer->GetCellDataAt(sourcePos)));
_buffer->Write(OutputCellIterator({ &data, 1 }), targetPos);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
} while (source.WalkInBounds(sourcePos, walkDirection) && target.WalkInBounds(targetPos, walkDirection));

return true;
}

// Method Description:
// - Inserts uiCount spaces starting from the cursor's current position, moving over the existing text
// - for example, if the buffer looks like this ('|' is the cursor): [abc|def]
// - calling InsertCharacter(1) will change it to: [abc| def],
// - i.e. the 'def' gets shifted over 1 space and **retain their previous text attributes**
// Arguments:
// - uiCount, the number of spaces to insert
// Return value:
// - true if succeeded, false otherwise
bool Terminal::InsertCharacter(const unsigned int uiCount)
{
// NOTE: the code below is _extremely_ similar to DeleteCharacter
// We will want to use this same logic and implement a helper function instead
// that does the 'move a region from here to there' operation
SHORT dist;
if (!SUCCEEDED(UIntToShort(uiCount, &dist)))
{
return false;
}
const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto copyFromPos = cursorPos;
const COORD copyToPos{ cursorPos.X + dist, cursorPos.Y };
auto sourceWidth = _mutableViewport.RightExclusive() - copyFromPos.X;
SHORT width;
if (!SUCCEEDED(UIntToShort(sourceWidth, &width)))
{
return false;
}

// Get a rectangle of the source
auto source = Viewport::FromDimensions(copyFromPos, width, 1);
const auto sourceOrigin = source.Origin();

// Get a rectangle of the target
const auto target = Viewport::FromDimensions(copyToPos, source.Dimensions());
const auto walkDirection = Viewport::DetermineWalkDirection(source, target);

auto sourcePos = source.GetWalkOrigin(walkDirection);
auto targetPos = target.GetWalkOrigin(walkDirection);

// Iterate over the source cell data and copy it over to the target
do
{
const auto data = OutputCell(*(_buffer->GetCellDataAt(sourcePos)));
_buffer->Write(OutputCellIterator({ &data, 1 }), targetPos);
} while (source.WalkInBounds(sourcePos, walkDirection) && target.WalkInBounds(targetPos, walkDirection));
auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), dist);
_buffer->Write(eraseIter, cursorPos);

return true;
}

bool Terminal::EraseCharacters(const unsigned int numChars)
{
const auto absoluteCursorPos = _buffer->GetCursor().GetPosition();
const auto viewport = _GetMutableViewport();
const short distanceToRight = viewport.RightExclusive() - absoluteCursorPos.X;
const short fillLimit = std::min(static_cast<short>(numChars), distanceToRight);
auto eraseIter = OutputCellIterator(L' ', _buffer->GetCurrentAttributes(), fillLimit);
auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), fillLimit);
_buffer->Write(eraseIter, absoluteCursorPos);
return true;
}

// Method description:
// - erases a line of text, either from
// 1. beginning to the cursor's position
// 2. cursor's position to end
// 3. beginning to end
// - depending on the erase type
// Arguments:
// - the erase type
// Return value:
// - true if succeeded, false otherwise
bool Terminal::EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType)
{
const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto viewport = _GetMutableViewport();
COORD startPos = { 0 };
startPos.Y = cursorPos.Y;
// nlength determines the number of spaces we need to write
DWORD nlength = 0;

// Determine startPos.X and nlength by the eraseType
switch (eraseType)
{
case DispatchTypes::EraseType::FromBeginning:
nlength = cursorPos.X - viewport.Left() + 1;
break;
case DispatchTypes::EraseType::ToEnd:
startPos.X = cursorPos.X;
nlength = viewport.RightInclusive() - startPos.X;
break;
case DispatchTypes::EraseType::All:
startPos.X = viewport.Left();
nlength = viewport.RightInclusive() - startPos.X;
break;
case DispatchTypes::EraseType::Scrollback:
return false;
}
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), nlength);
_buffer->Write(eraseIter, startPos);
return true;
}

// Method description:
// - erases text in the buffer in two ways depending on erase type
// 1. 'erases' all text visible to the user (i.e. the text in the viewport)
// 2. erases all the text in the scrollback
// Arguments:
// - the erase type
// Return Value:
// - true if succeeded, false otherwise
bool Terminal::EraseInDisplay(const DispatchTypes::EraseType eraseType)
{
// Store the relative cursor position so we can restore it later after we move the viewport
const auto cursorPos = _buffer->GetCursor().GetPosition();
auto relativeCursor = cursorPos;
_mutableViewport.ConvertToOrigin(&relativeCursor);

// Initialize the new location of the viewport
// the top and bottom parameters are determined by the eraseType
SMALL_RECT newWin;
newWin.Left = _mutableViewport.Left();
newWin.Right = _mutableViewport.RightExclusive();

if (eraseType == DispatchTypes::EraseType::All)
{
// In this case, we simply move the viewport down, effectively pushing whatever text was on the screen into the scrollback
// and thus 'erasing' the text visible to the user
const auto coordLastChar = _buffer->GetLastNonSpaceCharacter(_mutableViewport);
if (coordLastChar.X == 0 && coordLastChar.Y == 0)
{
// Nothing to clear, just return
return true;
}

short sNewTop = coordLastChar.Y + 1;

// Increment the circular buffer only if the new location of the viewport would be 'below' the buffer
const short delta = (sNewTop + _mutableViewport.Height()) - (_buffer->GetSize().Height());
for (auto i = 0; i < delta; i++)
{
_buffer->IncrementCircularBuffer();
sNewTop--;
}

newWin.Top = sNewTop;
newWin.Bottom = sNewTop + _mutableViewport.Height();
}
else if (eraseType == DispatchTypes::EraseType::Scrollback)
{
// We only want to erase the scrollback, and leave everything else on the screen as it is
// so we grab the text in the viewport and rotate it up to the top of the buffer
COORD scrollFromPos{ 0, 0 };
_mutableViewport.ConvertFromOrigin(&scrollFromPos);
_buffer->ScrollRows(scrollFromPos.Y, _mutableViewport.Height(), -scrollFromPos.Y);

// Since we only did a rotation, the text that was in the scrollback is now _below_ where we are going to move the viewport
// and we have to make sure we erase that text
auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), _mutableViewport.RightInclusive());
auto eraseStart = _mutableViewport.Height();
auto eraseEnd = _buffer->GetLastNonSpaceCharacter(_mutableViewport).Y;
for (SHORT i = eraseStart; i <= eraseEnd; i++)
{
COORD erasePos{ 0, i };
_buffer->Write(eraseIter, erasePos);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
}

// Reset the scroll offset now because there's nothing for the user to 'scroll' to
_scrollOffset = 0;

newWin.Top = 0;
newWin.Bottom = _mutableViewport.Height();
}
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
else
{
return false;
}

// Move the viewport, adjust the scoll bar if needed, and restore the old cursor position
_mutableViewport = Viewport::FromExclusive(newWin);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
Terminal::_NotifyScrollEvent();
SetCursorPosition(relativeCursor.X, relativeCursor.Y);

return true;
}

bool Terminal::SetWindowTitle(std::wstring_view title)
{
_title = title;
Expand Down
Loading