From 6060989b4abc47bab2bcb6db7188433605bec76a Mon Sep 17 00:00:00 2001 From: Richard Palethorpe Date: Thu, 19 Dec 2024 14:28:02 +0000 Subject: [PATCH] sheet: Allow extensions to disable row numbers --- app/cli.c | 1 + app/ext_example/mysheet_extension.c | 18 +++++++++--------- app/sheet/file.c | 17 +++++++++++++++++ app/sheet/handlers_internal.h | 5 +++++ include/zsv/ext.h | 5 +++++ include/zsv/ext/sheet.h | 9 +++++++++ 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/app/cli.c b/app/cli.c index 40fb1b75..0a1b016b 100644 --- a/app/cli.c +++ b/app/cli.c @@ -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; diff --git a/app/ext_example/mysheet_extension.c b/app/ext_example/mysheet_extension.c index 318be95b..c9648876 100644 --- a/app/ext_example/mysheet_extension.c +++ b/app/ext_example/mysheet_extension.c @@ -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) @@ -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) @@ -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; } diff --git a/app/sheet/file.c b/app/sheet/file.c index 2ad41318..74c55727 100644 --- a/app/sheet/file.c +++ b/app/sheet/file.c @@ -1,3 +1,5 @@ +#include + 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, @@ -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); +} diff --git a/app/sheet/handlers_internal.h b/app/sheet/handlers_internal.h index dfbf5758..b7e4567b 100644 --- a/app/sheet/handlers_internal.h +++ b/app/sheet/handlers_internal.h @@ -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 */ diff --git a/include/zsv/ext.h b/include/zsv/ext.h index 8cf65d46..5816508f 100644 --- a/include/zsv/ext.h +++ b/include/zsv/ext.h @@ -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 diff --git a/include/zsv/ext/sheet.h b/include/zsv/ext/sheet.h index f3e6b7e6..51c1e1c0 100644 --- a/include/zsv/ext/sheet.h +++ b/include/zsv/ext/sheet.h @@ -1,6 +1,8 @@ #ifndef ZSVSHEET_H #define ZSVSHEET_H +#include + /* custom sheet handler id */ typedef int zsvsheet_proc_id_t; @@ -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