feat: Display welcome message.

This commit is contained in:
jmug
2026-10-26 11:31:55 -07:00
parent 49cb6ab62a
commit 9fe7f35932
+27 -2
View File
@@ -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++) {
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);