add message bar

This commit is contained in:
SowinskiBraeden committed 2022-09-06 21:21:36 -07:00
1 parent 316f2b05ad
commit 50d9fa53e9
1 file changed
+82 -9
+82 -9
View File
@@ -9,11 +9,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 ***/
@@ -40,19 +42,23 @@ enum editorKey {
// erow stands for "editor row"
typedef struct erow {
int size;
int resize;
int rsize;
char *chars;
char *render;
} erow;
struct editorConfig {
int cx, cy;
int rx;
int rowoff;
int coloff;
int screenrows;
int screencols;
int numrows;
erow *row;
char *filename;
char statusmsg[80];
time_t statusmsg_time;
struct termios orig_termios;
};
@@ -182,6 +188,15 @@ int getWindowSize(int *rows, int *cols) {
/*** row operations ***/
int editorRowCxToRx(erow *row, int cx) {
int rx = 0;
for (int j = 0; j < cx; j++) {
if (row->chars[j] == '\t') rx += (LEVEL_TAB_STOP - 1) - (rx % LEVEL_TAB_STOP);
rx++;
}
return rx;
}
void editorUpdateRow(erow *row) {
int tabs = 0;
for (int j = 0; j < row->size; j++) {
@@ -189,13 +204,13 @@ void editorUpdateRow(erow *row) {
}
free(row->render);
row->render = malloc(row->size + tabs*(LEVEL_TAB_STOP - 1) + 1);
row->render = (char*)malloc(row->size + tabs*(LEVEL_TAB_STOP - 1) + 1);
int idx = 0;
for (int j = 0; j < row->size; j++) {
if (row->chars[j] == '\t') {
row->render[idx++] = ' ';
while (idx % LEVEL_TAB_STOP != 0) row->render(idx++) = ' ';
while (idx % LEVEL_TAB_STOP != 0) row->render[idx++] = ' ';
} else {
row->render[idx++] = row->chars[j];
}
@@ -223,6 +238,9 @@ void editorAppendRow(char *s, size_t len) {
/*** file i/o ***/
void editorOpen(char *filename) {
free(E.filename);
E.filename = strdup(filename);
FILE *fp = fopen(filename, "r");
if (!fp) die("fopen");
@@ -265,10 +283,13 @@ void abFree(struct abuf *ab) {
/*** output ***/
void editorScroll() {
E.rx = 0;
if (E.cy < E.numrows) E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
if (E.cy < E.rowoff) E.rowoff = E.cy;
if (E.cy >= E.rowoff + E.screenrows) E.rowoff = E.cy - E.screenrows + 1;
if (E.cx < E.coloff) E.coloff = E.cx;
if (E.cx >= E.coloff + E.screencols) E.coloff = E.cx - E.screencols + 1;
if (E.cx < E.coloff) E.coloff = E.rx;
if (E.cx >= E.coloff + E.screencols) E.coloff = E.rx - E.screencols + 1;
}
void editorDrawRows(struct abuf *ab) {
@@ -298,24 +319,55 @@ void editorDrawRows(struct abuf *ab) {
}
abAppend(ab, "\x1b[K", 3);
if (y < E.screenrows - 1) abAppend(ab, "\r\n", 2);
abAppend(ab, "\r\n", 2);
}
}
void editorDrawStatusBar(struct abuf* ab) {
abAppend(ab, "\x1b[7m", 4);
char status[80], rstatus[80];
int len = snprintf(status, sizeof(status), "%.20s - %d lines",
E.filename ? E.filename : "[No Name]", E.numrows);
int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d",
E.cy + 1, E.numrows);
if (len > E.screencols) len = E.screencols;
abAppend(ab, status, len);
int len = 0;
while (len < E.screencols) {
if (E.screencols - len == rlen) {
abAppend(ab, rstatus, rlen);
break;
} else {
abAppend(ab, " ", 1);
len++;
}
}
abAppend(ab, "\x1b[m", 3);
abAppend(ab, "\r\n", 2);
}
void editorDrawMessageBar(struct abuf* ab) {
abAppend(ab, "\x1[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() {
editorScroll();
struct abuf ab = ABUF_INIT;
abAppend(&ab, "\x1b[?25l", 6);
// abAppend(&ab, "\x1b[2J", 4); This line clears whole screen
abAppend(&ab, "\x1b[H", 3);
editorDrawRows(&ab);
editorDrawStatusBar(&ab);
editorDrawMessageBar(&ab);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
(E.cx - E.coloff) + 1);
(E.rx - E.coloff) + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6);
@@ -324,6 +376,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);
}
/*** input ***/
void editorMoveCursor(int key) {
@@ -365,12 +425,18 @@ void editorProcessKeypress() {
break;
case END_KEY:
E.cx = E.screencols - 1;
if (E.cy < E.numrows) E.cx = E.row[E.cy].size;
break;
case PAGE_UP:
case PAGE_DOWN:
{
if (c == PAGE_UP) E.cy = E.rowoff;
else if (c == PAGE_DOWN) {
E.cy = E.rowoff + E.screenrows - 1;
if (E.cy > E.numrows) E.cy = E.numrows;
}
int times = E.screenrows;
while (times--)
editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
@@ -391,12 +457,17 @@ void editorProcessKeypress() {
void initEditor() {
E.cx = 0;
E.cy = 0;
E.rx = 0;
E.rowoff = 0;
E.coloff = 0;
E.numrows = 0;
E.row = NULL;
E.filename = NULL;
E.statusmsg = '\0';
E.statusmsg_time = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
E.screenrows -= 2;
}
int main(int argc, char *argv[]) {
@@ -404,6 +475,8 @@ int main(int argc, char *argv[]) {
initEditor();
if (argc >= 2) editorOpen(argv[1]);
editorSetStatusMessage("HELP: Ctrl-Q = quit");
while (1) {
editorRefreshScreen();
editorProcessKeypress();