statusbars

This commit is contained in:
Tyrel Souza 2021-10-21 01:13:33 -04:00
parent 1a5dd5dcc3
commit ddbc8d6346
2 changed files with 75 additions and 8 deletions

View File

@ -5,3 +5,5 @@ https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#vertical-scrolling
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#horizontal-scrolling https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#horizontal-scrolling
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#tabs-and-the-cursor https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#tabs-and-the-cursor
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#scrolling-with-page-up-and-page-down https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#scrolling-with-page-up-and-page-down
https://viewsourcecode.org/snaptoken/kilo/05.aTextEditor.html

81
tted.c
View File

@ -6,11 +6,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 */
@ -48,6 +50,9 @@ struct editorConfig {
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;
}; };
struct editorConfig E; struct editorConfig E;
@ -176,14 +181,15 @@ int getWindowSize(int *rows, int *cols) {
int editorRowCxToRx(erow *row, int cx) { int editorRowCxToRx(erow *row, int cx) {
int rx = 0; int rx = 0;
int j; int j;
for (j=0; j < cx; j++){ for (j = 0; j < cx; j++) {
if (row->chars[j] == '\t') { if (row->chars[j] == '\t') {
rx += (TTED_TAB_STOP -1) - (rx % TTED_TAB_STOP); rx += (TTED_TAB_STOP - 1) - (rx % TTED_TAB_STOP);
} }
rx++; rx++;
} }
return rx; return rx;
} }
void editorUpdateRow(erow *row) { void editorUpdateRow(erow *row) {
int tabs = 0; int tabs = 0;
int j; int j;
@ -194,7 +200,7 @@ void editorUpdateRow(erow *row) {
} }
free(row->render); free(row->render);
row->render = malloc(row->size + tabs*(TTED_TAB_STOP - 1) + 1); row->render = malloc(row->size + tabs * (TTED_TAB_STOP - 1) + 1);
int idx = 0; int idx = 0;
for (j = 0; j < row->size; j++) { for (j = 0; j < row->size; j++) {
@ -226,6 +232,8 @@ 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");
@ -267,7 +275,7 @@ void abFree(struct abuf *ab) {
/** output */ /** output */
void editorScroll() { void editorScroll() {
E.rx = 0; E.rx = 0;
if (E.cy < E.numrows){ if (E.cy < E.numrows) {
E.rx = editorRowCxToRx(&E.row[E.cy], E.cx); E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
} }
@ -314,12 +322,42 @@ 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);
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, "\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() {
editorScroll(); editorScroll();
@ -327,6 +365,8 @@ void editorRefreshScreen() {
abAppend(&ab, "\x1b[?25l", 6); abAppend(&ab, "\x1b[?25l", 6);
abAppend(&ab, "\x1b[H", 3); // Cursor Position home abAppend(&ab, "\x1b[H", 3); // Cursor Position home
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, (E.rx - E.coloff) + 1); snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
@ -337,6 +377,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) {
@ -393,11 +441,21 @@ 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);
@ -421,7 +479,12 @@ void initEditor() {
E.coloff = 0; E.coloff = 0;
E.numrows = 0; E.numrows = 0;
E.row = NULL; E.row = NULL;
E.filename = NULL;
E.statusmsg[0] = '\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[]) {
@ -431,6 +494,8 @@ int main(int argc, char *argv[]) {
editorOpen(argv[1]); editorOpen(argv[1]);
} }
editorSetStatusMessage("HELP: Ctrl-Q = quit");
while (1) { while (1) {
editorRefreshScreen(); editorRefreshScreen();
editorProcessKeypress(); editorProcessKeypress();