Skip to content

Commit

Permalink
Sheet ext cleanup 20241107 (#269)
Browse files Browse the repository at this point in the history
* clean up extension API to not require internal includes
  • Loading branch information
liquidaty committed Nov 9, 2024
1 parent a1c8a33 commit f31e988
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks
e->ext_opts_used = ext_opts_used;

#ifdef ZSVSHEET_BUILD
e->ext_sheet_handler_key = zsvsheet_handler_key;
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;
Expand Down
14 changes: 7 additions & 7 deletions app/ext_example/my_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#include <string.h>
#include <assert.h>
#include <zsv/ext/implementation.h>
// #include <zsv/ext/sheet.h>
#include "../../include/zsv/ext/sheet.h"
#include "../../app/sheet/procedure.h"
#include <zsv/ext/sheet.h>
#include <zsv/utils/writer.h>

/**
Expand Down Expand Up @@ -71,15 +69,17 @@ static enum zsv_ext_status echo_main(zsv_execution_context ctx, int argc, const
const char *opts_used);

#ifdef ZSVSHEET_BUILD

/**
* Here we define a custom command for the zsv `sheet` feature
*/
zsvsheet_handler_status my_test_command_handler(struct zsvsheet_proc_context *ctx) {
zsvsheet_handler_status my_test_command_handler(zsvsheet_proc_context_t ctx) {
char result_buffer[256] = {0};
assert(ctx->invocation.type == zsvsheet_proc_invocation_type_keypress);
int ch = zsv_cb.ext_sheet_keypress(ctx);
if (ch < 0)
return zsvsheet_handler_status_error;
zsv_cb.ext_sheet_prompt(ctx, result_buffer, sizeof(result_buffer), "You pressed %c. Now enter something here",
(char)ctx->invocation.u.keypress.ch);

(char)ch);
if (*result_buffer == '\0')
return zsvsheet_handler_status_ok;

Expand Down
6 changes: 4 additions & 2 deletions app/sheet/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ zsvsheet_handler_status zsvsheet_handler_set_status(struct zsvsheet_proc_context
/**
* Get the key press that triggered this subcommand handler
*/
int zsvsheet_handler_key(zsvsheet_subcommand_handler_context_t ctx) {
return ctx->ch;
int zsvsheet_ext_keypress(zsvsheet_proc_context_t ctx) {
if (ctx && ctx->invocation.type == zsvsheet_proc_invocation_type_keypress)
return ctx->invocation.u.keypress.ch;
return -1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/sheet/handlers_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ zsvsheet_handler_status zsvsheet_handler_set_status(struct zsvsheet_proc_context
/**
* Get the key press that triggered this subcommand handler
*/
int zsvsheet_handler_key(zsvsheet_subcommand_handler_context_t ctx);
int zsvsheet_ext_keypress(zsvsheet_proc_context_t);

/**
* Get the current buffer
Expand Down
7 changes: 1 addition & 6 deletions include/zsv/ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ typedef void *zsv_execution_context;
typedef enum zsv_ext_status (*zsv_ext_main)(zsv_execution_context ctx, int argc, const char *argv[],
struct zsv_opts *opts, const char *opts_used);

typedef struct zsvsheet_proc_context *zsvsheet_proc_context_t;

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

/**
* ZSV callbacks structure
*
Expand Down Expand Up @@ -186,7 +181,7 @@ struct zsv_ext_callbacks {
/**
* Get the key press that triggered this subcommand handler
*/
int (*ext_sheet_handler_key)(zsvsheet_subcommand_handler_context_t ctx);
int (*ext_sheet_keypress)(zsvsheet_proc_context_t ctx);

/****** Managing buffers ******/
/**
Expand Down
6 changes: 6 additions & 0 deletions include/zsv/ext/sheet.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef ZSVSHEET_H
#define ZSVSHEET_H

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

typedef struct zsvsheet_proc_context *zsvsheet_proc_context_t;

typedef enum zsvsheet_handler_status {
zsvsheet_handler_status_ok = 0,
zsvsheet_handler_status_error,
Expand All @@ -14,5 +19,6 @@ typedef struct zsvsheet_handler_context *zsvsheet_handler_context_t;
typedef struct zsvsheet_subcommand_handler_context *zsvsheet_subcommand_handler_context_t;

typedef void *zsvsheet_handler_buffer_t;
// int zsvsheet_ext_keypress(zsvsheet_proc_context_t);

#endif

0 comments on commit f31e988

Please sign in to comment.