From 74df1685ed8ff0f6e6d31796da943d5de17eb7a4 Mon Sep 17 00:00:00 2001 From: Arthur Barraux Date: Sat, 21 Sep 2024 08:59:09 +0200 Subject: [PATCH] project architecture --- test/Makefile => Makefile | 2 +- test/main.c => main.c | 49 ++++++++++++++++++++++++++++----------- main.h | 4 ++++ 3 files changed, 40 insertions(+), 15 deletions(-) rename test/Makefile => Makefile (82%) rename test/main.c => main.c (63%) create mode 100644 main.h diff --git a/test/Makefile b/Makefile similarity index 82% rename from test/Makefile rename to Makefile index c76584c..e05496b 100644 --- a/test/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ # @file # @version 0.1 -main: main.c +app: main.c main.h $(CC) main.c -o main -Wall -Wextra -pedantic # end diff --git a/test/main.c b/main.c similarity index 63% rename from test/main.c rename to main.c index 35706b5..e8438b8 100644 --- a/test/main.c +++ b/main.c @@ -1,4 +1,4 @@ -#include +#include "main.h" #include #include #include @@ -14,6 +14,7 @@ struct termios orig_termios; /* terminal */ void die(const char *s) { + wipeScreen(); perror(s); exit(1); } @@ -43,26 +44,46 @@ void enableRawMode() { } } +char editorReadKey() { + int nread; + char c; + while ((nread = read(STDIN_FILENO, &c, 1)) != 1) { + if (nread == -1 && errno != EAGAIN) { + die("read"); + } + } + return c; +} + +/* output */ + +void wipeScreen() { + /* Clear the screen */ + write(STDOUT_FILENO, "\x1b[2J", 4); + /* Put the cursor at the top left corner */ + write(STDOUT_FILENO, "\x1b[H", 3); +} + +/* input */ +void editorProcessKeypress() { + char c = editorReadKey(); + + switch (c) { + case CTRL_KEY('q'): + exit(0); + break; + } +} + /* init */ int main() { - char c; enableRawMode(); while (1) { - c = '\0'; - if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) { - die("read"); - } - if (iscntrl(c)) { - printf("%d\r\n", c); - } else { - printf("%d ('%c')\r\n", c, c); - } - if (c == CTRL_KEY('q')) { - break; - } + wipeScreen(); + editorProcessKeypress(); } return 0; } diff --git a/main.h b/main.h new file mode 100644 index 0000000..2f87483 --- /dev/null +++ b/main.h @@ -0,0 +1,4 @@ +#ifndef MAIN_H +#define MAIN_H +void wipeScreen(); +#endif