Skip to content

Commit

Permalink
sheet: Hide row numbers on pivot groups and refactor ui args
Browse files Browse the repository at this point in the history
  • Loading branch information
richiejp committed Dec 18, 2024
1 parent 2ed256f commit 512b19c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 85 deletions.
13 changes: 8 additions & 5 deletions app/sheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct zsvsheet_opts {
static size_t zsvsheet_cell_display_width(struct zsvsheet_ui_buffer *ui_buffer,
struct zsvsheet_display_dimensions *ddims) {
size_t width = ddims->columns /
(ui_buffer->dimensions.col_count + (ui_buffer->rownum_col_offset && !ui_buffer->has_row_num ? 1 : 0));
(ui_buffer->dimensions.col_count + (ui_buffer->rownum_col_offset && !ui_buffer->no_add_row_num ? 1 : 0));
return width < ZSVSHEET_CELL_DISPLAY_MIN_WIDTH ? ZSVSHEET_CELL_DISPLAY_MIN_WIDTH : width;
}

Expand Down Expand Up @@ -472,16 +472,17 @@ static zsvsheet_status zsvsheet_help_handler(struct zsvsheet_proc_context *ctx)
struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
struct zsvsheet_display_info *di = &state->display_info;
struct zsvsheet_screen_buffer_opts bopts = {
.no_rownum_column = 1,
.cell_buff_len = 64,
.max_cell_len = 0,
.rows = 256,
};
struct zsvsheet_opts zsvsheet_opts = {0};
zsvsheet_opts.hide_row_nums = 1;
struct zsvsheet_ui_buffer_opts uibopts = {
.zsvsheet_opts = &zsvsheet_opts,
.buff_opts = &bopts,
.filename = NULL,
.data_filename = NULL,
.no_rownum_col_offset = 1,
.write_after_open = 0,
};
struct zsvsheet_ui_buffer *uib = NULL;
Expand Down Expand Up @@ -766,8 +767,10 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op

if (handler_state.display_info.update_buffer && zsvsheet_buffer_data_filename(ub)) {
struct zsvsheet_opts zsvsheet_opts = {0};
if (read_data(&ub, NULL, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col, header_span,
&zsvsheet_opts, custom_prop_handler)) {
struct zsvsheet_ui_buffer_opts uibopts = {0};
uibopts.zsvsheet_opts = &zsvsheet_opts;
if (read_data(&ub, &uibopts, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col, header_span,
custom_prop_handler)) {
zsvsheet_priv_set_status(&display_dims, ZSVSHEET_STATUS_HIGH_PRIO,
"Unexpected error!"); // to do: better error message
continue;
Expand Down
5 changes: 4 additions & 1 deletion app/sheet/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ int zsvsheet_ui_buffer_open_file_opts(struct zsvsheet_ui_buffer_opts *uibopts,
struct zsvsheet_screen_buffer_opts bopts = {0};
struct zsvsheet_ui_buffer *tmp_ui_buffer = NULL;

if (!uibopts->zsvsheet_opts)
uibopts->zsvsheet_opts = &zsvsheet_opts;

if (!uibopts->buff_opts)
uibopts->buff_opts = &bopts;

int err = 0;
if ((err = read_data(&tmp_ui_buffer, uibopts, 0, 0, 0, &zsvsheet_opts, custom_prop_handler)) != 0 || !tmp_ui_buffer ||
if ((err = read_data(&tmp_ui_buffer, uibopts, 0, 0, 0, custom_prop_handler)) != 0 || !tmp_ui_buffer ||
!tmp_ui_buffer->buff_used_rows) {
zsvsheet_ui_buffer_delete(tmp_ui_buffer);
if (err)
Expand Down
2 changes: 1 addition & 1 deletion app/sheet/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ struct zsvsheet_buffer_data zsvsheet_buffer_info(zsvsheet_buffer_t h) {
struct zsvsheet_buffer_data d = {0};
struct zsvsheet_ui_buffer *b = h;
if (b) {
d.has_row_num = b->has_row_num;
d.has_row_num = b->no_add_row_num;
}
return d;
}
9 changes: 4 additions & 5 deletions app/sheet/pivot.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ static zsvsheet_status zsv_sqlite3_to_csv(zsvsheet_proc_context_t pctx, struct z
fclose(writer_opts.stream);

if (tmp_fn && zsv_file_exists(tmp_fn)) {
struct zsvsheet_opts zsvsheet_opts = {0};
zsvsheet_opts.hide_row_nums = 1;
struct zsvsheet_ui_buffer_opts uibopts = {0};
uibopts.zsvsheet_opts = &zsvsheet_opts;
uibopts.data_filename = tmp_fn;
zst = zsvsheet_open_file_opts(pctx, &uibopts);
} else {
Expand Down Expand Up @@ -205,11 +208,7 @@ zsvsheet_status pivot_drill_down(zsvsheet_proc_context_t ctx) {
if (!zdb || !(sql_str = sqlite3_str_new(zdb->db)))
zst = zsvsheet_status_memory;
else if (zdb->rc == SQLITE_OK && zsv_sqlite3_add_csv(zdb, pd->data_filename, &pd->zopts, NULL) == SQLITE_OK) {
if (zsvsheet_buffer_info(buff).has_row_num)
sqlite3_str_appendf(sql_str, "select *");
else
sqlite3_str_appendf(sql_str, "select rowid as [Row #], *");
sqlite3_str_appendf(sql_str, " from data where %s = %Q", pd->value_sql, pr->value);
sqlite3_str_appendf(sql_str, "select rowid as [Row #], * from data where %s = %Q", pd->value_sql, pr->value);
zst = zsv_sqlite3_to_csv(ctx, zdb, sqlite3_str_value(sql_str), NULL, NULL, NULL);
}

Expand Down
29 changes: 16 additions & 13 deletions app/sheet/read-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@ static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char

static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_ui_buffer will be allocated
struct zsvsheet_ui_buffer_opts *uibopts, // if *uibufferp == NULL and uibopts != NULL
size_t start_row, size_t start_col, size_t header_span, struct zsvsheet_opts *zsvsheet_opts,
size_t start_row, size_t start_col, size_t header_span,
struct zsv_prop_handler *custom_prop_handler) {
const char *filename = (uibufferp && *uibufferp) ? (*uibufferp)->filename : uibopts ? uibopts->filename : NULL;
struct zsvsheet_opts *zsvsheet_opts = uibopts->zsvsheet_opts;
struct zsv_opts opts = {0};
if (uibufferp && *uibufferp)
opts = (*uibufferp)->zsv_opts;
else if (uibopts)
else
opts = uibopts->zsv_opts;

struct zsvsheet_ui_buffer *uibuff = uibufferp ? *uibufferp : NULL;
size_t remaining_rows_to_skip = start_row;
size_t remaining_header_to_skip = header_span;
size_t original_row_num = 0;
FILE *fp;
const char *filename = NULL;

if (uibuff) {
if (uibuff->data_filename)
Expand All @@ -68,7 +69,7 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
filename = uibuff->filename;
}

if (!filename && uibopts) {
if (!filename) {
if (uibopts->data_filename)
filename = uibopts->data_filename;
else if (uibopts->filename)
Expand Down Expand Up @@ -116,17 +117,18 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
size_t find_len = zsvsheet_opts->find ? strlen(zsvsheet_opts->find) : 0;
size_t rows_searched = 0;
zsvsheet_screen_buffer_t buffer = uibuff ? uibuff->buffer : NULL;
if (uibuff && uibuff->has_row_num)
if (uibuff && uibuff->no_add_row_num)
zsvsheet_opts->hide_row_nums = 1;
size_t rownum_column_offset = !zsvsheet_opts->hide_row_nums;

while (zsv_next_row(parser) == zsv_status_row &&
(rows_read == 0 || rows_read < zsvsheet_screen_buffer_rows(buffer))) { // for each row

size_t col_count = zsv_cell_count(parser);
if (uibuff == NULL && uibufferp && uibopts && col_count > 0) {
if (uibuff == NULL && uibufferp && col_count > 0) {
enum zsvsheet_priv_status stat;
struct zsvsheet_ui_buffer *tmp_uibuff = NULL;
if (!(buffer = zsvsheet_screen_buffer_new(col_count, uibopts->buff_opts, &stat)) ||
if (!(buffer = zsvsheet_screen_buffer_new(col_count + rownum_column_offset, uibopts->buff_opts, &stat)) ||
stat != zsvsheet_priv_status_ok || !(tmp_uibuff = zsvsheet_ui_buffer_new(buffer, uibopts))) {
if (tmp_uibuff)
zsvsheet_ui_buffer_delete(tmp_uibuff);
Expand All @@ -138,14 +140,14 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
}

// row number
size_t rownum_column_offset = 0;
if (rows_read == 0 && zsvsheet_opts->hide_row_nums == 0) {
// Check if we already have Row #
struct zsv_cell c = zsv_get_cell(parser, 0);
if (c.len == ZSVSHEET_ROWNUM_HEADER_LEN && !memcmp(c.str, ZSVSHEET_ROWNUM_HEADER, c.len)) {
zsvsheet_opts->hide_row_nums = 1;
if (uibuff)
uibuff->has_row_num = 1;
uibuff->no_add_row_num = 1;
rownum_column_offset = 1;
}
}

Expand All @@ -155,7 +157,7 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
continue;
}
if (uibuff) {
if (col_count + !buffer->opts.no_rownum_column > buffer->cols) {
if (col_count > buffer->cols) {
if (zsvsheet_screen_buffer_grow(buffer, col_count) != zsvsheet_priv_status_ok)
return -1;
}
Expand Down Expand Up @@ -187,7 +189,6 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
sprintf(buff, "########");
zsvsheet_screen_buffer_write_cell(buffer, rows_read, 0, (unsigned char *)buff);
}
rownum_column_offset = 1;
}

for (size_t i = start_col; i < col_count && i + rownum_column_offset < zsvsheet_screen_buffer_cols(buffer); i++) {
Expand Down Expand Up @@ -261,14 +262,16 @@ static void *get_data_index(void *gdi) {
static size_t zsvsheet_find_next(struct zsvsheet_ui_buffer *uib, const char *needle,
struct zsvsheet_opts *zsvsheet_opts, size_t header_span,
struct zsv_prop_handler *custom_prop_handler) {
struct zsvsheet_ui_buffer_opts uibopts = {0};
struct zsvsheet_rowcol *input_offset = &uib->input_offset;
struct zsvsheet_rowcol *buff_offset = &uib->buff_offset;
size_t cursor_row = uib->cursor_row;
zsvsheet_opts->find = needle;
zsvsheet_opts->found_rownum = 0;
uibopts.zsvsheet_opts = zsvsheet_opts;
// TO DO: check if it exists in current row, later column (and change 'cursor_row - 1' below to 'cursor_row')
read_data(&uib, NULL, input_offset->row + buff_offset->row + header_span + cursor_row - 1, 0, header_span,
zsvsheet_opts, custom_prop_handler);
read_data(&uib, &uibopts, input_offset->row + buff_offset->row + header_span + cursor_row - 1, 0, header_span,
custom_prop_handler);
zsvsheet_opts->find = NULL;
return zsvsheet_opts->found_rownum;
}
5 changes: 0 additions & 5 deletions app/sheet/screen_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet
if (opts->cell_buff_len < sizeof(void *) * 2)
*stat = zsvsheet_priv_status_error;
else {
if (!opts->no_rownum_column)
cols++;
void *data = calloc(opts->rows, cols * opts->cell_buff_len);
if (!data)
*stat = zsvsheet_priv_status_memory;
Expand All @@ -108,9 +106,6 @@ enum zsvsheet_priv_status zsvsheet_screen_buffer_grow(zsvsheet_screen_buffer_t b
size_t cell_buff_len = buff->opts.cell_buff_len;
size_t old_row_len = old_cols * cell_buff_len;

if (!buff->opts.no_rownum_column)
cols++;

size_t row_len = cols * cell_buff_len;

assert(cols > old_cols);
Expand Down
1 change: 0 additions & 1 deletion app/sheet/screen_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct zsvsheet_screen_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
};

zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet_screen_buffer_opts *opts,
Expand Down
3 changes: 3 additions & 0 deletions app/sheet/transformation.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ enum zsvsheet_status zsvsheet_push_transformation(zsvsheet_proc_context_t ctx,
goto error;
}

struct zsvsheet_opts zsvsheet_opts = {0};
struct zsvsheet_ui_buffer_opts uibopts = {0};

zsvsheet_opts.hide_row_nums = 1;
uibopts.zsvsheet_opts = &zsvsheet_opts;
uibopts.data_filename = zsvsheet_transformation_filename(trn);
uibopts.write_after_open = 1;

Expand Down
13 changes: 9 additions & 4 deletions app/sheet/ui_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct zsvsheet_ui_buffer {
unsigned char index_ready : 1;
unsigned char rownum_col_offset : 1;
unsigned char index_started : 1;
unsigned char has_row_num : 1;
unsigned char no_add_row_num : 1;
unsigned char mutex_inited : 1;
unsigned char write_in_progress : 1;
unsigned char write_done : 1;
Expand Down Expand Up @@ -93,11 +93,11 @@ void zsvsheet_ui_buffer_delete(struct zsvsheet_ui_buffer *ub) {
}

struct zsvsheet_ui_buffer_opts {
struct zsvsheet_opts *zsvsheet_opts;
struct zsvsheet_screen_buffer_opts *buff_opts;
const char *filename;
const char *data_filename;
struct zsv_opts zsv_opts; // options to use when opening this file
char no_rownum_col_offset;
char write_after_open;
};

Expand All @@ -109,9 +109,14 @@ struct zsvsheet_ui_buffer *zsvsheet_ui_buffer_new(zsvsheet_screen_buffer_t buffe
uib->buffer = buffer;
uib->mutex_inited = 1;
memcpy(&uib->mutex, &init, sizeof(init));
if (!(uibopts && uibopts->no_rownum_col_offset))
uib->rownum_col_offset = 1;
uib->rownum_col_offset = !uibopts;

if (uibopts) {
if (uibopts->zsvsheet_opts->hide_row_nums)
uib->no_add_row_num = 1;
else
uib->rownum_col_offset = 1;

if (uibopts->filename && !(uib->filename = strdup(uibopts->filename))) {
zsvsheet_ui_buffer_delete(uib);
return NULL;
Expand Down
50 changes: 25 additions & 25 deletions app/test/expected/test-sheet-pivot-1-groups.out
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
Row # value Count
1 ad 27
2 ae 147
3 af 27822
4 ag 55
5 ai 14
6 al 4730
7 am 896
8 an 75
9 ao 6170
10 ar 2817
11 at 4740
12 au 3403
13 aw 37
14 az 3522
15 ba 5076
16 bb 165
17 bd 8280
18 be 5056
19 bf 3248
20 bg 6258
21 bh 102
22 bi 646
23 bj 1331
? for help 1
value Count
ad 27
ae 147
af 27822
ag 55
ai 14
al 4730
am 896
an 75
ao 6170
ar 2817
at 4740
au 3403
aw 37
az 3522
ba 5076
bb 165
bd 8280
be 5056
bf 3248
bg 6258
bh 102
bi 646
bj 1331
? for help ad
50 changes: 25 additions & 25 deletions app/test/expected/test-sheet-pivot-2-groups.out
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
Row # value Count
1 C1 1
2 C10 1
3 C100 1
4 C1000 1
5 C1001 1
6 C1002 1
7 C1003 1
8 C1004 1
9 C1005 1
10 C1006 1
11 C1007 1
12 C1008 1
13 C1009 1
14 C101 1
15 C1010 1
16 C1011 1
17 C1012 1
18 C1013 1
19 C1014 1
20 C1015 1
21 C1016 1
22 C1017 1
23 C1018 1
? for help 1
value Count
C1 1
C10 1
C100 1
C1000 1
C1001 1
C1002 1
C1003 1
C1004 1
C1005 1
C1006 1
C1007 1
C1008 1
C1009 1
C101 1
C1010 1
C1011 1
C1012 1
C1013 1
C1014 1
C1015 1
C1016 1
C1017 1
C1018 1
? for help C1

0 comments on commit 512b19c

Please sign in to comment.