Repository: microsoft/terminal
File: src/terminal/adapter/adaptDispatch.cpp (C++)

Code before:
// - True.
bool AdaptDispatch::DeleteLine(const VTInt distance)
{
    _InsertDeleteLineHelper(-gsl::narrow_cast<int32_t>(distance));
    return true;
}

// Routine Description:
// - Internal logic for adding or removing columns in the active screen buffer.
// Parameters:
// - delta - Number of columns to modify (positive if inserting, negative if deleting).
// Return Value:
// - <none>
void AdaptDispatch::_InsertDeleteColumnHelper(const VTInt delta)
{
    const auto viewport = _api.GetViewport();
    auto& textBuffer = _api.GetTextBuffer();
    const auto bufferWidth = textBuffer.GetSize().Width();

    const auto& cursor = textBuffer.GetCursor();
    const auto col = cursor.GetPosition().x;
    const auto row = cursor.GetPosition().y;

    const auto [topMargin, bottomMargin] = _GetVerticalMargins(viewport, true);
    const auto [leftMargin, rightMargin] = _GetHorizontalMargins(bufferWidth);
    if (row >= topMargin && row <= bottomMargin && col >= leftMargin && col <= rightMargin)
    {
        // We emulate inserting and deleting by scrolling the area between the cursor and the right margin.
        _ScrollRectHorizontally(textBuffer, { col, topMargin, rightMargin + 1, bottomMargin + 1 }, delta);
    }
}

// Routine Description:
// - DECIC - This control function inserts one or more blank columns in the
//    scrolling region, starting at the column that has the cursor.
// Arguments:
// - distance - number of columns to insert
// Return Value:
// - True.
bool AdaptDispatch::InsertColumn(const VTInt distance)
{
    _InsertDeleteColumnHelper(gsl::narrow_cast<int32_t>(distance));
    return true;
}

// Routine Description:
// - DECDC - This control function deletes one or more columns in the scrolling
//    region, starting with the column that has the cursor.
// Arguments:
// - distance - number of columns to delete
// Return Value:

Review comment (suggestion):
If we had a `_GetMargins` function we could use a `til::point` / `til::rect` check.
BTW I recently wrote a `range<T>` struct for AtlasEngine ([here](https://github.com/microsoft/terminal/blob/910c61b7e19319f3ddd3023c532542726e4e3b8d/src/renderer/atlas/common.h#L103-L125)) which has a `contains()` function. This might be useful for code like this too. (Although it would need to be split up into `range` and `inclusive_range` or something.)

Code after:
// - True.
bool AdaptDispatch::DeleteLine(const VTInt distance)
{
    _InsertDeleteLineHelper(-distance);
    return true;
}

// Routine Description:
// - Internal logic for adding or removing columns in the active screen buffer.
// Parameters:
// - delta - Number of columns to modify (positive if inserting, negative if deleting).
// Return Value:
// - <none>
void AdaptDispatch::_InsertDeleteColumnHelper(const VTInt delta)
{
    const auto viewport = _api.GetViewport();
    auto& textBuffer = _api.GetTextBuffer();
    const auto bufferWidth = textBuffer.GetSize().Width();

    const auto& cursor = textBuffer.GetCursor();
    const auto col = cursor.GetPosition().x;
    const auto row = cursor.GetPosition().y;

    const auto [topMargin, bottomMargin] = _GetVerticalMargins(viewport, true);
    const auto [leftMargin, rightMargin] = _GetHorizontalMargins(bufferWidth);
    if (row >= topMargin && row <= bottomMargin && col >= leftMargin && col <= rightMargin)
    {
        // We emulate inserting and deleting by scrolling the area between the cursor and the right margin.
        _ScrollRectHorizontally(textBuffer, { col, topMargin, rightMargin + 1, bottomMargin + 1 }, delta);
    }
}

// Routine Description:
// - DECIC - This control function inserts one or more blank columns in the
//    scrolling region, starting at the column that has the cursor.
// Arguments:
// - distance - number of columns to insert
// Return Value:
// - True.
bool AdaptDispatch::InsertColumn(const VTInt distance)
{
    _InsertDeleteColumnHelper(distance);
    return true;
}

// Routine Description:
// - DECDC - This control function deletes one or more columns in the scrolling
//    region, starting with the column that has the cursor.
// Arguments:
// - distance - number of columns to delete
// Return Value: