Add message bar

This commit is contained in:
Timothy Warren 2019-08-19 16:21:50 -04:00
parent ee63abd7ad
commit 256ab17b63

37
kilo.c
View File

@ -7,11 +7,13 @@
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
/*** defines ***/
@ -52,6 +54,8 @@ struct editorConfig {
int numrows;
erow *row;
char *filename;
char statusmsg[80];
time_t statusmsg_time;
struct termios orig_termios;
};
@ -486,6 +490,23 @@ 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()
@ -499,6 +520,7 @@ void editorRefreshScreen()
editorDrawRows(&ab);
editorDrawStatusBar(&ab);
editorDrawMessageBar(&ab);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH",
@ -511,6 +533,15 @@ 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);
}
/*** input ***/
void editorMoveCursor(int key)
@ -633,13 +664,15 @@ 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[])
@ -651,6 +684,8 @@ int main(int argc, char *argv[])
editorOpen(argv[1]);
}
editorSetStatusMessage("HELP: Ctrl-Q = quit");
while (1)
{
editorRefreshScreen();