robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesrefactor: move ugrid.c => tui/ugrid.c #38540bff9b5f010

master
1.9 KiB90 linesraw
1#include <assert.h>
2#include <string.h>
3
4#include "nvim/grid.h"
5#include "nvim/memory.h"
6#include "nvim/tui/ugrid.h"
7
8#include "tui/ugrid.c.generated.h"
9
10void ugrid_init(UGrid *grid)
11{
12  grid->cells = NULL;
13}
14
15void ugrid_free(UGrid *grid)
16{
17  destroy_cells(grid);
18}
19
20void ugrid_resize(UGrid *grid, int width, int height)
21{
22  destroy_cells(grid);
23  grid->cells = xmalloc((size_t)height * sizeof(UCell *));
24  for (int i = 0; i < height; i++) {
25    grid->cells[i] = xcalloc((size_t)width, sizeof(UCell));
26  }
27
28  grid->width = width;
29  grid->height = height;
30}
31
32void ugrid_clear(UGrid *grid)
33{
34  clear_region(grid, 0, grid->height - 1, 0, grid->width - 1, 0);
35}
36
37void ugrid_clear_chunk(UGrid *grid, int row, int col, int endcol, sattr_T attr)
38{
39  clear_region(grid, row, row, col, endcol - 1, attr);
40}
41
42void ugrid_goto(UGrid *grid, int row, int col)
43{
44  grid->row = row;
45  grid->col = col;
46}
47
48void ugrid_scroll(UGrid *grid, int top, int bot, int left, int right, int count)
49{
50  // Compute start/stop/step for the loop below
51  int start, stop, step;
52  if (count > 0) {
53    start = top;
54    stop = bot - count + 1;
55    step = 1;
56  } else {
57    start = bot;
58    stop = top - count - 1;
59    step = -1;
60  }
61
62  // Copy cell data
63  for (int i = start; i != stop; i += step) {
64    UCell *target_row = grid->cells[i] + left;
65    UCell *source_row = grid->cells[i + count] + left;
66    assert(right >= left && left >= 0);
67    memcpy(target_row, source_row,
68           sizeof(UCell) * ((size_t)right - (size_t)left + 1));
69  }
70}
71
72static void clear_region(UGrid *grid, int top, int bot, int left, int right, sattr_T attr)
73{
74  for (int row = top; row <= bot; row++) {
75    UGRID_FOREACH_CELL(grid, row, left, right + 1, {
76      cell->data = schar_from_ascii(' ');
77      cell->attr = attr;
78    });
79  }
80}
81
82static void destroy_cells(UGrid *grid)
83{
84  if (grid->cells) {
85    for (int i = 0; i < grid->height; i++) {
86      xfree(grid->cells[i]);
87    }
88    XFREE_CLEAR(grid->cells);
89  }
90}