Repository: microsoft/terminal
File: src/buffer/out/textBuffer.cpp (C++)

Code before:
        // FOR DISCUSSION: Maybe also try to leave a
        // default-attributes-to-the-end attr here, for the case where the line
        // had color, then flowed onto the subsequent row.

        // If we found the old row that the caller was interested in, set the
        // out value of that parameter to the cursor's current Y position (the
        // new location of the _end_ of that row in the buffer).
        if (positionInfo.has_value())
        {
            if (!foundOldMutable)
            {
                if (iOldRow >= positionInfo.value().get().mutableViewportTop)
                {
                    positionInfo.value().get().mutableViewportTop = newCursor.GetPosition().Y;
                    foundOldMutable = true;
                }
            }

            if (!foundOldVisible)
            {
                if (iOldRow >= positionInfo.value().get().visibleViewportTop)
                {
                    positionInfo.value().get().visibleViewportTop = newCursor.GetPosition().Y;
                    foundOldVisible = true;
                }
            }
        }

        if (SUCCEEDED(hr))
        {
            // If we didn't have a full row to copy, insert a new
            // line into the new buffer.
            // Only do so if we were not forced to wrap. If we did
            // force a word wrap, then the existing line break was
            // only because we ran out of space.
            if (iRight < cOldColsTotal && !row.WasWrapForced())
            {
                if (iRight == cOldCursorPos.X && iOldRow == cOldCursorPos.Y)
                {
                    cNewCursorPos = newCursor.GetPosition();
                    fFoundCursorPos = true;
                }
                // Only do this if it's not the final line in the buffer.
                // On the final line, we want the cursor to sit
                // where it is done printing for the cursor
                // adjustment to follow.
                if (iOldRow < cOldRowsTotal - 1)
                {
                    hr = newBuffer.NewlineCursor() ? hr : E_OUTOFMEMORY;
                }
                else

Review comment (suggestion):
Add a comment explaining "we already copied all text with the attribute color, but it's possible for someone to just put some color into the buffer to the right of that without any text. the buffer looks weird to the user when we resize and it starts losing those colors, so we need to copy them over too... as long as there is space."

Code after:
                fFoundCursorPos = true;
            }

            try
            {
                // TODO: MSFT: 19446208 - this should just use an iterator and the inserter...
                const auto glyph = chars->Char();
                const auto dbcsAttr = chars->DbcsAttr();
                const auto textAttr = *attrs;

                if (!newBuffer.InsertCharacter(glyph, dbcsAttr, textAttr))
                {
                    hr = E_OUTOFMEMORY;
                    break;
                }
            }
            CATCH_RETURN();

            ++chars;
            ++attrs;
        }

        // GH#32: Copy the attributes from the rest of the row into this new buffer.
        // From where we are in the old buffer, to the end of the row, copy the
        // remaining attributes.
        // - if the old buffer is smaller than the new buffer, then just copy
        //   what we have, as it was. We already copied all _text_ with colors,
        //   but it's possible for someone to just put some color into the
        //   buffer to the right of that without any text (as just spaces). The
        //   buffer looks weird to the user when we resize and it starts losing
        //   those colors, so we need to copy them over too... as long as there
        //   is space. The last attr in the row will be extended to the end of
        //   the row in the new buffer.
        // - if the old buffer is WIDER, than we might have wrapped onto a new
        //   line. Use the cursor's position's Y so that we know where the new
        //   row is, and start writing at the cursor position. Again, the attr
        //   in the last column of the old row will be extended to the end of the
        //   row that the text was flowed onto.
        //   - if the text in the old buffer didn't actually fill the whole
        //     line in the new buffer, then we didn't wrap. That's fine. just
        //     copy attributes from the old row till the end of the new row, and
        //     move on.
        const auto newRowY = newCursor.GetPosition().Y;
        auto& newRow = newBuffer.GetRowByOffset(newRowY);
        auto newAttrColumn = newCursor.GetPosition().X;
        const auto newWidth = newBuffer.GetLineWidth(newRowY);
        // Stop when we get to the end of the buffer width, or the new position
        // for inserting an attr would be past the right of the new buffer.
        for (short copyAttrCol = iOldCol;
             copyAttrCol < cOldColsTotal && newAttrColumn < newWidth;
             copyAttrCol++)