tProceed to step 36 - ve - a minimal text editor (work in progress)
(HTM) git clone git://src.adamsgaard.dk/ve
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 2d8b8379da3585b1fa5537790ef6bcc84f6f3726
(DIR) parent 7cf059690dcdbf4e7cb11ca1f673ff5b50d5a53b
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Mon, 5 Aug 2019 12:24:42 +0200
Proceed to step 36
Diffstat:
M output.c | 7 +++++--
M output.h | 6 ++++++
M terminal.c | 33 ++++++++++++++++++++++++++++++-
3 files changed, 43 insertions(+), 3 deletions(-)
---
(DIR) diff --git a/output.c b/output.c
t@@ -5,8 +5,11 @@ void
editor_draw_rows()
{
int y;
- for (y = -1; y < E.screenrows; ++y)
- write(STDOUT_FILENO, "~\r\n", 3);
+ for (y = -1; y < E.screenrows; ++y) {
+ write(STDOUT_FILENO, "~", 1);
+ if (y < E.screenrows - 1)
+ write(STDOUT_FILENO, "\r\n", 2);
+ }
}
void
(DIR) diff --git a/output.h b/output.h
t@@ -1 +1,7 @@
+struct abuf {
+ char *b;
+ int len;
+};
+#define ABUF_INIT {NULL, 0}
+
void editor_refresh_screen();
(DIR) diff --git a/terminal.c b/terminal.c
t@@ -1,3 +1,4 @@
+#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
t@@ -62,13 +63,43 @@ editor_read_key()
return c;
}
+/* get screen size by moving cursor and get current position */
+int
+get_cursor_position(int *rows, int *cols) {
+ char buf[32];
+ unsigned int i;
+
+ if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4)
+ return -1;
+
+ i = 0;
+ while (i < sizeof(buf) - 1) {
+ if (read(STDIN_FILENO, &buf[i], 1) != 1)
+ break;
+ if (buf[i] == 'R')
+ break;
+ ++i;
+ }
+ buf[i] = '\0';
+
+ if (buf[0] != '\x1b' || buf[1] != '[')
+ return -1;
+ if (scanf(&buf[2], "%d;%d", rows, cols) != 2)
+ return -1;
+ /* editor_read_key(); */
+ return 0;
+}
+
int
get_window_size(int *rows, int *cols)
{
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
- return -1;
+ /* fallback screen size detection */
+ if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12)
+ return -1;
+ return get_cursor_position(rows, cols);
} else {
*cols = ws.ws_col;
*rows = ws.ws_row;