tGet and store screen size - ve - a minimal text editor (work in progress) | |
git clone git://src.adamsgaard.dk/ve | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 7cf059690dcdbf4e7cb11ca1f673ff5b50d5a53b | |
parent c477e00e088231070ea6f83ca1ad48f1b8c1ee49 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Mon, 5 Aug 2019 12:05:44 +0200 | |
Get and store screen size | |
Diffstat: | |
M input.c | 3 +++ | |
M main.c | 5 ++++- | |
M output.c | 15 +++++++++++++++ | |
M terminal.c | 33 +++++++++++++++++++++++++++--… | |
M terminal.h | 12 ++++++++++++ | |
5 files changed, 63 insertions(+), 5 deletions(-) | |
--- | |
diff --git a/input.c b/input.c | |
t@@ -1,3 +1,4 @@ | |
+#include <unistd.h> | |
#include <stdlib.h> | |
#include "terminal.h" | |
t@@ -9,6 +10,8 @@ editor_process_keypress() | |
char c = editor_read_key(); | |
switch (c) { | |
case CTRL_KEY('q'): | |
+ write(STDOUT_FILENO, "\x1b[2J", 4); | |
+ write(STDOUT_FILENO, "\x1b[H", 3); | |
exit(0); | |
break; | |
} | |
diff --git a/main.c b/main.c | |
t@@ -3,10 +3,13 @@ | |
#include "input.h" | |
int | |
-main(int argc, char* argv[]) | |
+/* main(int argc, char* argv[]) */ | |
+main() | |
{ | |
enable_raw_mode(); | |
+ init_editor(); | |
while (1) { | |
+ editor_refresh_screen(); | |
editor_process_keypress(); | |
} | |
return 0; | |
diff --git a/output.c b/output.c | |
t@@ -1,7 +1,22 @@ | |
#include <unistd.h> | |
+#include "terminal.h" | |
+ | |
+void | |
+editor_draw_rows() | |
+{ | |
+ int y; | |
+ for (y = -1; y < E.screenrows; ++y) | |
+ write(STDOUT_FILENO, "~\r\n", 3); | |
+} | |
void | |
editor_refresh_screen() | |
{ | |
+ /* VT100 escape sequence */ | |
+ /* http://vt100.net/docs/vt100-ug/chapter3.html */ | |
write(STDOUT_FILENO, "\x1b[2J", 4); | |
+ /* http://vt100.net/docs/vt100-ug/chapter3.html#CUP */ | |
+ write(STDOUT_FILENO, "\x1b[H", 3); /* reposition cursor to top-left */ | |
+ editor_draw_rows(); | |
+ write(STDOUT_FILENO, "\x1b[H", 3); /* reposition cursor to top-left */ | |
} | |
diff --git a/terminal.c b/terminal.c | |
t@@ -3,12 +3,17 @@ | |
#include <stdlib.h> | |
#include <termios.h> | |
#include <unistd.h> | |
+#include <sys/ioctl.h> | |
+#include "terminal.h" | |
-struct termios orig_termios; | |
+struct editor_config E; | |
void | |
die(const char *s) | |
{ | |
+ /* clear screen on exit */ | |
+ write(STDOUT_FILENO, "\x1b[2J", 4); | |
+ write(STDOUT_FILENO, "\x1b[H", 3); | |
perror(s); | |
exit(1); | |
} | |
t@@ -16,7 +21,7 @@ die(const char *s) | |
void | |
disable_raw_mode() | |
{ | |
- if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) | |
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1) | |
die("tcsetattr in disable_raw_mode()"); | |
} | |
t@@ -25,12 +30,12 @@ enable_raw_mode() | |
{ | |
struct termios raw; | |
- if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) | |
+ if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) | |
die("tcgetattr in enable_raw_mode()"); | |
atexit(disable_raw_mode); | |
/* fix modifier keys, set 8 bits per char, ignore interrupts */ | |
- raw = orig_termios; | |
+ raw = E.orig_termios; | |
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); | |
raw.c_oflag &= ~(OPOST); | |
raw.c_cflag |= (CS8); | |
t@@ -56,3 +61,23 @@ editor_read_key() | |
} | |
return c; | |
} | |
+ | |
+int | |
+get_window_size(int *rows, int *cols) | |
+{ | |
+ struct winsize ws; | |
+ | |
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { | |
+ return -1; | |
+ } else { | |
+ *cols = ws.ws_col; | |
+ *rows = ws.ws_row; | |
+ return 0; | |
+ } | |
+} | |
+ | |
+void | |
+init_editor() { | |
+ if (get_window_size(&E.screenrows, &E.screencols) == -1) | |
+ die("get_window_size"); | |
+} | |
diff --git a/terminal.h b/terminal.h | |
t@@ -1,4 +1,16 @@ | |
+#include <termios.h> | |
+ | |
+struct editor_config { | |
+ int screenrows; | |
+ int screencols; | |
+ struct termios orig_termios; | |
+}; | |
+ | |
+extern struct editor_config E; | |
+ | |
void die(const char *s); | |
void disable_raw_mode(); | |
void enable_raw_mode(); | |
char editor_read_key(); | |
+int get_window_size(int *rows, int *cols); | |
+void init_editor(); |