mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-25 01:40:19 -05:00
grid: track both linear and row,col cursor
This commit is contained in:
parent
963b266cce
commit
50c43be0d9
6 changed files with 95 additions and 27 deletions
75
grid.c
75
grid.c
|
|
@ -1,6 +1,10 @@
|
|||
#include "grid.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define min(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define max(x, y) ((x) > (y) ? (x) : (y))
|
||||
|
||||
void
|
||||
grid_erase(struct grid *grid, int start, int end)
|
||||
|
|
@ -17,19 +21,74 @@ grid_erase(struct grid *grid, int start, int end)
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
grid_cursor_move(struct grid *grid, int cols)
|
||||
int
|
||||
grid_cursor_linear(const struct grid *grid, int row, int col)
|
||||
{
|
||||
int new_cursor = grid->cursor + cols;
|
||||
grid->cells[grid->cursor].dirty = true;
|
||||
grid->cells[new_cursor].dirty = true;
|
||||
grid->cursor = new_cursor;
|
||||
return row * grid->cols + col;
|
||||
}
|
||||
|
||||
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->print_needs_wrap = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue