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

Code before:
        }

        return hstring{ str };
    }

    // Get all of our recent commands. This will only really work if the user has enabled shell integration.
    Control::CommandHistoryContext ControlCore::CommandHistory() const
    {
        auto terminalLock = _terminal->LockForWriting();
        auto context = winrt::make_self<CommandHistoryContext>();
        const auto& textBuffer = _terminal->GetTextBuffer();

        for (const auto& mark : _terminal->GetScrollMarks())
        {
            // The command text is between the `end` (which denotes the end of
            // the prompt) and the `commandEnd`.
            bool markHasCommand = mark.commandEnd.has_value() &&
                                  mark.commandEnd != mark.end;
            if (!markHasCommand)
                continue;

            // Get the text of the command
            const auto line = mark.end.y;
            const auto& row = textBuffer.GetRowByOffset(line);
            const auto rowText = row.GetText();
            const auto commandText = rowText.substr(mark.end.x, mark.commandEnd->x - mark.end.x);

            // Trim off trailing spaces.
            const auto strEnd = commandText.find_last_not_of(UNICODE_SPACE);
            if (strEnd != std::string::npos)
            {
                const auto trimmed = commandText.substr(0, strEnd + 1);
                context->History().Append(winrt::hstring{ trimmed });
            }
        }
        return *context;
    }

    Core::Scheme ControlCore::ColorScheme() const noexcept
    {
        Core::Scheme s;

        // This part is definitely a hack.
        //
        // This function is usually used by the "Preview Color Scheme"
        // functionality in TerminalPage. If we've got an unfocused appearance,
        // then we've applied that appearance before this is even getting called
        // (because the command palette is open with focus on top of us). If we
        // return the _current_ colors now, we'll return out the _unfocused_
        // colors. If we do that, and the user dismisses the command palette,
        // then the scheme that will get restored is the _unfocused_ one, which

Review comment (performance):
@lhecker This is the original place where I should be doing something better. Something kinda like what you're doing in `ROW::CopyRangeFrom`? Like

```c++
auto ROW::GetText(til::CoordType columnBegin, til::CoordType columnLimit)
{
    const auto otherColBeg = other._clampedColumnInclusive(otherBegin);
    const auto otherColLimit = other._clampedColumnInclusive(otherLimit);
    std::span<uint16_t> charOffsets;
    std::wstring_view chars;

    if (otherColBeg < otherColLimit)
    {
        charOffsets = other._charOffsets.subspan(otherColBeg, static_cast<size_t>(otherColLimit) - otherColBeg + 1);
        const auto charsOffset = charOffsets.front() & CharOffsetsMask;
        // We _are_ using span. But C++ decided that string_view and span aren't convertible.
        // _chars is a std::span for performance and because it refers to raw, shared memory.
#pragma warning(suppress : 26481) // Don't use pointer arithmetic. Use span instead (bounds.1).
        chars = { other._chars.data() + charsOffset, other._chars.size() - charsOffset };
    }
    
    return chars;
}

```

?

Code after:
            // If we are handling a double / triple-click or shift+single click
            // we establish selection using the selected mode
            // (expand both "start" and "end" selection points)
            _terminal->MultiClickSelection(terminalPosition, mode);
            selectionNeedsToBeCopied = true;
        }
        else if (_settings->RepositionCursorWithMouse()) // This is also mode==Char && !shiftEnabled
        {
            // If we're handling a single left click, without shift pressed, and
            // outside mouse mode, AND the user has RepositionCursorWithMouse turned
            // on, let's try to move the cursor.
            //
            // We'll only move the cursor if the user has clicked after the last
            // mark, if there is one. That means the user also needs to set up
            // shell integration to enable this feature.
            //
            // As noted in GH #8573, there's plenty of edge cases with this
            // approach, but it's good enough to bring value to 90% of use cases.
            const auto cursorPos{ _terminal->GetCursorPosition() };

            // Does the current buffer line have a mark on it?
            const auto& marks{ _terminal->GetScrollMarks() };
            if (!marks.empty())
            {
                const auto& last{ marks.back() };
                const auto [start, end] = last.GetExtent();
                const auto lastNonSpace = _terminal->GetTextBuffer().GetLastNonSpaceCharacter();

                // If the user clicked off to the right side of the prompt, we
                // want to send keystrokes to the last character in the prompt +1.
                //
                // We don't want to send too many here. In CMD, if the user's
                // last command is longer than what they've currently typed, and
                // they press right arrow at the end of the prompt, COOKED_READ
                // will fill in characters from the previous command.
                //
                // By only sending keypresses to the end of the command + 1, we
                // should leave the cursor at the very end of the prompt,
                // without adding any characters from a previous command.
                auto clampedClick = terminalPosition;
                if (terminalPosition > lastNonSpace)
                {
                    clampedClick = lastNonSpace + til::point{ 1, 0 };
                    _terminal->GetTextBuffer().GetSize().Clamp(clampedClick);
                }

                if (clampedClick >= end)
                {
                    // Get the distance between the cursor and the click, in cells.
                    const auto bufferSize = _terminal->GetTextBuffer().GetSize();