From aaf5894ad9272698cfafeb8e299b925fb9554417 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 5 Aug 2022 18:26:37 +0200 Subject: grid: get rid of empty row at the bottom after reflowing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the window is resized and we reflow the text, we ended up inserting an empty row at the bottom. This happens whenever the actual last row has a hard linebreak (which almost always is the case); we then end the reflow with a line break, causing an extra, empty, row to be allocated and inserted. This patch fixes this by detecting when: 1) the last row is empty 2) the next to last row has a hard line break In this case, we roll back the last line break, by adjusting the new offset we just calculated, and free:ing the empty row. TODO: it would be nice if we could detect this in the reflow loop instead, and avoid doing the last line break all together. I haven’t yet been able to find a way to do this correctly. Closes #1108 --- grid.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 2790d16b..f40803e1 100644 --- a/grid.c +++ b/grid.c @@ -984,6 +984,26 @@ grid_resize_and_reflow( /* Set offset such that the last reflowed row is at the bottom */ grid->offset = new_row_idx - new_screen_rows + 1; + + if (new_col_idx == 0) { + int next_to_last_new_row_idx = new_row_idx - 1; + next_to_last_new_row_idx += new_rows; + next_to_last_new_row_idx &= new_rows - 1; + + const struct row *next_to_last_row = new_grid[next_to_last_new_row_idx]; + if (next_to_last_row != NULL && next_to_last_row->linebreak) { + /* + * The next to last row is actually the *last* row. But we + * ended the reflow with a line-break, causing an empty + * row to be inserted at the bottom. Undo this. + */ + /* TODO: can we detect this in the reflow loop above instead? */ + grid->offset--; + grid_row_free(new_grid[new_row_idx]); + new_grid[new_row_idx] = NULL; + } + } + while (grid->offset < 0) grid->offset += new_rows; while (new_grid[grid->offset] == NULL) -- cgit v1.3