grid: track both linear and row,col cursor

This commit is contained in:
Daniel Eklöf 2019-06-17 21:15:20 +02:00
parent 963b266cce
commit 50c43be0d9
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 95 additions and 27 deletions

14
csi.c
View file

@ -96,7 +96,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 'J': { case 'J': {
assert(term->vt.params.idx == 0); assert(term->vt.params.idx == 0);
int start = term->grid.cursor / term->grid.cols * term->grid.cols; int start = grid_cursor_linear(&term->grid, term->grid.cursor.row, 0);
int end = term->grid.cols * term->grid.rows; int end = term->grid.cols * term->grid.rows;
grid_erase(&term->grid, start, end); grid_erase(&term->grid, start, end);
return true; return true;
@ -104,10 +104,10 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 'K': { case 'K': {
assert(term->vt.params.idx == 0); assert(term->vt.params.idx == 0);
int start = term->grid.cursor; int start = term->grid.linear_cursor;
int end = (start + term->grid.cols - 1) / term->grid.cols * term->grid.cols - 1; int end = grid_cursor_linear(
assert(start <= end); &term->grid, term->grid.cursor.row, term->grid.cols - 1);
assert((end + 1) % term->grid.cols == 0); LOG_DBG("K: %d -> %d", start, end);
grid_erase(&term->grid, start, end); grid_erase(&term->grid, start, end);
return true; return true;
@ -115,13 +115,13 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 'C': { case 'C': {
int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1; int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1;
grid_cursor_move(&term->grid, count); grid_cursor_right(&term->grid, count);
return true; return true;
} }
case 'D': { case 'D': {
int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1; int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1;
grid_cursor_move(&term->grid, -count); grid_cursor_left(&term->grid, count);
return true; return true;
} }

75
grid.c
View file

@ -1,6 +1,10 @@
#include "grid.h" #include "grid.h"
#include <string.h> #include <string.h>
#include <assert.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
void void
grid_erase(struct grid *grid, int start, int end) grid_erase(struct grid *grid, int start, int end)
@ -17,19 +21,74 @@ grid_erase(struct grid *grid, int start, int end)
} }
} }
void int
grid_cursor_move(struct grid *grid, int cols) grid_cursor_linear(const struct grid *grid, int row, int col)
{ {
int new_cursor = grid->cursor + cols; return row * grid->cols + col;
grid->cells[grid->cursor].dirty = true; }
grid->cells[new_cursor].dirty = true;
grid->cursor = new_cursor; void
grid_cursor_to(struct grid *grid, int row, int col)
{
assert(row >= 0);
assert(row < grid->rows);
assert(col >= 0);
assert(col < grid->cols);
int new_linear = row * grid->cols + col;
assert(new_linear >= 0);
assert(new_linear < grid->rows * grid->cols);
grid->cells[grid->linear_cursor].dirty = true;
grid->cells[new_linear].dirty = true;
grid->dirty = true;
grid->print_needs_wrap = false;
grid->linear_cursor = new_linear;
grid->cursor.col = col;
grid->cursor.row = row;
}
void
grid_cursor_left(struct grid *grid, int count)
{
int move_amount = min(grid->cursor.col, count);
int new_linear = grid->linear_cursor - move_amount;
int new_col = grid->cursor.col - move_amount;
assert(new_linear >= 0);
assert(new_linear < grid->rows * grid->cols);
assert(new_col >= 0);
assert(new_col < grid->cols);
grid->cells[grid->linear_cursor].dirty = true;
grid->cells[new_linear].dirty = true;
grid->linear_cursor = new_linear;
grid->cursor.col = new_col;
grid->dirty = true; grid->dirty = true;
grid->print_needs_wrap = false; grid->print_needs_wrap = false;
} }
void void
grid_cursor_to(struct grid *grid, int pos) grid_cursor_right(struct grid *grid, int count)
{ {
grid_cursor_move(grid, pos - grid->cursor); int move_amount = min(grid->cols - grid->cursor.col - 1, count);
int new_linear = grid->linear_cursor + move_amount;
int new_col = grid->cursor.col + move_amount;
assert(new_linear >= 0);
assert(new_linear < grid->rows * grid->cols);
assert(new_col >= 0);
assert(new_col < grid->cols);
grid->cells[grid->linear_cursor].dirty = true;
grid->cells[new_linear].dirty = true;
grid->linear_cursor = new_linear;
grid->cursor.col = new_col;
grid->dirty = true;
grid->print_needs_wrap = false;
} }

10
grid.h
View file

@ -4,5 +4,11 @@
void grid_erase(struct grid *grid, int start, int end); void grid_erase(struct grid *grid, int start, int end);
void grid_cursor_to(struct grid *grid, int pos); void grid_cursor_to(struct grid *grid, int row, int col);
void grid_cursor_move(struct grid *grid, int cols); void grid_cursor_left(struct grid *grid, int count);
void grid_cursor_right(struct grid *grid, int count);
int grid_cursor_linear(const struct grid *grid, int row, int col);
//void grid_cursor_to(struct grid *grid, int pos);
//void grid_cursor_move(struct grid *grid, int cols);

2
main.c
View file

@ -97,7 +97,7 @@ grid_render(struct context *c)
cell->dirty = false; cell->dirty = false;
bool has_cursor = c->term.grid.cursor == cell_idx; bool has_cursor = c->term.grid.linear_cursor == cell_idx;
int y_ofs = row * c->term.grid.cell_height + c->fextents.ascent; int y_ofs = row * c->term.grid.cell_height + c->fextents.ascent;
int x_ofs = col * c->term.grid.cell_width; int x_ofs = col * c->term.grid.cell_width;

View file

@ -28,8 +28,13 @@ struct grid {
int cell_width; int cell_width;
int cell_height; int cell_height;
int cursor; int linear_cursor;
struct {
int row;
int col;
} cursor;
bool print_needs_wrap; bool print_needs_wrap;
struct cell *cells; struct cell *cells;
uint32_t foreground; uint32_t foreground;

14
vt.c
View file

@ -158,13 +158,11 @@ action(struct terminal *term, enum action action, uint8_t c)
LOG_DBG("execute: 0x%02x", c); LOG_DBG("execute: 0x%02x", c);
switch (c) { switch (c) {
case '\r': case '\r':
grid_cursor_to( grid_cursor_left(&term->grid, term->grid.cursor.col);
&term->grid,
term->grid.cursor / term->grid.cols * term->grid.cols);
break; break;
case '\b': case '\b':
grid_cursor_move(&term->grid, -1); grid_cursor_left(&term->grid, 1);
break; break;
} }
@ -179,9 +177,9 @@ action(struct terminal *term, enum action action, uint8_t c)
case ACTION_PRINT: { case ACTION_PRINT: {
if (term->grid.print_needs_wrap) if (term->grid.print_needs_wrap)
grid_cursor_move(&term->grid, 1); grid_cursor_to(&term->grid, term->grid.cursor.row + 1, 0);
struct cell *cell = &term->grid.cells[term->grid.cursor]; struct cell *cell = &term->grid.cells[term->grid.linear_cursor];
cell->dirty = true; cell->dirty = true;
@ -197,8 +195,8 @@ action(struct terminal *term, enum action action, uint8_t c)
cell->attrs = term->vt.attrs; cell->attrs = term->vt.attrs;
if ((term->grid.cursor + 1) % term->grid.cols) if (term->grid.cursor.col < term->grid.cols - 1)
grid_cursor_move(&term->grid, 1); grid_cursor_right(&term->grid, 1);
else else
term->grid.print_needs_wrap = true; term->grid.print_needs_wrap = true;