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