From 9fe7f3593296b4113ca1f2028cec93d8140374be Mon Sep 17 00:00:00 2001 From: jmug Date: Mon, 26 Oct 2026 11:31:55 -0700 Subject: [PATCH] feat: Display welcome message. --- kilo.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/kilo.c b/kilo.c index ffa860c..4ecd316 100644 --- a/kilo.c +++ b/kilo.c @@ -13,6 +13,7 @@ /*** defines ***/ #define CTRL_KEY(k) ((k) & 0x1f) +#define KILO_VERSION "0.0.1" /*** data ***/ @@ -146,8 +147,30 @@ void abFree(struct abuf *ab) { void editorDrawRows(struct abuf *ab) { for (int y = 0; y < E.screenrows; y++) { - abAppend(ab, "~", 1); + if (y == E.screenrows / 3) { + char welcome[80]; + int welcomelen = snprintf( + welcome, + sizeof(welcome), + "Kil editor -- version %s", + KILO_VERSION + ); + if (welcomelen > E.screencols) + welcomelen = E.screencols; + int padding = (E.screencols - welcomelen) / 2; + if (padding) { + abAppend(ab, "~", 1); + padding--; + } + while (padding--) + abAppend(ab, " ", 1); + abAppend(ab, welcome, welcomelen); + } else { + abAppend(ab, "~", 1); + } + // Erase the line content to the right of the cursor. + abAppend(ab, "\x1b[K", 3); // No new line on the last line, the terminal scrolls past the // first line otherwise. if (y < E.screenrows - 1) { @@ -158,8 +181,8 @@ void editorDrawRows(struct abuf *ab) { void editorRefreshScreen() { struct abuf ab = ABUF_INIT; - // Clear the whole screen. - abAppend(&ab, "\x1b[2J", 4); + // Hide cursor. + abAppend(&ab, "\x1b[?25l", 6); // Move cursor to top left. abAppend(&ab, "\x1b[H", 3); @@ -167,6 +190,8 @@ void editorRefreshScreen() { // Return cursor to top left after drawing. abAppend(&ab, "\x1b[H", 3); + // Show cursor again. + abAppend(&ab, "\x1b[?25h", 6); write(STDOUT_FILENO, ab.b, ab.len); abFree(&ab);