From d201bf2bf253c80a8629f11e44da9daca67470aa Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 17 Jun 2023 17:46:10 -0700 Subject: console library works now --- Makefile | 2 +- src/conio.c | 16 ++++++++++++ src/conio.h | 6 +++++ src/console.c | 22 +++++++++++------ src/console.h | 8 +++--- src/operetta.c | 77 ++++++++++++++++++++++++++++++++++++++++++---------------- src/operetta.h | 5 ++-- 7 files changed, 100 insertions(+), 36 deletions(-) create mode 100644 src/conio.c create mode 100644 src/conio.h diff --git a/Makefile b/Makefile index 7bf5db5..3abb97e 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PREFIX ?= /usr BINDIR ?= ${PREFIX}/bin generate: - $(CC) -o operetta src/operetta.c src/proposition.c src/parse.c src/lexer.c src/eval.c src/console.c $(CFLAGS) + $(CC) -o operetta src/operetta.c src/proposition.c src/parse.c src/lexer.c src/eval.c src/console.c src/conio.c $(CFLAGS) install: operetta install operetta ${DESTDIR}${BINDIR} diff --git a/src/conio.c b/src/conio.c new file mode 100644 index 0000000..b69f087 --- /dev/null +++ b/src/conio.c @@ -0,0 +1,16 @@ +#include "conio.h" +#include +#include +#include + +int getch() { + struct termios oldattr, newattr; + int ch; + tcgetattr(STDIN_FILENO, &oldattr); + newattr = oldattr; + newattr.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newattr); + ch = getchar(); + tcsetattr(STDIN_FILENO, TCSANOW, &oldattr); + return ch; +} diff --git a/src/conio.h b/src/conio.h new file mode 100644 index 0000000..520e146 --- /dev/null +++ b/src/conio.h @@ -0,0 +1,6 @@ +#ifndef CONIO_H +#define CONIO_H + +int getch(void); + +#endif diff --git a/src/console.c b/src/console.c index dba8c85..8d0cebb 100644 --- a/src/console.c +++ b/src/console.c @@ -37,10 +37,15 @@ consoleLineSet_t init_consolelineset(consoleLine_t* lines, int count) { lineSet.count = count; return lineSet; } +void set_cache(consoleLineSet_t lines) { + lineCache = lines; +} void clear_screen() { - printf("\033[2J"); + /* clear screen, move cursor to the top */ + printf("\033[2J\033[0;0H"); } void draw_screen() { + clear_screen(); int linesDrawn = 0; struct winsize win; ioctl(fileno(stdout), TIOCGWINSZ, &win); @@ -48,13 +53,13 @@ void draw_screen() { int height = (int)(win.ws_row); int i = scroll; int e = 0; - while (i - scroll + e < height) { + while (i - scroll + e < height && i - scroll < lineCache.count) { char c = lineCache.lines[i].color; if (i == selected) { - printf("%s%s", c == CBLACK ? BLKB : c == CRED ? REDB : c == CGREEN ? GRNB : WHTB, BLK); + printf("%s%s", BLK, c == CBLACK ? BLKB : c == CRED ? REDB : c == CGREEN ? GRNB : WHTB); } else { - printf("%s%s", BLKB, c == CBLACK ? BLK : c == CRED ? RED : c == CGREEN ? GRN : WHT); + printf("%s%s", c == CBLACK ? BLK : c == CRED ? RED : c == CGREEN ? GRN : WHT, BLKB); } printf("%s\n", lineCache.lines[i].content); if (lineCache.lines[i].isExpanded) { @@ -63,10 +68,10 @@ void draw_screen() { } i++; } - printf("%s\n", RESET); + printf("%s", RESET); } void move_selection(int amount) { - selected = (selected + amount) % lineCache.count; + selected = (((selected + amount) % lineCache.count) + lineCache.count) % lineCache.count; struct winsize win; ioctl(fileno(stdout), TIOCGWINSZ, &win); int height = (int)(win.ws_row); @@ -79,7 +84,10 @@ void move_selection(int amount) { } void interact_selected() { if (lineCache.lines[selected].onInteract == NULL) { - lineCache.lines[selected].isExpanded = !lineCache.lines[selected].isExpanded; } else { lineCache.lines[selected].onInteract(); + lineCache.lines[selected].isExpanded = !lineCache.lines[selected].isExpanded; + } + else { + lineCache.lines[selected].onInteract(); } clear_screen(); draw_screen(); diff --git a/src/console.h b/src/console.h index 88eb8a4..2e6ba4c 100644 --- a/src/console.h +++ b/src/console.h @@ -1,9 +1,9 @@ #ifndef CONSOLE #define CONSOLE #define BLK "\033[0;30m" -#define RED "\033[0:31m" -#define GRN "\033[0:32m" -#define WHT "\033[0:37m" +#define RED "\033[0;31m" +#define GRN "\033[0;32m" +#define WHT "\033[0;37m" #define BLKB "\033[40m" #define REDB "\033[41m" #define GRNB "\033[42m" @@ -31,7 +31,7 @@ typedef struct ConsoleLineSet { int count; } consoleLineSet_t; consoleLineSet_t init_consolelineset(consoleLine_t* lines, int count); -void set_lines(consoleLineSet_t lines); +void set_cache(consoleLineSet_t lines); void clear_screen(void); void draw_screen(void); void move_selection(int amount); diff --git a/src/operetta.c b/src/operetta.c index f754895..7e257f0 100644 --- a/src/operetta.c +++ b/src/operetta.c @@ -1,52 +1,87 @@ #include #include +#include #include "operetta.h" #include "lexer.h" #include "parse.h" #include "proposition.h" #include "eval.h" +#include "console.h" +#include "conio.h" int main(int argc, char** argv) { /* read file */ - char* path = argv[1]; + char* path; + if (argc > 0) { + path = argv[1]; + } + else { + exit(0); + } char* content = read_file(path); int lineCount = count_lines(content); lineSet_t lines = parse_lines(content, lineCount); + consoleLine_t* consoleLines = malloc(sizeof(consoleLine_t) * lines.count); for (int i = 0; i < lines.count; i++) { line_t line = lines.lines[i]; - printf("%d. ", i + 1); - for (int j = 0; j < line.scope.depth; j++) { - printf(" > "); + char* s = calloc(sizeof(char), 100); + s[0] = '\0'; + char* propString = write_prop(line.proposition); + char* justString = write_just(line.justification); + snprintf(s, 100, "%d. %s BECAUSE: %s", i + 1, propString, justString); + free(propString); + free(justString); + consoleLines[i] = init_simple_consoleline(CWHITE, s); + } + set_cache(init_consolelineset(consoleLines, lines.count)); + /* enter main loop */ + while (1) { + draw_screen(); + int c = getch(); + if (c == 'j') { + move_selection(1); + } + if (c == 'k') { + move_selection(-1); } - write_prop(line.proposition); - printf(" BECAUSE: "); - write_just(line.justification); - printf(just_is_sound(line, lines) ? " " : " "); - printf(" [%d]", line.scope.id); - printf("\n"); } free(content); } -void write_prop(prop_t prop) { +char* write_prop(prop_t prop) { + char* out = calloc(sizeof(char), 100); + out[0] = '\0'; if (prop.isNegative) { - printf("%s", "!"); + strcat(out, "!"); } if (prop.isSimple) { - printf("%s", prop.name); + strcat(out, prop.name); } else { - printf("("); - write_prop(*(prop.propA)); - printf(" %s ", tttos(prop.relation)); - write_prop(*(prop.propB)); - printf(")"); + strcat(out, "("); + char* c1 = write_prop(*prop.propA); + strcat(out, c1); + free(c1); + strcat(out, " "); + strcat(out, tttos(prop.relation)); + strcat(out, " "); + char* c2 = write_prop(*prop.propB); + strcat(out, c2); + free(c2); + strcat(out, ")"); } + return out; } -void write_just(just_t just) { - printf("%s", jrtos(just.rule)); +char* write_just(just_t just) { + char* out = calloc(sizeof(char), 50); + out[0] = '\0'; + strcat(out, jrtos(just.rule)); for (int i = 0; i < just.referencesCount; i++) { - printf(" %d", just.references[i]); + char* n = calloc(sizeof(char), 5); + snprintf(n, 5, " %d", just.references[i]); + strcat(out, n); + free(n); } + return out; } char* read_file(char* path) { FILE* file = fopen(path, "r"); diff --git a/src/operetta.h b/src/operetta.h index 6d348ed..17f3728 100644 --- a/src/operetta.h +++ b/src/operetta.h @@ -2,8 +2,7 @@ #define OPERETTA #include "parse.h" #include "proposition.h" -void write_line(line_t line); -void write_prop(prop_t prop); -void write_just(just_t just); +char* write_prop(prop_t prop); +char* write_just(just_t just); char* read_file(char* path); #endif -- cgit v1.3.1