diff --git a/kilo.c b/kilo.c index 05dca6d..9308feb 100644 --- a/kilo.c +++ b/kilo.c @@ -1,29 +1,36 @@ -#include #include #include #include #include #include +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) struct termios orig_termios; -void editor_clear_screen() { - write(STDIN_FILENO, "\x1b[2J", 4); - write(STDIN_FILENO, "\x1b[H", 3); -} +int main() { + if (!isatty(STDIN_FILENO)) { + printf("kilo only supports a terminal at standard in. Exiting."); + exit(1); + } -void die(const char *s) { - editor_clear_screen(); + enable_raw_mode(); + while (1) { + editor_redraw_screen(); + editor_process_key(); + } - perror(s); - exit(1); -} - -void disable_raw_mode() { - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) - die("tcsetattr"); + return 0; } void enable_raw_mode() { @@ -36,20 +43,9 @@ void enable_raw_mode() { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr"); } -char editor_read_key() { - char c = '\0'; - - 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 disable_raw_mode() { + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) + die("tcsetattr"); } void editor_redraw_screen() { @@ -60,6 +56,18 @@ void editor_redraw_screen() { 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() { char c; read(STDIN_FILENO, &c, 1); @@ -75,17 +83,9 @@ void editor_process_key() { } } -int main() { - if (!isatty(STDIN_FILENO)) { - printf("kilo only supports a terminal at standard in. Exiting."); - exit(1); - } +void die(const char *s) { + editor_clear_screen(); - enable_raw_mode(); - while (1) { - editor_redraw_screen(); - editor_process_key(); - } - - return 0; + perror(s); + exit(1); }