Skip to content

Commit

Permalink
sheet: Allow extensions to disable row numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
richiejp committed Dec 19, 2024
1 parent 495045f commit 6060989
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks
e->ext_sheet_buffer_filename = zsvsheet_buffer_filename;
e->ext_sheet_buffer_data_filename = zsvsheet_buffer_data_filename;
e->ext_sheet_open_file = zsvsheet_open_file;
e->ext_sheet_open_file_opts = zsvsheet_ext_open_file_opts;
e->ext_sheet_register_proc = zsvsheet_register_proc;
e->ext_sheet_register_proc_key_binding = zsvsheet_register_proc_key_binding;
e->ext_sheet_push_transformation = zsvsheet_push_transformation;
Expand Down
18 changes: 9 additions & 9 deletions app/ext_example/mysheet_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ static zsvsheet_status zsv_sqlite3_to_csv(zsvsheet_proc_context_t pctx, struct z
if (writer_opts.stream)
fclose(writer_opts.stream);

if (tmp_fn && zsv_file_exists(tmp_fn))
zst = zsv_cb.ext_sheet_open_file(pctx, tmp_fn, NULL);
else {
if (tmp_fn && zsv_file_exists(tmp_fn)) {
struct zsvsheet_open_file_opts ofopts = {0};
ofopts.data_filename = tmp_fn;
ofopts.no_auto_row_num = 1;
zst = zsv_cb.ext_sheet_open_file_opts(pctx, &ofopts);
} else {
if (zst == zsvsheet_status_ok) {
zst = zsvsheet_status_error; // to do: make this more specific
if (!err_msg && zdb && zdb->rc != SQLITE_OK)
Expand Down Expand Up @@ -228,12 +231,9 @@ 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_cb.ext_sqlite3_add_csv(zdb, pd->data_filename, NULL, NULL) == SQLITE_OK) {
if (zsv_cb.ext_sheet_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, "select rowid as [Row #], *");
sqlite3_str_appendf(sql_str, " from data where %s = %Q", pd->value_sql, pr->value);
fprintf(stderr, "SQL: %s\n", sqlite3_str_value(sql_str));
// fprintf(stderr, "SQL: %s\n", sqlite3_str_value(sql_str));
zst = zsv_sqlite3_to_csv(ctx, zdb, sqlite3_str_value(sql_str), NULL, NULL, NULL);
}
if (sql_str)
Expand Down Expand Up @@ -326,7 +326,7 @@ enum zsv_ext_status zsv_ext_init(struct zsv_ext_callbacks *cb, zsv_execution_con
int proc_id = zsv_cb.ext_sheet_register_proc("my-sheet-pivot", "my sheet pivot", my_pivot_table_command_handler);
if (proc_id < 0)
return zsv_ext_status_error;
zsv_cb.ext_sheet_register_proc_key_binding('v', proc_id);
zsv_cb.ext_sheet_register_proc_key_binding('s', proc_id);
return zsv_ext_status_ok;
}

Expand Down
17 changes: 17 additions & 0 deletions app/sheet/file.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <zsv/ext/sheet.h>

int zsvsheet_ui_buffer_open_file_opts(struct zsvsheet_ui_buffer_opts *uibopts,
struct zsv_prop_handler *custom_prop_handler,
struct zsvsheet_ui_buffer **ui_buffer_stack_bottom,
Expand Down Expand Up @@ -65,3 +67,18 @@ zsvsheet_status zsvsheet_open_file_opts(struct zsvsheet_proc_context *ctx, struc
return zsvsheet_status_error;
return zsvsheet_status_ok;
}

zsvsheet_status zsvsheet_ext_open_file_opts(struct zsvsheet_proc_context *ctx, struct zsvsheet_open_file_opts *opts) {
struct zsvsheet_ui_buffer_opts uibopts = {0};
struct zsvsheet_opts zsvsheet_opts = {0};
struct zsvsheet_screen_buffer_opts bopts = {0};

zsvsheet_opts.hide_row_nums = opts->no_auto_row_num;
uibopts.zsvsheet_opts = &zsvsheet_opts;
uibopts.filename = opts->filename;
uibopts.data_filename = opts->data_filename;
if (opts->zsv_opts)
uibopts.zsv_opts = *opts->zsv_opts;

return zsvsheet_open_file_opts(ctx, &uibopts);
}
5 changes: 5 additions & 0 deletions app/sheet/handlers_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ zsvsheet_status zsvsheet_open_file(struct zsvsheet_proc_context *ctx, const char

/** extension support **/

/**
* Open a tabular file with external facing options
*/
zsvsheet_status zsvsheet_ext_open_file_opts(struct zsvsheet_proc_context *ctx, struct zsvsheet_open_file_opts *opts);

/**
* Set the subcommand prompt
*/
Expand Down
5 changes: 5 additions & 0 deletions include/zsv/ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ struct zsv_ext_callbacks {
*/
zsvsheet_status (*ext_sheet_open_file)(zsvsheet_proc_context_t, const char *filepath, struct zsv_opts *zopts);

/**
* Open a tabular file with more available options
*/
zsvsheet_status (*ext_sheet_open_file_opts)(struct zsvsheet_proc_context *ctx, struct zsvsheet_open_file_opts *opts);

/**
* Set custom context
* @param on_close optional callback to invoke when the buffer is closed
Expand Down
9 changes: 9 additions & 0 deletions include/zsv/ext/sheet.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef ZSVSHEET_H
#define ZSVSHEET_H

#include <zsv.h>

/* custom sheet handler id */
typedef int zsvsheet_proc_id_t;

Expand All @@ -22,6 +24,13 @@ typedef struct zsvsheet_subcommand_context *zsvsheet_subcommand_context_t;
typedef void *zsvsheet_buffer_t;
// int zsvsheet_ext_keypress(zsvsheet_proc_context_t);

struct zsvsheet_open_file_opts {
const char *filename;
const char *data_filename;
struct zsv_opts *zsv_opts;
char no_auto_row_num;
};

typedef struct zsvsheet_transformation *zsvsheet_transformation;
/**
* Transformation options passed to zsvsheet_push_transformation
Expand Down

0 comments on commit 6060989

Please sign in to comment.