mild refactoring, clean up windows properly upon exit

This commit is contained in:
2025-08-12 09:21:36 -04:00
parent 790e60d23f
commit 7fb5978b34
5 changed files with 46 additions and 40 deletions

View File

@@ -21,7 +21,7 @@ EXECUTABLE := $(TARGET)
CC := gcc
LD := gcc
CFLAGS := -std=c99 -pedantic -Wall -Wextra -O2 -ggdb
CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -O2 -ggdb
LDFLAGS := $(CFLAGS) -lncurses
all: $(EXECUTABLE)

View File

@@ -3,30 +3,11 @@
#include <curses.h>
#include <string.h>
#include <locale.h>
#include "globals.h"
#include "screens.h"
const unsigned int GETCH_TIMEOUT = 10; /* in ms */
char error_message[MAX_ERROR_MESSAGE_SIZE];
static void draw_screen (struct Screen *screen, char *input) {
screen->draw_screen(screen, input);
box(screen->win, 0, 0);
/* wattron(screen->win, A_STANDOUT); */
mvwprintw(screen->win, 0, 1, " %s ", screen->name);
/* wattroff(screen->win, A_STANDOUT); */
}
int main() {
initscr(); /* start ncurses */
noecho(); /* hide keyboard input */
curs_set(0); /* disable cursor */
timeout(GETCH_TIMEOUT); /* set timeout for getch() */
setlocale(LC_CTYPE, ""); /* set locale, UTF8 support is enabled here */
unsigned int terminal_width;
unsigned int terminal_height;
unsigned int old_terminal_width = 0;
@@ -35,32 +16,17 @@ int main() {
enum Status status = STATUS_NEED_REFRESH; /* refresh screens[active_screen].window on first loop */
char input = ' ';
strcpy(error_message, "");
getmaxyx(stdscr, terminal_height, terminal_width);
unsigned int screen_before_error = HOME;
enum ActiveScreen active_screen = HOME;
/* could be it's own init func within the screen's file. */
screens[HOME].win = newwin(terminal_height, terminal_width, 0, 0);
screens[ERROR].win = newwin(terminal_height, terminal_width, 0, 0);
static void draw_screen (struct Screen *screen, char *input) {
screen->draw_screen(screen, input);
wattron(screen->win, A_STANDOUT);
mvwprintw(screen->win, 0, 1, " %s ", screen->name);
wattroff(screen->win, A_STANDOUT);
}
/* main event loop */
while (status != STATUS_QUIT) {
if ((input = getch())) { /* if any input was read */
/* TODO: factor out input to another function */
if (input == 'q') {
status = STATUS_QUIT;
} else {
status = STATUS_NEED_REFRESH;
};
};
getmaxyx(stdscr, terminal_height, terminal_width);
/* resize active screen's window */
if (old_terminal_width != terminal_width || old_terminal_height != terminal_height) {
static void resize_active_screen() {
wclear(screens[active_screen].win);
wresize(screens[active_screen].win, terminal_height, terminal_width);
mvwin(screens[active_screen].win, 0, 0);
@@ -78,6 +44,41 @@ int main() {
active_screen = screen_before_error;
};
status = STATUS_NEED_REFRESH;
}
int main() {
initscr(); /* start ncurses */
noecho(); /* hide keyboard input */
curs_set(0); /* disable cursor */
keypad(stdscr, TRUE); /* enable extra keys */
timeout(GETCH_TIMEOUT); /* set timeout for getch() */
setlocale(LC_CTYPE, ""); /* set locale to current locale */
strcpy(error_message, "");
getmaxyx(stdscr, terminal_height, terminal_width);
/* could be it's own init func within the screen's file. */
screens[HOME].win = newwin(terminal_height, terminal_width, 0, 0);
screens[ERROR].win = newwin(terminal_height, terminal_width, 0, 0);
/* main event loop */
while (status != STATUS_QUIT) {
if ((input = getch())) { /* if any input was read */
/* TODO: factor out input to another function */
if (input == 'q') {
status = STATUS_QUIT;
} else {
status = STATUS_NEED_REFRESH;
};
};
getmaxyx(stdscr, terminal_height, terminal_width);
if (old_terminal_width != terminal_width || old_terminal_height != terminal_height) {
resize_active_screen();
};
/* refresh active screen's window */
@@ -90,7 +91,8 @@ int main() {
};
/* clean up */
delwin(screens[active_screen].win); /* TODO: delete all windows in screens[] */
delwin(screens[0].win); /* could be in a destructor function in the screen's file */
delwin(screens[1].win);
nocbreak();
endwin(); /* ends curses mode */
curs_set(1);

View File

@@ -3,6 +3,8 @@
#include <curses.h>
#include "screens.h"
void draw_error(struct Screen *screen, __attribute__((unused)) char *input) {
/* char *input is unused */
void draw_error(struct Screen *screen, char *input) {
box(screen->win, 0, 0);
mvwprintw(screen->win, 1, 2, "%s", error_message);
}

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: MIT */
#define GETCH_TIMEOUT 10 /* in ms */
#define MAX_SCREEN_NAME_SIZE 20 /* size of the screen name array */
#define MAX_ERROR_MESSAGE_SIZE 60 /* size of error_message array */

View File

@@ -14,12 +14,13 @@ void draw_home(struct Screen *screen, char *input) {
" \\__,_|_| |_| |_|_.__/ \\___|_| |___/ .__/|_|\\__,_|\\___\\___(_)_| |_|\\___|\\__|\n"
" | | \n"
" |_| \n";
mvwprintw(screen->win, 1, 2, "Thank you for visiting:");
mvwprintw(screen->win, 2, 1, "%s", banner);
mvwprintw(screen->win, 10, 1, "Your current input is: %s", input);
wclrtoeol(screen->win); /* clear rest of line to prevent longer inputs from sticking around */
mvwprintw(screen->win, 11, 1, "Git hash: %s", GIT_HASH);
mvwprintw(screen->win, 12, 1, "Git tag: %s", GIT_TAG);
mvwprintw(screen->win, 13, 1, "CC version: %s", CC_VERSION);
mvwprintw(screen->win, 14, 1, "Build date: %s", BUILD_DATE);
box(screen->win, 0, 0);
}