Basic UI with commands

This commit is contained in:
2020-06-12 11:40:48 -07:00
parent b4d90769ba
commit 78859dc4fa
8 changed files with 236 additions and 27 deletions

View File

@@ -1,17 +1,40 @@
#include "base.h"
#include <curses.h>
#include <stdbool.h>
#include <string.h>
void draw_text(char *text, int *x, int *y) {
#include "commands.h"
#include "model.h"
void draw_status_line(ui_state *state) {
int old_x = state->curs_x;
int old_y = state->curs_y;
state->curs_y = LINES - 1;
state->curs_x = 0;
if (state->mode == NORMAL) {
draw_text("NORMAL", state);
} else {
draw_text(":", state);
draw_text(state->input_buffer, state);
}
clrtoeol();
state->curs_x = old_x;
state->curs_y = old_y;
}
void draw_text(const char *text, ui_state *state) {
if (!text || strnlen(text, 1) == 0) return;
mvaddch(*y, *x, *text);
mvaddch(state->curs_y, state->curs_x, *text);
++text;
for (; *text != '\0'; ++text) {
addch(*text);
}
getyx(stdscr, *y, *x);
getyx(stdscr, state->curs_y, state->curs_x);
}

View File

@@ -1,10 +1,19 @@
#ifndef UI_BASE_H_
#define UI_BASE_H_
#include <stdbool.h>
#include "model.h"
/**
* Prints the string text starting at position *x, *y. x and y are updated to
* the position after the last character
*/
void draw_text(char *text, int *x, int *y);
void draw_text(const char *text, ui_state *state);
/**
* Render the input status bar
*/
void draw_status_line(ui_state *state);
#endif // UI_BASE_H_

92
ui/commands.c Normal file
View File

@@ -0,0 +1,92 @@
#include "commands.h"
#include <curses.h>
#include <stdbool.h>
#include "../asana/asana.h"
#include "base.h"
bool ui_quit(ui_state *state) { return true; }
bool ui_help(ui_state *state) {
state->curs_x = 0;
state->curs_y = 0;
draw_text("Commands:", state);
state->curs_x = 0;
state->curs_y += 2;
for (size_t i = 0; i < UI_NUM_COMMANDS; i++) {
draw_text("\t", state);
draw_text(ui_commands[i].command, state);
draw_text("\t", state);
draw_text(ui_commands[i].help_text, state);
clrtoeol();
state->curs_x = 0;
state->curs_y++;
}
return false;
}
bool ui_mytasks(ui_state *state) {
state->curs_x = 0;
state->curs_y = 0;
draw_text("Fetching user...", state);
clrtoeol();
/*
User me;
user_info(&me);
if (me.workspaces == NULL || me.workspaces_len == 0) {
fprintf(stderr, "Unable to get workspaces.\n");
return true;
}
*/
state->curs_x = 0;
state->curs_y = 0;
draw_text("Fetching user task list ID...", state);
clrtoeol();
char gid[64];
gid[0] = '\0';
if (user_task_list_gid(
/*me.workspaces[me.workspaces_len-1].gid*/ "15793206719", gid) !=
ASANA_ERR_OK) {
fprintf(stderr, "Unable to get task list ID. %s\n", gid);
return true;
}
state->curs_x = 0;
state->curs_y = 0;
draw_text("Fetching user task list", state);
clrtoeol();
Project my_tasks;
if (user_task_list(gid, &my_tasks) != ASANA_ERR_OK) {
fprintf(stderr, "Unable to get task list.\n");
return true;
}
state->curs_x = 0;
state->curs_y = 0;
draw_text("My Tasks", state);
clrtoeol();
for (size_t i = 0; i < my_tasks.tasks_len; i++) {
state->curs_y++;
state->curs_x = 0;
draw_text(my_tasks.tasks[i].name, state);
clrtoeol();
}
return false;
}
ui_command ui_commands[UI_NUM_COMMANDS] = {
{"help", "print this help message", ui_help},
{"q", "quit the application", ui_quit},
{"mytasks", "fetch and display your task list", ui_mytasks}};

19
ui/commands.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef UI_COMMANDS_H_
#define UI_COMMANDS_H_
#include <stdbool.h>
#include "model.h"
typedef struct ui_command {
const char *command;
const char *help_text;
bool (*handler)(ui_state *);
} ui_command;
#define UI_NUM_COMMANDS 3
extern ui_command ui_commands[];
bool handle_command(ui_state *state);
#endif // UI_COMMANDS_H_

1
ui/model.c Normal file
View File

@@ -0,0 +1 @@
#include "model.h"

21
ui/model.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef UI_MODEL_H_
#define UI_MODEL_H_
#include <stdlib.h>
enum UI_MODE {
NORMAL,
COMMAND,
};
#define UI_INPUT_BUFFER_SIZE 4096
typedef struct ui_state {
int curs_x;
int curs_y;
enum UI_MODE mode;
size_t buffer_size;
char input_buffer[UI_INPUT_BUFFER_SIZE];
} ui_state;
#endif // UI_MODEL_H_