project architecture
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
# @file
|
# @file
|
||||||
# @version 0.1
|
# @version 0.1
|
||||||
|
|
||||||
main: main.c
|
app: main.c main.h
|
||||||
$(CC) main.c -o main -Wall -Wextra -pedantic
|
$(CC) main.c -o main -Wall -Wextra -pedantic
|
||||||
|
|
||||||
# end
|
# end
|
||||||
+35
-14
@@ -1,4 +1,4 @@
|
|||||||
#include <ctype.h>
|
#include "main.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -14,6 +14,7 @@ struct termios orig_termios;
|
|||||||
/* terminal */
|
/* terminal */
|
||||||
|
|
||||||
void die(const char *s) {
|
void die(const char *s) {
|
||||||
|
wipeScreen();
|
||||||
perror(s);
|
perror(s);
|
||||||
exit(1);
|
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 */
|
/* init */
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char c;
|
|
||||||
|
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
c = '\0';
|
wipeScreen();
|
||||||
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) {
|
editorProcessKeypress();
|
||||||
die("read");
|
|
||||||
}
|
|
||||||
if (iscntrl(c)) {
|
|
||||||
printf("%d\r\n", c);
|
|
||||||
} else {
|
|
||||||
printf("%d ('%c')\r\n", c, c);
|
|
||||||
}
|
|
||||||
if (c == CTRL_KEY('q')) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user