Skip to content

Commit

Permalink
sheet var/struct rename: ztv=>zsvsheet, zsv_sheet=>zsvsheet etc (#218)
Browse files Browse the repository at this point in the history
* sheet var/struct rename: ztv=>zsvsheet, zsv_sheet=>zsvsheet etc
  • Loading branch information
liquidaty authored Oct 11, 2024
1 parent ec19a9e commit 9208a83
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 274 deletions.
4 changes: 2 additions & 2 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ ZSV=$(BINDIR)/zsv${EXE}
SOURCES=echo paste count count-pull select select-pull 2tsv 2json serialize flatten pretty stack desc sql 2db compare prop rm mv jq
CLI_SOURCES=echo select desc count paste 2tsv pretty sql flatten 2json serialize stack 2db compare prop rm mv jq

ifeq ($(ZSV_BUILD_SHEET),1)
ifeq ($(ZSVSHEET_BUILD),1)
SOURCES+=sheet
CLI_SOURCES+=sheet
CFLAGS+=-DZSV_BUILD_SHEET
CFLAGS+=-DZSVSHEET_BUILD
CFLAGS+=${CFLAGS_NCURSES}
LDFLAGS+=${LDFLAGS_NCURSES}
endif
Expand Down
4 changes: 2 additions & 2 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ZSV_MAIN_DECL(desc);
ZSV_MAIN_DECL(sql);
ZSV_MAIN_DECL(2db);
ZSV_MAIN_DECL(compare);
#ifdef ZSV_BUILD_SHEET
#ifdef ZSVSHEET_BUILD
ZSV_MAIN_DECL(sheet);
#endif
ZSV_MAIN_DECL(echo);
Expand Down Expand Up @@ -111,7 +111,7 @@ struct builtin_cmd builtin_cmds[] = {
CLI_BUILTIN_COMMAND(sql),
CLI_BUILTIN_COMMAND(2db),
CLI_BUILTIN_COMMAND(compare),
#ifdef ZSV_BUILD_SHEET
#ifdef ZSVSHEET_BUILD
CLI_BUILTIN_COMMAND(sheet),
#endif
CLI_BUILTIN_COMMAND(echo),
Expand Down
238 changes: 121 additions & 117 deletions app/sheet.c

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions app/sheet/buffer.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#include "buffer.h"

struct zsv_sheet_buffer {
struct zsvsheet_buffer {
size_t cols;
size_t long_cell_count;
struct zsv_sheet_buffer_opts opts;
struct zsvsheet_buffer_opts opts;
unsigned char *data;
// to do: add hooks for extension
};

static inline size_t buffer_data_offset(zsv_sheet_buffer_t buff, size_t row, size_t col) {
static inline size_t buffer_data_offset(zsvsheet_buffer_t buff, size_t row, size_t col) {
assert(row < buff->opts.rows && col < buff->cols);
return row * buff->cols * buff->opts.cell_buff_len + col * buff->opts.cell_buff_len;
}

static void set_long_cell(zsv_sheet_buffer_t buff, size_t offset, char *heap) {
static void set_long_cell(zsvsheet_buffer_t buff, size_t offset, char *heap) {
char **target = (char **)(buff->data + offset);
*target = heap;
// memcpy(buff->data + offset, heap, sizeof(heap));
// set flag indicating that this is long cell
*(buff->data + offset + buff->opts.cell_buff_len - 1) = (char)1;
}

static char *get_long_cell(zsv_sheet_buffer_t buff, size_t offset) {
static char *get_long_cell(zsvsheet_buffer_t buff, size_t offset) {
char **valuep = (char **)(buff->data + offset);
if (valuep)
return *valuep;
Expand All @@ -32,15 +32,15 @@ static inline int is_long_cell(const unsigned char *mem, size_t cell_buff_len) {
return *(mem + cell_buff_len - 1) != '\0';
}

size_t zsv_sheet_buffer_cols(zsv_sheet_buffer_t buff) {
size_t zsvsheet_buffer_cols(zsvsheet_buffer_t buff) {
return buff->cols;
}

size_t zsv_sheet_buffer_rows(zsv_sheet_buffer_t buff) {
size_t zsvsheet_buffer_rows(zsvsheet_buffer_t buff) {
return buff->opts.rows;
}

static void free_long_cell(zsv_sheet_buffer_t buff, size_t offset) {
static void free_long_cell(zsvsheet_buffer_t buff, size_t offset) {
if (is_long_cell(buff->data + offset, buff->opts.cell_buff_len)) {
char *value_copy = get_long_cell(buff, offset);
free(value_copy);
Expand All @@ -49,7 +49,7 @@ static void free_long_cell(zsv_sheet_buffer_t buff, size_t offset) {
}
}

void zsv_sheet_buffer_delete(zsv_sheet_buffer_t buff) {
void zsvsheet_buffer_delete(zsvsheet_buffer_t buff) {
if (buff) {
for (size_t i = 0; i < buff->opts.rows && buff->long_cell_count > 0; i++) {
for (size_t j = 0; j < buff->cols && buff->long_cell_count > 0; j++) {
Expand All @@ -62,28 +62,28 @@ void zsv_sheet_buffer_delete(zsv_sheet_buffer_t buff) {
}
}

zsv_sheet_buffer_t zsv_sheet_buffer_new(size_t cols, struct zsv_sheet_buffer_opts *opts,
enum zsv_sheet_buffer_status *stat) {
zsvsheet_buffer_t zsvsheet_buffer_new(size_t cols, struct zsvsheet_buffer_opts *opts,
enum zsvsheet_buffer_status *stat) {
if (opts->rows == 0)
opts->rows = ZSV_SHEET_BUFFER_DEFAULT_ROW_COUNT;
opts->rows = ZSVSHEET_BUFFER_DEFAULT_ROW_COUNT;
else if (opts->rows < 256)
opts->rows = 256;
if (opts->cell_buff_len == 0)
opts->cell_buff_len = ZSV_SHEET_BUFFER_DEFAULT_CELL_BUFF_LEN;
opts->cell_buff_len = ZSVSHEET_BUFFER_DEFAULT_CELL_BUFF_LEN;
if (opts->max_cell_len == 0)
opts->max_cell_len = ZSV_SHEET_BUFFER_DEFAULT_MAX_CELL_LEN;
opts->max_cell_len = ZSVSHEET_BUFFER_DEFAULT_MAX_CELL_LEN;
if (opts->cell_buff_len < sizeof(void *) * 2)
*stat = zsv_sheet_buffer_status_error;
*stat = zsvsheet_buffer_status_error;
else {
if (!opts->no_rownum_column)
cols++;
void *data = calloc(opts->rows, cols * opts->cell_buff_len);
if (!data)
*stat = zsv_sheet_buffer_status_memory;
*stat = zsvsheet_buffer_status_memory;
else {
struct zsv_sheet_buffer *buff = calloc(1, sizeof(*buff));
struct zsvsheet_buffer *buff = calloc(1, sizeof(*buff));
if (!buff)
*stat = zsv_sheet_buffer_status_memory;
*stat = zsvsheet_buffer_status_memory;
else {
buff->opts.rows = opts->rows;
buff->cols = cols;
Expand All @@ -101,9 +101,9 @@ zsv_sheet_buffer_t zsv_sheet_buffer_new(size_t cols, struct zsv_sheet_buffer_opt
#define UTF8_NOT_FIRST_CHAR(x) ((x & 0xC0) == 0x80)
#endif

enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell_w_len(zsv_sheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value, size_t len) {
enum zsv_sheet_buffer_status stat = zsv_sheet_buffer_status_ok;
enum zsvsheet_buffer_status zsvsheet_buffer_write_cell_w_len(zsvsheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value, size_t len) {
enum zsvsheet_buffer_status stat = zsvsheet_buffer_status_ok;
size_t offset = buffer_data_offset(buff, row, col);
free_long_cell(buff, offset);
if (len < buff->opts.cell_buff_len) {
Expand All @@ -120,7 +120,7 @@ enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell_w_len(zsv_sheet_buffer_
len--;
}
if (!len) // the only reason len could be 0 is if our input was not valid utf8, but check to make sure anyway
stat = zsv_sheet_buffer_status_utf8;
stat = zsvsheet_buffer_status_utf8;
else {
char *value_copy = malloc(1 + len);
if (value_copy) {
Expand All @@ -129,18 +129,18 @@ enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell_w_len(zsv_sheet_buffer_
set_long_cell(buff, offset, value_copy);
buff->long_cell_count++;
} else
stat = zsv_sheet_buffer_status_memory;
stat = zsvsheet_buffer_status_memory;
}
}
return stat;
}

enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell(zsv_sheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value) {
return zsv_sheet_buffer_write_cell_w_len(buff, row, col, value, strlen((void *)value));
enum zsvsheet_buffer_status zsvsheet_buffer_write_cell(zsvsheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value) {
return zsvsheet_buffer_write_cell_w_len(buff, row, col, value, strlen((void *)value));
}

const unsigned char *zsv_sheet_buffer_cell_display(zsv_sheet_buffer_t buff, size_t row, size_t col) {
const unsigned char *zsvsheet_buffer_cell_display(zsvsheet_buffer_t buff, size_t row, size_t col) {
if (row < buff->opts.rows && col < buff->cols) {
size_t offset = row * buff->cols * buff->opts.cell_buff_len + col * buff->opts.cell_buff_len;
const unsigned char *cell = &buff->data[offset];
Expand Down
44 changes: 22 additions & 22 deletions app/sheet/buffer.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#ifndef ZSV_SHEET_BUFFER_H
#define ZSV_SHEET_BUFFER_H
#ifndef ZSVSHEET_BUFFER_H
#define ZSVSHEET_BUFFER_H

#define ZSV_SHEET_BUFFER_DEFAULT_CELL_BUFF_LEN 16
#define ZSV_SHEET_BUFFER_DEFAULT_MAX_CELL_LEN 32768 - 1
#define ZSV_SHEET_BUFFER_DEFAULT_ROW_COUNT 1000
#define ZSVSHEET_BUFFER_DEFAULT_CELL_BUFF_LEN 16
#define ZSVSHEET_BUFFER_DEFAULT_MAX_CELL_LEN 32768 - 1
#define ZSVSHEET_BUFFER_DEFAULT_ROW_COUNT 1000

typedef struct zsv_sheet_buffer *zsv_sheet_buffer_t;
typedef struct zsvsheet_buffer *zsvsheet_buffer_t;

enum zsv_sheet_buffer_status {
zsv_sheet_buffer_status_ok = 0,
zsv_sheet_buffer_status_memory,
zsv_sheet_buffer_status_error, // generic error
zsv_sheet_buffer_status_utf8
enum zsvsheet_buffer_status {
zsvsheet_buffer_status_ok = 0,
zsvsheet_buffer_status_memory,
zsvsheet_buffer_status_error, // generic error
zsvsheet_buffer_status_utf8
};

struct zsv_sheet_buffer_opts {
struct zsvsheet_buffer_opts {
size_t cell_buff_len; // default = 16. must be >= 2 * sizeof(void *)
size_t max_cell_len; // length in bytes; defaults to 32767
size_t rows; // rows to buffer. cannot be < 256
char no_rownum_column; // reserved. TO DO: if set, omit row num column
};

zsv_sheet_buffer_t zsv_sheet_buffer_new(size_t cols, struct zsv_sheet_buffer_opts *opts,
enum zsv_sheet_buffer_status *stat);
zsvsheet_buffer_t zsvsheet_buffer_new(size_t cols, struct zsvsheet_buffer_opts *opts,
enum zsvsheet_buffer_status *stat);

enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell(zsv_sheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value);
enum zsvsheet_buffer_status zsvsheet_buffer_write_cell(zsvsheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value);

enum zsv_sheet_buffer_status zsv_sheet_buffer_write_cell_w_len(zsv_sheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value, size_t len);
enum zsvsheet_buffer_status zsvsheet_buffer_write_cell_w_len(zsvsheet_buffer_t buff, size_t row, size_t col,
const unsigned char *value, size_t len);

const unsigned char *zsv_sheet_buffer_cell_display(zsv_sheet_buffer_t buff, size_t row, size_t col);
const unsigned char *zsvsheet_buffer_cell_display(zsvsheet_buffer_t buff, size_t row, size_t col);

void zsv_sheet_buffer_delete(zsv_sheet_buffer_t);
void zsvsheet_buffer_delete(zsvsheet_buffer_t);

size_t zsv_sheet_buffer_cols(zsv_sheet_buffer_t);
size_t zsv_sheet_buffer_rows(zsv_sheet_buffer_t buff);
size_t zsvsheet_buffer_cols(zsvsheet_buffer_t);
size_t zsvsheet_buffer_rows(zsvsheet_buffer_t buff);

#endif
23 changes: 12 additions & 11 deletions app/sheet/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ static size_t input_offset_top() {
return 0;
}

static size_t input_offset_bottom(struct input_dimensions *input_dims,
static size_t input_offset_bottom(struct zsvsheet_input_dimensions *input_dims,
size_t buffer_row_count // number of rows in the buffer, w/o regard to header
) {
return input_dims->row_count - buffer_row_count;
}

static size_t input_offset_centered(struct input_dimensions *input_dims,
static size_t input_offset_centered(struct zsvsheet_input_dimensions *input_dims,
size_t buffer_row_count, // number of rows in the buffer, w/o regard to header
size_t target_raw_ix // raw row index starting at zero, w/o regard to header
) {
Expand All @@ -22,7 +22,7 @@ static size_t input_offset_centered(struct input_dimensions *input_dims,
return target_raw_ix - buffer_half_row_count;
}

// ztv_goto_input_raw_row
// zsvsheet_goto_input_raw_row
// return non-zero if buffer must be reloaded
/*
------- INPUT
Expand All @@ -40,20 +40,21 @@ ioff->3 |
-------
*/

static void set_window_to_cursor(struct ztv_rowcol *buff_offset, size_t target_raw_row, struct ztv_rowcol *input_offset,
size_t input_header_span, struct display_dims *ddims, size_t cursor_row) {
static void set_window_to_cursor(struct zsvsheet_rowcol *buff_offset, size_t target_raw_row,
struct zsvsheet_rowcol *input_offset, size_t input_header_span,
struct zsvsheet_display_dimensions *ddims, size_t cursor_row) {
// assume that input_offset->row is fixed; set buff_offset->row
if (target_raw_row + ddims->header_span >= input_offset->row + cursor_row + input_header_span)
buff_offset->row = target_raw_row - input_offset->row - cursor_row + ddims->header_span - input_header_span;
else
buff_offset->row = 0;
}

static int ztv_goto_input_raw_row(zsv_sheet_buffer_t buffer, size_t input_raw_num, size_t input_header_span,
struct ztv_rowcol *input_offset, struct ztv_rowcol *buff_offset,
struct input_dimensions *input_dims, size_t *cursor_rowp, struct display_dims *ddims,
size_t final_cursor_position) {
size_t buffer_rows = zsv_sheet_buffer_rows(buffer);
static int zsvsheet_goto_input_raw_row(zsvsheet_buffer_t buffer, size_t input_raw_num, size_t input_header_span,
struct zsvsheet_rowcol *input_offset, struct zsvsheet_rowcol *buff_offset,
struct zsvsheet_input_dimensions *input_dims, size_t *cursor_rowp,
struct zsvsheet_display_dimensions *ddims, size_t final_cursor_position) {
size_t buffer_rows = zsvsheet_buffer_rows(buffer);
int update_buffer = 0;
if (input_raw_num < input_offset->row // move the buffer up
|| input_raw_num + input_header_span + 1 > buffer_rows // move the buffer down
Expand All @@ -67,7 +68,7 @@ static int ztv_goto_input_raw_row(zsv_sheet_buffer_t buffer, size_t input_raw_nu
} else {
if (final_cursor_position == (size_t)-1) {
// just move the cursor however much we needed to shift
size_t current = ztv_get_input_raw_row(input_offset, buff_offset, *cursor_rowp);
size_t current = zsvsheet_get_input_raw_row(input_offset, buff_offset, *cursor_rowp);
final_cursor_position = *cursor_rowp + input_raw_num - current;
}
}
Expand Down
42 changes: 21 additions & 21 deletions app/sheet/key-bindings.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include "key-bindings.h"

#ifndef ZTV_CTRL
#define ZTV_CTRL(c) ((c)&037)
#ifndef ZSVSHEET_CTRL
#define ZSVSHEET_CTRL(c) ((c)&037)
#endif

// to do: support customizable bindings
static enum ztv_key ztv_key_binding(int ch) {
static enum zsvsheet_key zsvsheet_key_binding(int ch) {
if (ch == 27) // escape
return ztv_key_escape;
return zsvsheet_key_escape;
if (ch == KEY_SF) // shift + down
return ztv_key_move_bottom;
return zsvsheet_key_move_bottom;
if (ch == KEY_SR) // shift + up
return ztv_key_move_top;
return zsvsheet_key_move_top;
if (ch == 'n')
return ztv_key_find_next;
return zsvsheet_key_find_next;
if (ch == KEY_SLEFT)
return ztv_key_move_first_col;
return zsvsheet_key_move_first_col;
if (ch == KEY_SRIGHT)
return ztv_key_move_last_col;
return zsvsheet_key_move_last_col;
if (ch == KEY_UP)
return ztv_key_move_up;
return zsvsheet_key_move_up;
if (ch == KEY_DOWN)
return ztv_key_move_down;
return zsvsheet_key_move_down;
if (ch == KEY_LEFT)
return ztv_key_move_left;
return zsvsheet_key_move_left;
if (ch == KEY_RIGHT)
return ztv_key_move_right;
if (ch == ZTV_CTRL('f'))
return ztv_key_pg_down;
if (ch == ZTV_CTRL('b'))
return ztv_key_pg_up;
return zsvsheet_key_move_right;
if (ch == ZSVSHEET_CTRL('f'))
return zsvsheet_key_pg_down;
if (ch == ZSVSHEET_CTRL('b'))
return zsvsheet_key_pg_up;
if (ch == 'f')
return ztv_key_filter;
return zsvsheet_key_filter;
if (ch == '/')
return ztv_key_find;
return zsvsheet_key_find;
if (ch == 'q')
return ztv_key_quit;
return ztv_key_unknown;
return zsvsheet_key_quit;
return zsvsheet_key_unknown;
}
Loading

0 comments on commit 9208a83

Please sign in to comment.