Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension api updates 20241110 #282

Merged
merged 5 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct cli_config {

static struct zsv_ext *zsv_ext_new(const char *dl_name, const char *id, char verbose);

zsvsheet_handler_status zsvsheet_ext_prompt(struct zsvsheet_proc_context *ctx, char *buffer, size_t bufsz,
const char *fmt, ...);
zsvsheet_status zsvsheet_ext_prompt(struct zsvsheet_proc_context *ctx, char *buffer, size_t bufsz, const char *fmt,
...);

#include "cli_ini.c"

Expand Down Expand Up @@ -245,6 +245,8 @@ static char *zsv_ext_errmsg(enum zsv_ext_status stat, zsv_execution_context ctx)
return s;
}
// use zsv_ext_status_other for silent errors. will not attempt to call errcode() or errstr()
case zsv_ext_status_not_permitted:
return strdup("Not permitted");
case zsv_ext_status_other:
// use zsv_ext_status_err for custom errors. will attempt to call errcode() and errstr()
// for custom error code and message (if not errcode or errstr not provided, will be silent)
Expand Down Expand Up @@ -386,11 +388,15 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks
#ifdef ZSVSHEET_BUILD
e->ext_sheet_keypress = zsvsheet_ext_keypress;
e->ext_sheet_prompt = zsvsheet_ext_prompt;
e->ext_sheet_handler_set_status = zsvsheet_handler_set_status;
e->ext_sheet_handler_buffer_current = zsvsheet_handler_buffer_current;
e->ext_sheet_handler_buffer_prior = zsvsheet_handler_buffer_prior;
e->ext_sheet_handler_buffer_filename = zsvsheet_handler_buffer_filename;
e->ext_sheet_handler_open_file = zsvsheet_handler_open_file;
e->ext_sheet_buffer_set_ctx = zsvsheet_buffer_set_ctx;
e->ext_sheet_buffer_get_ctx = zsvsheet_buffer_get_ctx;
e->ext_sheet_buffer_get_zsv_opts = zsvsheet_buffer_get_zsv_opts;
e->ext_sheet_set_status = zsvsheet_set_status;
e->ext_sheet_buffer_current = zsvsheet_buffer_current;
e->ext_sheet_buffer_prior = zsvsheet_buffer_prior;
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_register_proc = zsvsheet_register_proc;
e->ext_sheet_register_proc_key_binding = zsvsheet_register_proc_key_binding;
#endif
Expand Down
22 changes: 11 additions & 11 deletions app/ext_example/my_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,39 @@ static enum zsv_ext_status echo_main(zsv_execution_context ctx, int argc, const
/**
* Here we define a custom command for the zsv `sheet` feature
*/
zsvsheet_handler_status my_test_command_handler(zsvsheet_proc_context_t ctx) {
zsvsheet_status my_test_command_handler(zsvsheet_proc_context_t ctx) {
char result_buffer[256] = {0};
int ch = zsv_cb.ext_sheet_keypress(ctx);
if (ch < 0)
return zsvsheet_handler_status_error;
return zsvsheet_status_error;
zsv_cb.ext_sheet_prompt(ctx, result_buffer, sizeof(result_buffer), "You pressed %c. Now enter something here",
(char)ch);
if (*result_buffer == '\0')
return zsvsheet_handler_status_ok;
return zsvsheet_status_ok;

const char *temp_filename = "/tmp/zsvsheet_extension_example.csv";
FILE *f = fopen(temp_filename, "wb");
if (!f)
zsv_cb.ext_sheet_handler_set_status(ctx, "Unable to open for write: %s", temp_filename);
zsv_cb.ext_sheet_set_status(ctx, "Unable to open for write: %s", temp_filename);
else {
fprintf(f, "buffer #,file name\n");
// get a count of open buffers
int i = 0;
for (zsvsheet_handler_buffer_t buff = zsv_cb.ext_sheet_handler_buffer_current(ctx); buff;
buff = zsv_cb.ext_sheet_handler_buffer_prior(buff), i++)
for (zsvsheet_buffer_t buff = zsv_cb.ext_sheet_buffer_current(ctx); buff;
buff = zsv_cb.ext_sheet_buffer_prior(buff), i++)
;

// print a list of open buffers and filenames
for (zsvsheet_handler_buffer_t buff = zsv_cb.ext_sheet_handler_buffer_current(ctx); buff;
buff = zsv_cb.ext_sheet_handler_buffer_prior(buff), i--) {
const char *buff_filename = zsv_cb.ext_sheet_handler_buffer_filename(buff);
for (zsvsheet_buffer_t buff = zsv_cb.ext_sheet_buffer_current(ctx); buff;
buff = zsv_cb.ext_sheet_buffer_prior(buff), i--) {
const char *buff_filename = zsv_cb.ext_sheet_buffer_filename(buff);
if (buff_filename)
fprintf(f, "%i,%s\n", i, buff_filename); // assumes no need for quoting or escaping buff_filename...
}
fclose(f);
return zsv_cb.ext_sheet_handler_open_file(ctx, temp_filename, NULL);
return zsv_cb.ext_sheet_open_file(ctx, temp_filename, NULL);
}
return zsvsheet_handler_status_ok;
return zsvsheet_status_ok;
}
#endif

Expand Down
Loading