From a73bbecfcc8f562b8ff65b4be4f16ccc668a02f2 Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Mon, 11 Sep 2023 13:53:28 +0500 Subject: [PATCH] Move cursor to prompt --- include/buffer.h | 3 --- include/cursor.h | 2 -- src/kilo.c | 9 ++++++++- src/terminal.c | 9 ++++----- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/buffer.h b/include/buffer.h index 28cc892..ecabe75 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -7,8 +7,6 @@ #include "erow.h" #include "utils.h" -struct erow; - struct buffer { char *filename; @@ -30,5 +28,4 @@ struct erow *buffer_get_crow(struct buffer *buffer); size_t buffer_get_crow_len(struct buffer *buffer); void buffer_free(struct buffer *buffer); - #endif // BUFFER_H diff --git a/include/cursor.h b/include/cursor.h index 636aef6..c7a9c41 100644 --- a/include/cursor.h +++ b/include/cursor.h @@ -3,8 +3,6 @@ #include "buffer.h" -struct buffer; - void cursor_move(struct buffer *buffer, int dx, int dy); #endif // CURSOR_H diff --git a/src/kilo.c b/src/kilo.c index 449c057..ac74867 100644 --- a/src/kilo.c +++ b/src/kilo.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -58,18 +59,24 @@ void editor_set_message(const char *fmt, ...) { E.message_time = time(NULL); } -// TODO: Implement a fully featured line editor here +// TODO: This could use some work char *editor_prompt(const char *prompt) { size_t buf_cap = 64; size_t buf_size = 0; char *buf = malloc(buf_cap); + char *prompt_prefix = strstr(prompt, "%s"); + int prefix_len = (prompt_prefix ? prompt_prefix - prompt : 0); + buf[buf_size] = '\0'; while (true) { editor_set_message(prompt, buf); ui_draw_screen(); + if (prompt_prefix) + terminal_set_cursor_pos(E.screenrows + 2, prefix_len + buf_size + 1); + KEY key = terminal_read_key(); switch (key) { case BACKSPACE: diff --git a/src/terminal.c b/src/terminal.c index 7166b24..76646cd 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -1,8 +1,3 @@ -// IWYU pragma: no_include -// IWYU pragma: no_include -// IWYU pragma: no_include -// IWYU pragma: no_include - #include #include #include @@ -158,3 +153,7 @@ KEY terminal_read_key(void) { return (KEY) c; } +// IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include