Skip to content

Commit

Permalink
Initial Implementation for tab stops in TerminalDispatch (microsoft#9597
Browse files Browse the repository at this point in the history
)

* [x] Supports microsoft#1883
* [X] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [X] Tests added/passed
  • Loading branch information
skyline75489 authored and mpela81 committed Apr 17, 2021
1 parent e1b6440 commit 5095120
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "../../terminal/adapter/DispatchTypes.hpp"
#include "../../buffer/out/TextAttribute.hpp"
#include "../../types/inc/Viewport.hpp"

namespace Microsoft::Terminal::Core
{
Expand All @@ -22,6 +23,7 @@ namespace Microsoft::Terminal::Core
virtual TextAttribute GetTextAttributes() const noexcept = 0;
virtual void SetTextAttributes(const TextAttribute& attrs) noexcept = 0;

virtual Microsoft::Console::Types::Viewport GetBufferSize() noexcept = 0;
virtual bool SetCursorPosition(short x, short y) noexcept = 0;
virtual COORD GetCursorPosition() noexcept = 0;
virtual bool SetCursorVisibility(const bool visible) noexcept = 0;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Microsoft::Terminal::Core::Terminal final :
bool ExecuteChar(wchar_t wch) noexcept override;
TextAttribute GetTextAttributes() const noexcept override;
void SetTextAttributes(const TextAttribute& attrs) noexcept override;
Microsoft::Console::Types::Viewport GetBufferSize() noexcept override;
bool SetCursorPosition(short x, short y) noexcept override;
COORD GetCursorPosition() noexcept override;
bool SetCursorVisibility(const bool visible) noexcept override;
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ void Terminal::SetTextAttributes(const TextAttribute& attrs) noexcept
_buffer->SetCurrentAttributes(attrs);
}

Viewport Terminal::GetBufferSize() noexcept
{
return _buffer->GetSize();
}

bool Terminal::SetCursorPosition(short x, short y) noexcept
try
{
Expand Down
114 changes: 112 additions & 2 deletions src/cascadia/TerminalCore/TerminalDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,74 @@ try
}
CATCH_LOG_RETURN_FALSE()

bool TerminalDispatch::HorizontalTabSet() noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto column = _terminalApi.GetCursorPosition().X;

_InitTabStopsForWidth(width);
_tabStopColumns.at(column) = true;
return true;
}

bool TerminalDispatch::ForwardTab(const size_t numTabs) noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto cursorPosition = _terminalApi.GetCursorPosition();
auto column = cursorPosition.X;
const auto row = cursorPosition.Y;
auto tabsPerformed = 0u;
_InitTabStopsForWidth(width);
while (column + 1 < width && tabsPerformed < numTabs)
{
column++;
if (til::at(_tabStopColumns, column))
{
tabsPerformed++;
}
}

return _terminalApi.SetCursorPosition(column, row);
}

bool TerminalDispatch::BackwardsTab(const size_t numTabs) noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto cursorPosition = _terminalApi.GetCursorPosition();
auto column = cursorPosition.X;
const auto row = cursorPosition.Y;
auto tabsPerformed = 0u;
_InitTabStopsForWidth(width);
while (column > 0 && tabsPerformed < numTabs)
{
column--;
if (til::at(_tabStopColumns, column))
{
tabsPerformed++;
}
}

return _terminalApi.SetCursorPosition(column, row);
}

bool TerminalDispatch::TabClear(const DispatchTypes::TabClearType clearType) noexcept
{
bool success = false;
switch (clearType)
{
case DispatchTypes::TabClearType::ClearCurrentColumn:
success = _ClearSingleTabStop();
break;
case DispatchTypes::TabClearType::ClearAllColumns:
success = _ClearAllTabStops();
break;
default:
success = false;
break;
}
return success;
}

// Method Description:
// - Sets a single entry of the colortable to a new value
// Arguments:
Expand Down Expand Up @@ -550,6 +618,48 @@ bool TerminalDispatch::_ModeParamsHelper(const DispatchTypes::ModeParams param,
return success;
}

bool TerminalDispatch::_ClearSingleTabStop() noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto column = _terminalApi.GetCursorPosition().X;

_InitTabStopsForWidth(width);
_tabStopColumns.at(column) = false;
return true;
}

bool TerminalDispatch::_ClearAllTabStops() noexcept
{
_tabStopColumns.clear();
_initDefaultTabStops = false;
return true;
}

void TerminalDispatch::_ResetTabStops() noexcept
{
_tabStopColumns.clear();
_initDefaultTabStops = true;
}

void TerminalDispatch::_InitTabStopsForWidth(const size_t width)
{
const auto initialWidth = _tabStopColumns.size();
if (width > initialWidth)
{
_tabStopColumns.resize(width);
if (_initDefaultTabStops)
{
for (auto column = 8u; column < _tabStopColumns.size(); column += 8)
{
if (column >= initialWidth)
{
til::at(_tabStopColumns, column) = true;
}
}
}
}
}

bool TerminalDispatch::SoftReset() noexcept
{
// TODO:GH#1883 much of this method is not yet implemented in the Terminal,
Expand Down Expand Up @@ -623,8 +733,8 @@ bool TerminalDispatch::HardReset() noexcept
// Cursor to 1,1 - the Soft Reset guarantees this is absolute
success = CursorPosition(1, 1) && success;

// // Delete all current tab stops and reapply
// _ResetTabStops();
// Delete all current tab stops and reapply
_ResetTabStops();

return success;
}
13 changes: 13 additions & 0 deletions src/cascadia/TerminalCore/TerminalDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class TerminalDispatch : public Microsoft::Console::VirtualTerminal::TermDispatc
bool CarriageReturn() noexcept override;
bool SetWindowTitle(std::wstring_view title) noexcept override;

bool HorizontalTabSet() noexcept override; // HTS
bool ForwardTab(const size_t numTabs) noexcept override; // CHT, HT
bool BackwardsTab(const size_t numTabs) noexcept override; // CBT
bool TabClear(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::TabClearType clearType) noexcept override; // TBC

bool SetColorTableEntry(const size_t tableIndex, const DWORD color) noexcept override;
bool SetCursorStyle(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::CursorStyle cursorStyle) noexcept override;
bool SetCursorColor(const DWORD color) noexcept override;
Expand Down Expand Up @@ -79,9 +84,17 @@ class TerminalDispatch : public Microsoft::Console::VirtualTerminal::TermDispatc
private:
::Microsoft::Terminal::Core::ITerminalApi& _terminalApi;

std::vector<bool> _tabStopColumns;
bool _initDefaultTabStops = true;

size_t _SetRgbColorsHelper(const ::Microsoft::Console::VirtualTerminal::VTParameters options,
TextAttribute& attr,
const bool isForeground) noexcept;

bool _ModeParamsHelper(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::ModeParams param, const bool enable) noexcept;

bool _ClearSingleTabStop() noexcept;
bool _ClearAllTabStops() noexcept;
void _ResetTabStops() noexcept;
void _InitTabStopsForWidth(const size_t width);
};
Loading

0 comments on commit 5095120

Please sign in to comment.