Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Jan 15, 2023
1 parent d57f8ec commit 9303f8b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion kilo.c
Expand Up @@ -6,11 +6,13 @@

#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdarg.h>
#include <termios.h>
#include <unistd.h>

Expand Down Expand Up @@ -52,6 +54,8 @@ struct editorConfig {
int numrows;
erow *row;
char *filename;
char statusmsg[80];
time_t statusmsg_time;
struct termios orig_termios;
};

Expand Down Expand Up @@ -434,6 +438,15 @@ void editorDrawStatusBar(struct abuf *ab) {
}
}
abAppend(ab, "\x1b[m", 3);
abAppend(ab, "\r\n", 2);
}

void editorDrawMessageBar(struct abuf *ab) {
abAppend(ab, "\x1b[K", 3);
int msglen = strlen(E.statusmsg);
if (msglen > E.screencols) msglen = E.screencols;
if (msglen && time(NULL) - E.statusmsg_time < 5)
abAppend(ab, E.statusmsg, msglen);
}

void editorRefreshScreen() {
Expand All @@ -445,6 +458,7 @@ void editorRefreshScreen() {

editorDrawRows(&ab);
editorDrawStatusBar(&ab);
editorDrawMessageBar(&ab);

char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
Expand All @@ -457,6 +471,14 @@ void editorRefreshScreen() {
abFree(&ab);
}

void editorSetStatusMessage(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
va_end(ap);
E.statusmsg_time = time(NULL);
}

/*** init ***/

void initEditor() {
Expand All @@ -468,9 +490,11 @@ void initEditor() {
E.numrows = 0;
E.row = NULL;
E.filename = NULL;
E.statusmsg[0] = '\0';
E.statusmsg_time = 0;

if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
E.screenrows -= 1;
E.screenrows -= 2;
}

int main(int argc, char *argv[]) {
Expand All @@ -480,6 +504,8 @@ int main(int argc, char *argv[]) {
editorOpen(argv[1]);
}

editorSetStatusMessage("HELP: Ctrl-Q = quit");

while (1) {
editorRefreshScreen();
editorProcessKeyPress();
Expand Down

0 comments on commit 9303f8b

Please sign in to comment.