Skip to content

Commit

Permalink
Refactored the key binding system and implemented generic procedures (#…
Browse files Browse the repository at this point in the history
…237)

* Refactored the key binding system and implemented generic procedures
 - vim bindings
 - alternative bindings
 - register cmds before extension cmds
 - add subcommand invocation by name

I took the emacs approach where every functionality is a certain defined
procedure which can, but doesn't have to, be bound to a key. Each can
also be given a name so the future implementation of subcommands should
be very easy. I chose the name 'procedure' as there was already a lot
of ambiguity with the word 'command'.

I kept all the existing key bindings, although I modified some to align
them with vim. I also added a incomplete emacs profile just to showcase
how bindings can be changed.
  • Loading branch information
br1tney5pear5 authored Oct 28, 2024
1 parent a58e81e commit 1b36fcd
Show file tree
Hide file tree
Showing 13 changed files with 875 additions and 294 deletions.
22 changes: 20 additions & 2 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "sheet/sheet_internal.h"
#include "sheet/handlers_internal.h"
#endif
#include "sheet/procedure.h"
#include "sheet/key-bindings.h"

struct cli_config {
struct zsv_ext *extensions;
Expand All @@ -35,6 +37,9 @@ 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, ...);

#include "cli_ini.c"

typedef int(cmd_main)(int argc, const char *argv[]);
Expand Down Expand Up @@ -141,6 +146,8 @@ struct zsv_execution_data {
const char **argv;
char opts_used[ZSV_OPTS_SIZE_MAX];
void *custom_context; // user-defined
void *state; // program state relevant to the handler, e.g. cursor
// position in zsv sheet.
};

static const char *ext_opts_used(zsv_execution_context ctx) {
Expand Down Expand Up @@ -189,6 +196,16 @@ static void *ext_get_context(zsv_execution_context ctx) {
return d->custom_context;
}

static void ext_set_state(zsv_execution_context ctx, void *state) {
struct zsv_execution_data *d = ctx;
d->state = state;
}

static void *ext_get_state(zsv_execution_context ctx) {
struct zsv_execution_data *d = ctx;
return d->state;
}

static void ext_set_parser(zsv_execution_context ctx, zsv_parser parser) {
struct zsv_execution_data *d = ctx;
d->parser = parser;
Expand Down Expand Up @@ -367,13 +384,14 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks

#ifdef ZSVSHEET_BUILD
e->ext_sheet_handler_key = zsvsheet_handler_key;
e->ext_sheet_subcommand_prompt = zsvsheet_subcommand_prompt;
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_register_command = zsvsheet_register_command;
e->ext_sheet_register_proc = zsvsheet_register_proc;
e->ext_sheet_register_proc_key_binding = zsvsheet_register_proc_key_binding;
#endif
}
return e;
Expand Down
22 changes: 14 additions & 8 deletions app/ext_example/my_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <stdio.h>
#include <stdlib.h>
#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/utils/writer.h>

/**
Expand Down Expand Up @@ -72,13 +74,15 @@ 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_sheet_subcommand_handler(zsvsheet_subcommand_handler_context_t ctx) {
int ch = zsv_cb.ext_sheet_handler_key(ctx);
zsv_cb.ext_sheet_subcommand_prompt(ctx, "You pressed %c. Now enter something here", (char)ch);
return zsvsheet_handler_status_ok;
}
zsvsheet_handler_status my_test_command_handler(struct zsvsheet_proc_context *ctx) {
char result_buffer[256] = {0};
assert(ctx->invocation.type == zsvsheet_proc_invocation_type_keypress);
zsv_cb.ext_sheet_prompt(ctx, result_buffer, sizeof(result_buffer), "You pressed %c. Now enter something here",
(char)ctx->invocation.u.keypress.ch);

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

zsvsheet_handler_status my_sheet_handler(zsvsheet_handler_context_t ctx) {
const char *temp_filename = "/tmp/zsvsheet_extension_example.csv";
FILE *f = fopen(temp_filename, "wb");
if (!f)
Expand All @@ -103,7 +107,6 @@ zsvsheet_handler_status my_sheet_handler(zsvsheet_handler_context_t ctx) {
}
return zsvsheet_handler_status_ok;
}

#endif

/**
Expand Down Expand Up @@ -134,7 +137,10 @@ enum zsv_ext_status zsv_ext_init(struct zsv_ext_callbacks *cb, zsv_execution_con
zsv_cb.ext_add_command(ctx, "echo", "print the input data back to stdout", echo_main);

#ifdef ZSVSHEET_BUILD
zsv_cb.ext_sheet_register_command('t', "my-test-command", my_sheet_subcommand_handler, my_sheet_handler);
int proc_id = zsv_cb.ext_sheet_register_proc("my-test-command", my_test_command_handler);
if (proc_id < 0)
return zsv_ext_status_error;
zsv_cb.ext_sheet_register_proc_key_binding('t', proc_id);
#endif
return zsv_ext_status_ok;
}
Expand Down
Loading

0 comments on commit 1b36fcd

Please sign in to comment.