bool ConsoleLine::detectChangeAndSetLine()

in src/agent/ConsoleLine.cc [79:136]


bool ConsoleLine::detectChangeAndSetLine(const CHAR_INFO *const line, const int newLength)
{
    ASSERT(newLength >= 1);
    ASSERT(m_prevLength <= static_cast<int>(m_prevData.size()));

    if (newLength == m_prevLength) {
        bool equalLines = areLinesEqual(m_prevData.data(), line, newLength);
        if (!equalLines) {
            setLine(line, newLength);
        }
        return !equalLines;
    } else {
        if (m_prevLength == 0) {
            setLine(line, newLength);
            return true;
        }

        ASSERT(m_prevLength >= 1);
        const WORD prevBlank = m_prevData[m_prevLength - 1].Attributes;
        const WORD newBlank = line[newLength - 1].Attributes;

        bool equalLines = false;
        if (newLength < m_prevLength) {
            // The line has become shorter.  The lines are equal if the common
            // part is equal, and if the newly truncated characters were blank.
            equalLines =
                areLinesEqual(m_prevData.data(), line, newLength) &&
                isLineBlank(m_prevData.data() + newLength,
                            m_prevLength - newLength,
                            newBlank);
        } else {
            //
            // The line has become longer.  The lines are equal if the common
            // part is equal, and if both the extra characters and any
            // potentially reexposed characters are blank.
            //
            // Two of the most relevant terminals for winpty--mintty and
            // jediterm--don't (currently) erase the obscured content when a
            // line is cleared, so we should anticipate its existence when
            // making a terminal wider and reoutput the line.  See:
            //
            //  * https://github.com/mintty/mintty/issues/480
            //  * https://github.com/JetBrains/jediterm/issues/118
            //
            ASSERT(newLength > m_prevLength);
            equalLines =
                areLinesEqual(m_prevData.data(), line, m_prevLength) &&
                isLineBlank(m_prevData.data() + m_prevLength,
                            std::min<int>(m_prevData.size(), newLength) - m_prevLength,
                            prevBlank) &&
                isLineBlank(line + m_prevLength,
                            newLength - m_prevLength,
                            prevBlank);
        }
        setLine(line, newLength);
        return !equalLines;
    }
}