Add message bar

This commit is contained in:
Timothy Warren 2019-08-19 16:21:50 -04:00
parent ee63abd7ad
commit 256ab17b63
1 changed files with 36 additions and 1 deletions

37
kilo.c
View File

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