mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-16 22:05:21 -05:00
term: convert cell 'linefeed' attribute to a row 'linebreak' property
To do text reflow, we only need to know if a line has been explicitly linebreaked or not. If not, that means it wrapped, and that we should *not* insert a linebreak when reflowing text. When reflowing text, when reaching the end of a row in the old grid, only insert a linebreak in the new grid if the old row had been explicitly linebreaked. Furthermore, when reflowing text and wrapping a row in the new grid, mark the previous row as linebreaked if either the last cell was (the last column in the last row) empty, or the current cell (the first column in the new row) is empty. If both are non-empty, then we assume a linewrap.
This commit is contained in:
parent
60056fdd61
commit
ce8005545d
5 changed files with 30 additions and 42 deletions
58
render.c
58
render.c
|
|
@ -1009,17 +1009,16 @@ reflow(struct terminal *term, struct row **new_grid, int new_cols, int new_rows,
|
|||
|
||||
/* Walk current line of the old grid */
|
||||
for (int c = 0; c < old_cols; c++) {
|
||||
const struct cell *old_cell = &old_row->cells[c];
|
||||
|
||||
if (old_cell->wc == 0) {
|
||||
if (old_row->cells[c].wc == 0) {
|
||||
empty_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
assert(old_cell->wc != 0);
|
||||
|
||||
/* Non-empty cell. Emit preceeding string of empty cells,
|
||||
* and possibly line break for current cell */
|
||||
int old_cols_left = old_cols - c;
|
||||
int cols_needed = empty_count + old_cols_left;
|
||||
int new_cols_left = new_cols - new_col_idx;
|
||||
if (new_cols_left < cols_needed && new_cols_left >= old_cols_left)
|
||||
empty_count = max(0, empty_count - (cols_needed - new_cols_left));
|
||||
|
||||
for (int i = 0; i < empty_count + 1; i++) {
|
||||
if (new_col_idx >= new_cols) {
|
||||
|
|
@ -1036,32 +1035,30 @@ reflow(struct terminal *term, struct row **new_grid, int new_cols, int new_rows,
|
|||
new_row->dirty = true;
|
||||
}
|
||||
|
||||
assert(new_row != NULL);
|
||||
assert(new_col_idx >= 0);
|
||||
assert(new_col_idx < new_cols);
|
||||
|
||||
const struct cell *old_cell = &old_row->cells[c - empty_count + i];
|
||||
|
||||
if (new_col_idx == 0 && new_row_idx > 0 &&
|
||||
(new_grid[new_row_idx - 1]->cells[new_cols - 1].wc == 0 ||
|
||||
old_cell->wc == 0))
|
||||
{
|
||||
new_grid[new_row_idx - 1]->linebreak = true;
|
||||
}
|
||||
|
||||
new_row->cells[new_col_idx] = *old_cell;
|
||||
new_row->cells[new_col_idx].attrs.clean = 1;
|
||||
new_col_idx++;
|
||||
}
|
||||
|
||||
empty_count = 0;
|
||||
new_col_idx--;
|
||||
|
||||
assert(new_row != NULL);
|
||||
assert(new_col_idx >= 0);
|
||||
assert(new_col_idx < new_cols);
|
||||
|
||||
/* Copy current cell */
|
||||
new_row->cells[new_col_idx].attrs.clean = 1;
|
||||
new_row->cells[new_col_idx++] = *old_cell;
|
||||
}
|
||||
|
||||
/*
|
||||
* If last cell of the old grid's line is empty, then we
|
||||
* insert a linebreak in the new grid's line too. Unless, the
|
||||
* *entire* old line was empty.
|
||||
*/
|
||||
if (old_row->linebreak) {
|
||||
new_row->linebreak = true;
|
||||
|
||||
if (empty_count < old_cols &&
|
||||
//r < old_rows - 1 &&
|
||||
(old_row->cells[old_cols - 1].wc == 0 ||
|
||||
old_row->cells[old_cols - 1].attrs.linefeed))
|
||||
{
|
||||
new_col_idx = 0;
|
||||
new_row_idx = (new_row_idx + 1) & (new_rows - 1);
|
||||
|
||||
|
|
@ -1237,19 +1234,12 @@ maybe_resize(struct terminal *term, int width, int height, bool force)
|
|||
while (cursor_row < 0)
|
||||
cursor_row += term->grid->num_rows;
|
||||
|
||||
/* Heuristic to prevent a new prompt from being printed a new line */
|
||||
bool do_linefeed = false;
|
||||
if (term->cursor.point.col > 0)
|
||||
cursor_row--;
|
||||
else if (cursor_row >= term->rows)
|
||||
do_linefeed = true;
|
||||
|
||||
term_cursor_to(
|
||||
term,
|
||||
min(max(cursor_row, 0), term->rows - 1),
|
||||
min(term->cursor.point.col, term->cols - 1));
|
||||
|
||||
if (do_linefeed)
|
||||
if (cursor_row >= term->rows)
|
||||
term_linefeed(term);
|
||||
|
||||
term->render.last_cursor.cell = NULL;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue