begin framework for command handling
This commit is contained in:
parent
7d52612b72
commit
9b4826f9cb
@ -7,9 +7,11 @@ PKG_CHECK_MODULES(EDIT REQUIRED libedit)
|
||||
SET(TARGET "ncdc")
|
||||
|
||||
SET(SOURCES
|
||||
"include/ncdc/cmds.h"
|
||||
"include/ncdc/input.h"
|
||||
"include/ncdc/mainwindow.h"
|
||||
"include/ncdc/ncdc.h"
|
||||
"src/cmds.c"
|
||||
"src/emacs.c"
|
||||
"src/input.c"
|
||||
"src/mainwindow.c"
|
||||
|
19
ncdc/include/ncdc/cmds.h
Normal file
19
ncdc/include/ncdc/cmds.h
Normal 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
|
@ -39,4 +39,6 @@ extern char *ncdc_private_dir;
|
||||
int strwidth(char const *string);
|
||||
char *read_char(FILE *stream);
|
||||
|
||||
wchar_t const *w_next_word(wchar_t const *w, ssize_t len);
|
||||
|
||||
#endif
|
||||
|
11
ncdc/src/cmds.c
Normal file
11
ncdc/src/cmds.c
Normal 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);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#include <ncdc/mainwindow.h>
|
||||
#include <ncdc/input.h>
|
||||
#include <ncdc/cmds.h>
|
||||
#include <ncdc/ncdc.h>
|
||||
|
||||
typedef enum {
|
||||
@ -41,6 +42,8 @@ struct ncdc_mainwindow_
|
||||
|
||||
static void ncdc_mainwindow_resize(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)
|
||||
{
|
||||
@ -66,6 +69,7 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void)
|
||||
ptr->ref.cleanup = (dc_cleanup_t)ncdc_mainwindow_free;
|
||||
|
||||
ptr->in = ncdc_input_new();
|
||||
ncdc_input_set_callback(ptr->in, ncdc_mainwindow_callback, ptr);
|
||||
|
||||
ptr->guilds = newwin(5, 5, 1, 1);
|
||||
ptr->chat = newwin(5, 5, 4, 4);
|
||||
@ -84,6 +88,27 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void)
|
||||
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)
|
||||
{
|
||||
n->guilds_h = LINES - 2;
|
||||
|
@ -1,5 +1,19 @@
|
||||
#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)
|
||||
{
|
||||
uint8_t str[7] = {0};
|
||||
|
Loading…
Reference in New Issue
Block a user