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 <stdio.h>
#include <stdlib.h>
#include <termios.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)
struct termios orig_termios;
void editor_clear_screen() {
write(STDIN_FILENO, "\x1b[2J", 4);
write(STDIN_FILENO, "\x1b[H", 3);
}
void die(const char *s) {
editor_clear_screen();
perror(s);
int main() {
if (!isatty(STDIN_FILENO)) {
printf("kilo only supports a terminal at standard in. Exiting.");
exit(1);
}
}
void disable_raw_mode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
enable_raw_mode();
while (1) {
editor_redraw_screen();
editor_process_key();
}
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.");
void die(const char *s) {
editor_clear_screen();
perror(s);
exit(1);
}
enable_raw_mode();
while (1) {
editor_redraw_screen();
editor_process_key();
}
return 0;
}