robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
bff9b5f010
master
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{ 12grid -> cells = NULL ; 13} 14 15void ugrid_free (UGrid * grid ) 16{ 17destroy_cells (grid ); 18} 19 20void ugrid_resize (UGrid * grid ,int width ,int height ) 21{ 22destroy_cells (grid ); 23grid -> cells = xmalloc ((size_t )height * sizeof (UCell * )); 24for (int i = 0 ;i < height ;i ++ ) { 25grid -> cells [i ]= xcalloc ((size_t )width ,sizeof (UCell )); 26 } 27 28grid -> width = width ; 29grid -> height = height ; 30} 31 32void ugrid_clear (UGrid * grid ) 33{ 34clear_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{ 39clear_region (grid ,row ,row ,col ,endcol - 1 ,attr ); 40} 41 42void ugrid_goto (UGrid * grid ,int row ,int col ) 43{ 44grid -> row = row ; 45grid -> 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 51int start ,stop ,step ; 52if (count > 0 ) { 53start = top ; 54stop = bot - count + 1 ; 55step = 1 ; 56 }else { 57start = bot ; 58stop = top - count - 1 ; 59step = -1 ; 60 } 61 62// Copy cell data 63for (int i = start ;i != stop ;i += step ) { 64UCell * target_row = grid -> cells [i ]+ left ; 65UCell * source_row = grid -> cells [i + count ]+ left ; 66assert (right >=left && left >=0 ); 67memcpy (target_row ,source_row , 68sizeof (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{ 74for (int row = top ;row <=bot ;row ++ ) { 75UGRID_FOREACH_CELL (grid ,row ,left ,right + 1 , { 76cell -> data = schar_from_ascii (' ' ); 77cell -> attr = attr ; 78 }); 79 } 80} 81 82static void destroy_cells (UGrid * grid ) 83{ 84if (grid -> cells ) { 85for (int i = 0 ;i < grid -> height ;i ++ ) { 86xfree (grid -> cells [i ]); 87 } 88XFREE_CLEAR (grid -> cells ); 89 } 90}