Add status bar

This commit is contained in:
Timothy Warren 2019-08-19 16:14:33 -04:00
parent c1f46771d7
commit ee63abd7ad
1 changed files with 84 additions and 6 deletions

90
kilo.c
View File

@ -44,12 +44,14 @@ typedef struct erow {
struct editorConfig {
int cx, cy;
int rx;
int rowoff;
int coloff;
int screenrows;
int screencols;
int numrows;
erow *row;
char *filename;
struct termios orig_termios;
};
@ -236,6 +238,23 @@ int getWindowSize(int *rows, int *cols)
/*** row operations ***/
int editorRowCxToRx(erow *row, int cx)
{
int rx = 0;
int j;
for (j = 0; j < cx; j++)
{
if (row->chars[j] == '\t')
{
rx += (KILO_TAB_STOP -1) - (rx % KILO_TAB_STOP);
}
rx++;
}
return rx;
}
void editorUpdateRow(erow *row)
{
int tabs = 0;
@ -292,6 +311,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)
{
@ -348,6 +370,12 @@ void abFree(struct abuf *ab)
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;
@ -360,12 +388,12 @@ void editorScroll()
if (E.cx < E.coloff)
{
E.coloff = E.cx;
E.coloff = E.rx;
}
if (E.cx >= E.coloff + E.screencols)
{
E.coloff = E.cx - E.screencols + 1;
E.coloff = E.rx - E.screencols + 1;
}
}
@ -424,11 +452,40 @@ void editorDrawRows(struct abuf *ab)
}
abAppend(ab, "\x1b[K", 3);
if (y < E.screenrows - 1)
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, "\r\n", 2);
abAppend(ab, rstatus, rlen);
break;
}
else
{
abAppend(ab, " ", 1);
len++;
}
}
abAppend(ab, "\x1b[m", 3);
}
void editorRefreshScreen()
@ -441,10 +498,11 @@ void editorRefreshScreen()
abAppend(&ab, "\x1b[H", 3);
editorDrawRows(&ab);
editorDrawStatusBar(&ab);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH",
(E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1);
(E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6);
@ -525,11 +583,27 @@ 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--)
{
@ -553,15 +627,19 @@ 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;
if (getWindowSize(&E.screenrows, &E.screencols) == -1)
{
die("getWindowSize");
}
E.screenrows -= 1;
}
int main(int argc, char *argv[])