Repository: microsoft/terminal
File: src/host/getset.cpp (C++)

Code before:
        auto Unlock = wil::scope_exit([&] { UnlockConsole(); });

        auto& g = ServiceLocator::LocateGlobals();
        auto& gci = g.getConsoleInformation();

        const auto coordScreenBufferSize = context.GetBufferSize().Dimensions();
        const auto requestedBufferSize = data.dwSize;
        if (requestedBufferSize.X != coordScreenBufferSize.X ||
            requestedBufferSize.Y != coordScreenBufferSize.Y)
        {
            auto& commandLine = CommandLine::Instance();

            commandLine.Hide(FALSE);

            LOG_IF_FAILED(context.ResizeScreenBuffer(data.dwSize, TRUE));

            commandLine.Show();
        }
        const auto newBufferSize = context.GetBufferSize().Dimensions();

        for (size_t i = 0; i < std::size(data.ColorTable); i++)
        {
            gci.SetLegacyColorTableEntry(i, data.ColorTable[i]);
        }

        context.SetDefaultAttributes(TextAttribute{ data.wAttributes }, TextAttribute{ data.wPopupAttributes });

        const auto requestedViewport = Viewport::FromExclusive(data.srWindow);

        auto NewSize = requestedViewport.Dimensions();
        // If we have a window, clamp the requested viewport to the max window size
        if (!ServiceLocator::LocateGlobals().IsHeadless())
        {
            NewSize.X = std::min(NewSize.X, data.dwMaximumWindowSize.X);
            NewSize.Y = std::min(NewSize.Y, data.dwMaximumWindowSize.Y);
        }

        // If wrap text is on, then the window width must be the same size as the buffer width
        if (gci.GetWrapText())
        {
            NewSize.X = newBufferSize.X;
        }

        if (NewSize.X != context.GetViewport().Width() ||
            NewSize.Y != context.GetViewport().Height())
        {
            // GH#1856 - make sure to hide the commandline _before_ we execute
            // the resize, and the re-display it after the resize. If we leave
            // it displayed, we'll crash during the resize when we try to figure
            // out if the bounds of the old commandline fit within the new
            // window (it might not).

Review comment (suggestion):
```cpp
changedOneTableEntry |= newColor != gci.GetColorTableEntry(i);
```
...works as well.

Code after:
        auto Unlock = wil::scope_exit([&] { UnlockConsole(); });

        auto& g = ServiceLocator::LocateGlobals();
        auto& gci = g.getConsoleInformation();

        const auto coordScreenBufferSize = context.GetBufferSize().Dimensions();
        const auto requestedBufferSize = data.dwSize;
        if (requestedBufferSize.X != coordScreenBufferSize.X ||
            requestedBufferSize.Y != coordScreenBufferSize.Y)
        {
            auto& commandLine = CommandLine::Instance();

            commandLine.Hide(FALSE);

            LOG_IF_FAILED(context.ResizeScreenBuffer(data.dwSize, TRUE));

            commandLine.Show();
        }
        const auto newBufferSize = context.GetBufferSize().Dimensions();

        bool changedOneTableEntry = false;
        for (size_t i = 0; i < std::size(data.ColorTable); i++)
        {
            // Check if we actually changed a palette color
            const auto& newColor{ data.ColorTable[i] };
            changedOneTableEntry = changedOneTableEntry || (newColor != gci.GetColorTableEntry(i));

            // Set the new value.
            gci.SetLegacyColorTableEntry(i, newColor);
        }

        // GH#399: Trigger a redraw, so that updated colors are repainted, but
        // only do this if we're not in conpty mode. ConPTY will update the
        // colors of the palette elsewhere (after TODO GH#10639)
        //
        // Only do this if we actually changed the value of the palette though -
        // this API gets called all the time to change all sorts of things, but
        // not necessarily the palette.
        if (changedOneTableEntry && !gci.IsInVtIoMode())
        {
            if (auto* pRender{ ServiceLocator::LocateGlobals().pRender })
            {
                pRender->TriggerRedrawAll();
            }
        }

        context.SetDefaultAttributes(TextAttribute{ data.wAttributes }, TextAttribute{ data.wPopupAttributes });

        const auto requestedViewport = Viewport::FromExclusive(data.srWindow);

        auto NewSize = requestedViewport.Dimensions();