summaryrefslogtreecommitdiff
path: root/src/console.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-06-17 17:46:10 -0700
committerKai Stevenson <kai@kaistevenson.com>2023-06-17 17:46:10 -0700
commitd201bf2bf253c80a8629f11e44da9daca67470aa (patch)
tree25aff8873aba00ec130b2fb3a0879085ada1313a /src/console.c
parent55a781a696524d9a00ad162d00555db6df76e32c (diff)
console library works now
Diffstat (limited to 'src/console.c')
-rw-r--r--src/console.c22
1 files changed, 15 insertions, 7 deletions
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();