From 86a486934ed66ab28c3f794ef37c0a69b4e29a83 Mon Sep 17 00:00:00 2001 From: Carlos Alves Date: Wed, 11 Nov 2020 17:43:00 -0300 Subject: [PATCH] Reflow lines when Terminal resizes: both ways An attempt to resize on both ways: shrink and expand. Still thinking on how to handle _history --- src/Screen.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Screen.cpp b/src/Screen.cpp index 5e9ceab5..2cf50a9e 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -384,43 +384,55 @@ void Screen::resizeImage(int new_lines, int new_columns) } } - if (new_columns < _columns) { + if (new_columns != _columns) { + // Random not printable char to mark nextline + const auto NextLine = Character(130, + CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR), + CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR), + DEFAULT_RENDITION, + false); QVector images; // First copy everything. for (int i = 0; i < qMin(_lines, new_lines + 1) ; i++) { + // if the line have the 'NextLine' char, concat with the next line before push_back. + if (_screenLines[i].count() && _screenLines[i].at(_screenLines[i].count() - 1) == NextLine) { + _screenLines[i].pop_back(); + _screenLines[i].append(_screenLines[i + 1]); + _screenLines[i + 1] = _screenLines[i]; + _cuY--; + continue; + } images.push_back(_screenLines[i]); } // Then move the data to lines below. int currentPos = 0; while (currentPos != images.count()) { - - // If we are in the last line, append a new one. const bool shouldCopy = images[currentPos].size() > new_columns; - if (shouldCopy && images.count() - 1 == currentPos) { - images.append(ImageLine{}); - } - // Now copy from the current line, to the next one. + // Copy from the current line, to the next one. if (shouldCopy) { + // If we are in the last line, append a new one. + if (currentPos == images.count() - 1) { + images.append(ImageLine{}); + } + auto values = images[currentPos].mid(new_columns); images[currentPos].remove(new_columns, values.size()); - values.append(images[currentPos+1]); - images[currentPos+1] = values; + images[currentPos].append(NextLine); + images.insert(currentPos + 1, values); + _cuY++; } currentPos += 1; } // Now set the correct image based on the moved lines. auto newScreenLines = new ImageLine[new_lines + 1]; - for (int i = 0; i < qMin(_lines, new_lines + 1) ; i++) { + for (int i = 0; i < qMin(images.count(), new_lines + 1) ; i++) { newScreenLines[i] = images[i]; } delete[] _screenLines; _screenLines = newScreenLines; - } else if (new_columns > _columns) { - - } else { auto newScreenLines = new ImageLine[new_lines + 1]; for (int i = 0; i < qMin(_lines, new_lines + 1) ; i++) {