Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
                continue;
            }
            // If this mark is before/after the start of our search in the buffer, ...
            auto inTheRightDirection = (goUp && (m.outputEnd < start)) || // prev
                                       (!goUp && (m.commandEnd > start)); // next
            // (If we're going down, we need to compare the commandEnd, not the
            // outputEnd, to actually find the next one. Otherwise we'll just
            // find the mark of the current selection again.)
            if (inTheRightDirection)
            {
                // .. and we either haven't found a match, or the current
                // nearest is after this mark in the buffer
                if (!nearest.has_value() ||
                    ((goUp && (*m.outputEnd > *nearest->outputEnd)) || // prev
                     (!goUp && (*m.commandEnd < *nearest->outputEnd)))) // next
                {
                    // stash this as the new match
                    nearest = m;
                }
            }
        }

        if (nearest.has_value())
        {
            auto start = *nearest->commandEnd;
            auto end = *nearest->outputEnd;

            const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
            bufferSize.DecrementInBounds(end);

            auto lock = _terminal->LockForWriting();
            _terminal->SelectNewRegion(start, end);
            _renderer->TriggerSelection();
        }
    }

    void ControlCore::ColorSelection(const Control::SelectionColor& fg, const Control::SelectionColor& bg, Core::MatchMode matchMode)
    {
        if (HasSelection())
        {
            const auto pForeground = winrt::get_self<implementation::SelectionColor>(fg);
            const auto pBackground = winrt::get_self<implementation::SelectionColor>(bg);

            TextColor foregroundAsTextColor;
            TextColor backgroundAsTextColor;

            if (pForeground)
            {
                foregroundAsTextColor = pForeground->AsTextColor();
            }


Review comment (suggestion):
```suggestion
            const auto start = *nearest->commandEnd;
            const auto end = *nearest->outputEnd;
```

Code after:
        const auto bufferSize = BufferHeight();

        // UserScrollViewport, to update the Terminal about where the viewport should be
        // then raise a _terminalScrollPositionChanged to inform the control to update the scrollbar.
        if (tgt.has_value())
        {
            UserScrollViewport(tgt->start.y);
            _terminalScrollPositionChanged(tgt->start.y, viewHeight, bufferSize);
        }
        else
        {
            if (direction == ScrollToMarkDirection::Last || direction == ScrollToMarkDirection::Next)
            {
                UserScrollViewport(BufferHeight());
                _terminalScrollPositionChanged(BufferHeight(), viewHeight, bufferSize);
            }
            else if (direction == ScrollToMarkDirection::First || direction == ScrollToMarkDirection::Previous)
            {
                UserScrollViewport(0);
                _terminalScrollPositionChanged(0, viewHeight, bufferSize);
            }
        }
    }

    void ControlCore::SelectCommand(const bool goUp)
    {
        const til::point start = HasSelection() ? (goUp ? _terminal->GetSelectionAnchor() : _terminal->GetSelectionEnd()) :
                                                  _terminal->GetTextBuffer().GetCursor().GetPosition();
        std::optional<DispatchTypes::ScrollMark> nearest{ std::nullopt };
        const auto& marks{ _terminal->GetScrollMarks() };

        // Early return so we don't have to check for the validity of `nearest` below after the loop exits.
        if (marks.empty())
        {
            return;
        }

        static constexpr til::point worst{ til::CoordTypeMax, til::CoordTypeMax };
        til::point bestDistance{ worst };

        for (const auto& m : marks)
        {
            if (!m.HasCommand())
            {
                continue;
            }

            const auto distance = goUp ? start - m.end : m.end - start;
            if ((distance > til::point{ 0, 0 }) && distance < bestDistance)
            {
                nearest = m;