osc: add support for OSC 133;A (prompt markers)

This patch adds support for the OSC-133;A sequence, introduced by
FinalTerm and implemented by iTerm2, Kitty and more. See
https://iterm2.com/documentation-one-page.html#documentation-escape-codes.html.

The shell emits the OSC just before printing the prompt. This lets the
terminal know where, in the scrollback, there are prompts.

We implement this using a simple boolean in the row struct ("this row
has a prompt"). The prompt marker must be reflowed along with the text
on window resizes.

In an ideal world, erasing, or overwriting the cell where the OSC was
emitted, would remove the prompt mark. Since we don't store this
information in the cell struct, we can't do that. The best we can do
is reset it in erase_line(). This works well enough in the "normal"
screen, when used with a "normal" shell. It doesn't really work in
fullscreen apps, on the alt screen. But that doesn't matter since we
don't support jumping between prompts on the alt screen anyway.

To be able to jump between prompts, two new key bindings have been
added: prompt-prev and prompt-next, bound to ctrl+shift+z and
ctrl+shift+x respectively.

prompt-prev will jump to the previous, not currently visible, prompt,
by moving the viewport, ensuring the prompt is at the top of the
screen.

prompt-next jumps to the next prompt, visible or not. Again, by moving
the viewport to ensure the prompt is at the top of the screen. If
we're at the bottom of the scrollback, the viewport is instead moved
as far down as possible.

Closes #30
This commit is contained in:
Daniel Eklöf 2022-06-15 18:44:23 +02:00
parent 96f23b4c64
commit bdb79e8b9f
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
15 changed files with 285 additions and 18 deletions

40
grid.c
View file

@ -24,6 +24,7 @@
* scrollback, with the *highest* number being at the bottom of the
* screen, where new input appears.
*/
int
grid_row_abs_to_sb(const struct grid *grid, int screen_rows, int abs_row)
{
@ -43,6 +44,38 @@ int grid_row_sb_to_abs(const struct grid *grid, int screen_rows, int sb_rel_row)
return abs_row;
}
int
grid_sb_start_ignore_uninitialized(const struct grid *grid, int screen_rows)
{
int scrollback_start = grid->offset + screen_rows;
scrollback_start &= grid->num_rows - 1;
while (grid->rows[scrollback_start] == NULL) {
scrollback_start++;
scrollback_start &= grid->num_rows - 1;
}
return scrollback_start;
}
int
grid_row_abs_to_sb_precalc_sb_start(const struct grid *grid, int sb_start,
int abs_row)
{
int rebased_row = abs_row - sb_start + grid->num_rows;
rebased_row &= grid->num_rows - 1;
return rebased_row;
}
int
grid_row_sb_to_abs_precalc_sb_start(const struct grid *grid, int sb_start,
int sb_rel_row)
{
int abs_row = sb_rel_row + sb_start;
abs_row &= grid->num_rows - 1;
return abs_row;
}
static void
ensure_row_has_extra_data(struct row *row)
{
@ -196,6 +229,7 @@ grid_snapshot(const struct grid *grid)
clone_row->cells = xmalloc(grid->num_cols * sizeof(clone_row->cells[0]));
clone_row->linebreak = row->linebreak;
clone_row->dirty = row->dirty;
clone_row->prompt_marker = row->prompt_marker;
for (int c = 0; c < grid->num_cols; c++)
clone_row->cells[c] = row->cells[c];
@ -286,6 +320,7 @@ grid_row_alloc(int cols, bool initialize)
row->dirty = false;
row->linebreak = true;
row->extra = NULL;
row->prompt_marker = false;
if (initialize) {
row->cells = xcalloc(cols, sizeof(row->cells[0]));
@ -344,6 +379,7 @@ grid_resize_without_reflow(
new_row->dirty = old_row->dirty;
new_row->linebreak = false;
new_row->prompt_marker = old_row->prompt_marker;
if (new_cols > old_cols) {
/* Clear "new" columns */
@ -503,6 +539,7 @@ _line_wrap(struct grid *old_grid, struct row **new_grid, struct row *row,
/* Scrollback is full, need to re-use a row */
grid_row_reset_extra(new_row);
new_row->linebreak = true;
new_row->prompt_marker = false;
tll_foreach(old_grid->sixel_images, it) {
if (it->item.pos.row == *row_idx) {
@ -834,6 +871,9 @@ grid_resize_and_reflow(
xassert(new_col_idx + amount <= new_cols);
xassert(from + amount <= old_cols);
if (from == 0)
new_row->prompt_marker = old_row->prompt_marker;
memcpy(
&new_row->cells[new_col_idx], &old_row->cells[from],
amount * sizeof(struct cell));