Archived
1
Fork 0

Just moved some lines around

This commit is contained in:
Hadeed 2023-08-24 04:27:25 +05:00
parent 8b20212ee2
commit 2ded534ff7

80
kilo.c
View file

@ -1,29 +1,36 @@
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
void enable_raw_mode();
void disable_raw_mode();
void editor_redraw_screen();
void editor_clear_screen();
void editor_draw_rows();
void editor_process_key();
void die(const char *s);
#define CTRL_KEY(key) ((key) & 0x1f) #define CTRL_KEY(key) ((key) & 0x1f)
struct termios orig_termios; struct termios orig_termios;
void editor_clear_screen() { int main() {
write(STDIN_FILENO, "\x1b[2J", 4); if (!isatty(STDIN_FILENO)) {
write(STDIN_FILENO, "\x1b[H", 3); printf("kilo only supports a terminal at standard in. Exiting.");
}
void die(const char *s) {
editor_clear_screen();
perror(s);
exit(1); exit(1);
} }
void disable_raw_mode() { enable_raw_mode();
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) while (1) {
die("tcsetattr"); editor_redraw_screen();
editor_process_key();
}
return 0;
} }
void enable_raw_mode() { void enable_raw_mode() {
@ -36,20 +43,9 @@ void enable_raw_mode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr"); if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
} }
char editor_read_key() { void disable_raw_mode() {
char c = '\0'; if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
while (c == '\0')
read(STDIN_FILENO, &c, 1);
return c;
}
void editor_draw_rows() {
int rows = 24;
for (int y = 0; y < rows - 1; y++)
write(STDIN_FILENO, "~\r\n", 3);
write(STDIN_FILENO, "~", 1);
} }
void editor_redraw_screen() { void editor_redraw_screen() {
@ -60,6 +56,18 @@ void editor_redraw_screen() {
write(STDIN_FILENO, "\x1b[H", 3); write(STDIN_FILENO, "\x1b[H", 3);
} }
void editor_clear_screen() {
write(STDIN_FILENO, "\x1b[2J", 4);
write(STDIN_FILENO, "\x1b[H", 3);
}
void editor_draw_rows() {
int rows = 24;
for (int y = 0; y < rows - 1; y++)
write(STDIN_FILENO, "~\r\n", 3);
write(STDIN_FILENO, "~", 1);
}
void editor_process_key() { void editor_process_key() {
char c; char c;
read(STDIN_FILENO, &c, 1); read(STDIN_FILENO, &c, 1);
@ -75,17 +83,9 @@ void editor_process_key() {
} }
} }
int main() { void die(const char *s) {
if (!isatty(STDIN_FILENO)) { editor_clear_screen();
printf("kilo only supports a terminal at standard in. Exiting.");
perror(s);
exit(1); exit(1);
}
enable_raw_mode();
while (1) {
editor_redraw_screen();
editor_process_key();
}
return 0;
} }