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 3 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
22 changes: 17 additions & 5 deletions src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,24 +564,36 @@ bool TextBuffer::IncrementCircularBuffer()
// - 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
//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
3 changes: 3 additions & 0 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ 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 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
3 changes: 3 additions & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ 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 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
175 changes: 175 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,56 @@ 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;
COORD copyFromPos{ cursorPos.X + dist, cursorPos.Y };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
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, (SHORT)1);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
const auto sourceOrigin = source.Origin();
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

// 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).operator*());
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
_buffer->Write(OutputCellIterator({ &data, 1 }), targetPos);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

source.WalkInBounds(sourcePos, walkDirection);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
} while (target.WalkInBounds(targetPos, walkDirection));

return true;
}

bool Terminal::EraseCharacters(const unsigned int numChars)
{
const auto absoluteCursorPos = _buffer->GetCursor().GetPosition();
Expand All @@ -137,6 +187,131 @@ bool Terminal::EraseCharacters(const unsigned int numChars)
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(L' ', _buffer->GetCurrentAttributes(), nlength);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
_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
short delta = (sNewTop + _mutableViewport.Height()) - (_buffer->GetSize().Height());
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
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(L' ', _buffer->GetCurrentAttributes(), _mutableViewport.RightInclusive());
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
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
40 changes: 36 additions & 4 deletions src/cascadia/TerminalCore/TerminalDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ bool TerminalDispatch::CursorForward(const unsigned int uiDistance)
return _terminalApi.SetCursorPosition(newCursorPos.X, newCursorPos.Y);
}

bool TerminalDispatch::CursorUp(const unsigned int uiDistance)
{
const auto cursorPos = _terminalApi.GetCursorPosition();
const COORD newCursorPos{ cursorPos.X, cursorPos.Y + gsl::narrow<short>(uiDistance) };
return _terminalApi.SetCursorPosition(newCursorPos.X, newCursorPos.Y);
}

bool TerminalDispatch::EraseCharacters(const unsigned int uiNumChars)
{
return _terminalApi.EraseCharacters(uiNumChars);
Expand Down Expand Up @@ -98,9 +105,34 @@ bool TerminalDispatch::SetDefaultBackground(const DWORD dwColor)
}

// Method Description:
// - For now, this is a hacky backspace
// - TODO: GitHub #1883
bool TerminalDispatch::EraseInLine(const DispatchTypes::EraseType)
// - Erases characters in the buffer depending on the erase type
// Arguments:
// - eraseType: the erase type (from beginning, to end, or all)
// Return Value:
// True if handled successfully. False otherwise.
bool TerminalDispatch::EraseInLine(const DispatchTypes::EraseType eraseType)
{
return _terminalApi.EraseInLine(eraseType);
}

// Method Description:
// - Deletes uiCount number of characters starting from where the cursor is currently
// Arguments:
// - uiCount, the number of characters to delete
// Return Value:
// True if handled successfully. False otherwise.
bool TerminalDispatch::DeleteCharacter(const unsigned int uiCount)
{
return _terminalApi.DeleteCharacter(uiCount);
}

// Method Description:
// - Moves the viewport and erases text from the buffer depending on the eraseType
// Arguments:
// - eraseType: the desired erase type
// Return Value:
// True if handled successfully. False otherwise
bool TerminalDispatch::EraseInDisplay(const DispatchTypes::EraseType eraseType)
{
return _terminalApi.EraseCharacters(1);
return _terminalApi.EraseInDisplay(eraseType);
}
5 changes: 4 additions & 1 deletion src/cascadia/TerminalCore/TerminalDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TerminalDispatch : public Microsoft::Console::VirtualTerminal::TermDispatc
const unsigned int uiColumn) override; // CUP

bool CursorForward(const unsigned int uiDistance) override;
bool CursorUp(const unsigned int uiDistance) override;

bool EraseCharacters(const unsigned int uiNumChars) override;
bool SetWindowTitle(std::wstring_view title) override;
Expand All @@ -29,7 +30,9 @@ class TerminalDispatch : public Microsoft::Console::VirtualTerminal::TermDispatc

bool SetDefaultForeground(const DWORD dwColor) override;
bool SetDefaultBackground(const DWORD dwColor) override;
bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType /* eraseType*/) override; // ED
bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) override; // ED
bool DeleteCharacter(const unsigned int uiCount) override;
bool EraseInDisplay(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) override;

private:
::Microsoft::Terminal::Core::ITerminalApi& _terminalApi;
Expand Down