Basic UI with commands
This commit is contained in:
87
ncac.c
87
ncac.c
@@ -1,39 +1,80 @@
|
||||
#include "ncac.h"
|
||||
|
||||
#include <curses.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <term.h>
|
||||
|
||||
#include "asana/asana.h"
|
||||
#include "asana/fetch.h"
|
||||
#include "ui/base.h"
|
||||
#include "ui/commands.h"
|
||||
#include "ui/model.h"
|
||||
|
||||
void append_to_buffer(ui_state *state, char c) {
|
||||
if (state->buffer_size < UI_INPUT_BUFFER_SIZE - 1) {
|
||||
state->input_buffer[state->buffer_size++] = c;
|
||||
state->input_buffer[state->buffer_size] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
bool handle_command(ui_state *state) {
|
||||
for (size_t i = 0; i < UI_NUM_COMMANDS; i++) {
|
||||
if (strncmp(state->input_buffer, ui_commands[i].command,
|
||||
UI_INPUT_BUFFER_SIZE) == 0) {
|
||||
return ui_commands[i].handler(state);
|
||||
}
|
||||
}
|
||||
|
||||
state->curs_y = 0;
|
||||
state->curs_x = 0;
|
||||
|
||||
draw_text("Unknown command. Type \":help\" for a list of commands", state);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(/*int argc, char **argv*/) {
|
||||
setup();
|
||||
|
||||
int curs_x = 0;
|
||||
int curs_y = 0;
|
||||
int input;
|
||||
|
||||
char input;
|
||||
ui_state state;
|
||||
state.mode = 0;
|
||||
state.buffer_size = 0;
|
||||
state.input_buffer[0] = '\0';
|
||||
|
||||
while (true) {
|
||||
draw_status_line(&state);
|
||||
|
||||
while (1) {
|
||||
switch (input = getch()) {
|
||||
case 'a':
|
||||
draw_text("Hello, world!", &curs_x, &curs_y);
|
||||
break;
|
||||
case 'b':
|
||||
draw_text("Goodbye, world!", &curs_x, &curs_y);
|
||||
break;
|
||||
case 'j':
|
||||
get_me(&curs_x, &curs_y);
|
||||
break;
|
||||
case 'm':
|
||||
get_my_tasks(&curs_x, &curs_y);
|
||||
break;
|
||||
case 'q':
|
||||
finish(SIGTERM);
|
||||
break;
|
||||
case '\r':
|
||||
if (state.mode == COMMAND) {
|
||||
state.mode = NORMAL;
|
||||
if (handle_command(&state)) {
|
||||
finish(SIGTERM);
|
||||
}
|
||||
} else if (state.mode == NORMAL) {
|
||||
state.curs_y++;
|
||||
}
|
||||
break;
|
||||
case KEY_EXIT:
|
||||
state.mode = NORMAL;
|
||||
break;
|
||||
case ':':
|
||||
if (state.mode == NORMAL) {
|
||||
state.mode = COMMAND;
|
||||
state.buffer_size = 0;
|
||||
state.input_buffer[0] = '\0';
|
||||
} else {
|
||||
append_to_buffer(&state, input);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
append_to_buffer(&state, input);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -71,6 +112,8 @@ static void setup() {
|
||||
}
|
||||
noecho();
|
||||
nonl();
|
||||
intrflush(stdscr, FALSE);
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
// install handlers
|
||||
signal(SIGINT, &finish);
|
||||
@@ -95,7 +138,7 @@ void get_me(int *curs_x, int *curs_y) {
|
||||
|
||||
*curs_x = 0;
|
||||
(*curs_y)++;
|
||||
draw_text(gid, curs_x, curs_y);
|
||||
// draw_text(gid, curs_x, curs_y);
|
||||
}
|
||||
|
||||
void get_my_tasks(int *curs_x, int *curs_y) {
|
||||
@@ -124,12 +167,12 @@ void get_my_tasks(int *curs_x, int *curs_y) {
|
||||
*curs_x = 0;
|
||||
*curs_y = 0;
|
||||
|
||||
draw_text("My Tasks", curs_x, curs_y);
|
||||
// draw_text("My Tasks", curs_x, curs_y);
|
||||
*curs_x = 0;
|
||||
(*curs_y)+=2;
|
||||
|
||||
for (size_t i=0; i<my_tasks.tasks_len;i++) {
|
||||
draw_text(my_tasks.tasks[i].name, curs_x, curs_y);
|
||||
// draw_text(my_tasks.tasks[i].name, curs_x, curs_y);
|
||||
*curs_x = 0;
|
||||
(*curs_y)++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user