tPlace cursor in active prompt - 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 6ee81f6740a010441cd11c915f941081c80cc9a3
(DIR) parent e19cbd3366fe14e6475845d5a686d2c938cf3b6b
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 6 Aug 2019 21:37:59 +0200
Place cursor in active prompt
Diffstat:
M input.c | 5 ++++-
M output.c | 13 +++++++++++--
M output.h | 1 +
3 files changed, 16 insertions(+), 3 deletions(-)
---
(DIR) diff --git a/input.c b/input.c
t@@ -1,6 +1,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
+#include <string.h>
#include "te.h"
#include "terminal.h"
#include "edit.h"
t@@ -26,6 +27,8 @@ editor_prompt(char *prompt)
while (1) {
editor_set_status_message(prompt, buf);
editor_refresh_screen();
+ editor_place_cursor(strlen(prompt) - 1 + strlen(buf),
+ E.screen_rows + 2);
c = editor_read_key();
if (c == CTRL_KEY('h') || c == 127) { /* detect backspace */
t@@ -41,7 +44,7 @@ editor_prompt(char *prompt)
return buf;
}
} else if (!iscntrl(c) && c < 128) {
- if (buflen == bufsize - 1) {
+ if (buflen >= bufsize - 1) {
bufsize *= 2;
buf = realloc(buf, bufsize);
}
(DIR) diff --git a/output.c b/output.c
t@@ -166,10 +166,12 @@ editor_draw_rows(struct abuf *ab)
void
editor_refresh_screen()
{
+ char buf[32];
+ struct abuf ab = ABUF_INIT;
+
show_status_message();
editor_scroll();
- struct abuf ab = ABUF_INIT;
ab_append(&ab, "\x1b[?25l", 6); /* hide cursor */
ab_append(&ab, "\x1b[H", 3); /* cursor to home */
t@@ -177,7 +179,6 @@ editor_refresh_screen()
editor_draw_status(&ab);
editor_draw_status_message(&ab);
- char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH",
(E.cursor_y - E.row_offset)+1,
(E.cursor_rx - E.column_offset)+1);
t@@ -189,6 +190,14 @@ editor_refresh_screen()
ab_free(&ab);
}
+void
+editor_place_cursor(int cx, int cy)
+{
+ char buf[128];
+ snprintf(buf, sizeof(buf), "\x1b[%d;%dH", cy, cx);
+ write(STDOUT_FILENO, buf, strlen(buf));
+}
+
/* set status message text, uses same format as printf */
void
editor_set_status_message(const char *fmt, ...)
(DIR) diff --git a/output.h b/output.h
t@@ -9,5 +9,6 @@ struct abuf {
void editor_refresh_screen();
void editor_set_status_message(const char *fmt, ...);
+void editor_place_cursor(int cx, int cy);
#endif