begin framework for command handling

This commit is contained in:
Florian Stinglmayr 2019-06-28 16:25:52 +02:00
parent 7d52612b72
commit 9b4826f9cb
6 changed files with 73 additions and 0 deletions

View File

@ -7,9 +7,11 @@ PKG_CHECK_MODULES(EDIT REQUIRED libedit)
SET(TARGET "ncdc") SET(TARGET "ncdc")
SET(SOURCES SET(SOURCES
"include/ncdc/cmds.h"
"include/ncdc/input.h" "include/ncdc/input.h"
"include/ncdc/mainwindow.h" "include/ncdc/mainwindow.h"
"include/ncdc/ncdc.h" "include/ncdc/ncdc.h"
"src/cmds.c"
"src/emacs.c" "src/emacs.c"
"src/input.c" "src/input.c"
"src/mainwindow.c" "src/mainwindow.c"

19
ncdc/include/ncdc/cmds.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef NCDC_CMDS_H
#define NCDC_CMDS_H
#include <ncdc/ncdc.h>
#include <ncdc/mainwindow.h>
typedef bool (*ncdc_command_t)(ncdc_mainwindow_t n,
wchar_t const *s, size_t len);
typedef struct {
wchar_t const *name;
ncdc_command_t handler;
} ncdc_commands_t;
extern ncdc_commands_t cmds[];
bool ncdc_cmd_quit(ncdc_mainwindow_t n, wchar_t const *s, size_t len);
#endif

View File

@ -39,4 +39,6 @@ extern char *ncdc_private_dir;
int strwidth(char const *string); int strwidth(char const *string);
char *read_char(FILE *stream); char *read_char(FILE *stream);
wchar_t const *w_next_word(wchar_t const *w, ssize_t len);
#endif #endif

11
ncdc/src/cmds.c Normal file
View File

@ -0,0 +1,11 @@
#include <ncdc/cmds.h>
ncdc_commands_t cmds[] = {
{ L"quit", ncdc_cmd_quit },
{ NULL, NULL }
};
bool ncdc_cmd_quit(ncdc_mainwindow_t n, wchar_t const *s, size_t len)
{
exit(0);
}

View File

@ -1,5 +1,6 @@
#include <ncdc/mainwindow.h> #include <ncdc/mainwindow.h>
#include <ncdc/input.h> #include <ncdc/input.h>
#include <ncdc/cmds.h>
#include <ncdc/ncdc.h> #include <ncdc/ncdc.h>
typedef enum { typedef enum {
@ -41,6 +42,8 @@ struct ncdc_mainwindow_
static void ncdc_mainwindow_resize(ncdc_mainwindow_t n); static void ncdc_mainwindow_resize(ncdc_mainwindow_t n);
static void ncdc_mainwindow_update_focus(ncdc_mainwindow_t n); static void ncdc_mainwindow_update_focus(ncdc_mainwindow_t n);
static bool ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s,
size_t len, void *arg);
static void ncdc_mainwindow_free(ncdc_mainwindow_t n) static void ncdc_mainwindow_free(ncdc_mainwindow_t n)
{ {
@ -66,6 +69,7 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void)
ptr->ref.cleanup = (dc_cleanup_t)ncdc_mainwindow_free; ptr->ref.cleanup = (dc_cleanup_t)ncdc_mainwindow_free;
ptr->in = ncdc_input_new(); ptr->in = ncdc_input_new();
ncdc_input_set_callback(ptr->in, ncdc_mainwindow_callback, ptr);
ptr->guilds = newwin(5, 5, 1, 1); ptr->guilds = newwin(5, 5, 1, 1);
ptr->chat = newwin(5, 5, 4, 4); ptr->chat = newwin(5, 5, 4, 4);
@ -84,6 +88,27 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void)
return ptr; return ptr;
} }
static bool
ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s,
size_t len, void *arg)
{
ncdc_mainwindow_t mainwin = (ncdc_mainwindow_t)arg;
if (s[0] == '/') {
size_t i = 0;
wchar_t const *n = w_next_word(s, len);
for (; cmds[i].name != NULL; i++) {
if (wcsncmp(s+1, cmds[i].name, (n-s-1)) == 0) {
cmds[i].handler(mainwin, s, len);
return true;
}
}
}
return false;
}
static void ncdc_mainwindow_resize(ncdc_mainwindow_t n) static void ncdc_mainwindow_resize(ncdc_mainwindow_t n)
{ {
n->guilds_h = LINES - 2; n->guilds_h = LINES - 2;

View File

@ -1,5 +1,19 @@
#include <ncdc/ncdc.h> #include <ncdc/ncdc.h>
wchar_t const *w_next_word(wchar_t const *w, ssize_t len)
{
size_t i = 0;
if (len < 0) {
len = wcslen(w);
}
for (; !iswspace(w[i]) && i < len; i++)
;
return w+i;
}
char *read_char(FILE *stream) char *read_char(FILE *stream)
{ {
uint8_t str[7] = {0}; uint8_t str[7] = {0};