Status bar / editing / saving file

This commit is contained in:
Arthur Barraux
2024-10-08 14:57:59 +02:00
parent 297eef4ce2
commit 1c22c3beca
15 changed files with 334 additions and 27 deletions
+80 -6
View File
@@ -1,4 +1,8 @@
#include "../include/output.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
void editorDrawRows(struct editorConfig *E, struct abuf *ab) {
int y;
@@ -30,18 +34,77 @@ void editorDrawRows(struct editorConfig *E, struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else {
len = E->row[file_row].size > E->screencols ? E->screencols
: E->row[y].size;
abAppend(ab, E->row[file_row].chars, len);
len = E->row[file_row].rsize - E->col_offset;
if (len < 0)
len = 0;
if (len > E->screencols)
len = E->screencols;
abAppend(ab, &E->row[file_row].render[E->col_offset], len);
}
abAppend(ab, ERASE_END_LINE, 3);
if (y < E->screenrows - 1) {
abAppend(ab, "\r\n", 2);
abAppend(ab, "\r\n", 2);
}
}
void editorScroll(struct editorConfig *E) {
E->rx = E->cursor_x;
if (E->cursor_y < E->numrows) {
E->rx = editorRowCxToRx(&E->row[E->cursor_y], E->cursor_x);
}
if (E->cursor_y < E->row_offset) {
E->row_offset = E->cursor_y;
}
if (E->cursor_y >= E->row_offset + E->screenrows) {
E->row_offset = E->cursor_y - E->screenrows + 1;
}
if (E->rx < E->col_offset) {
E->col_offset = E->rx;
}
if (E->rx >= E->col_offset + E->screencols) {
E->col_offset = E->rx - E->screencols + 1;
}
}
void editorDrawStatusBar(struct editorConfig *E, struct abuf *ab) {
int len, render_len;
char status[80], render_status[80];
abAppend(ab, "\x1b[7m", 4); // inverting colors
len = snprintf(status, sizeof(status), "%.20s - %d lines",
E->filename ? E->filename : "[No Name]", E->numrows);
render_len = snprintf(render_status, sizeof(render_status), "%d/%d",
E->cursor_y + 1, E->numrows);
if (len > E->screencols) {
len = E->screencols;
}
abAppend(ab, status, len);
while (len < E->screencols) {
if (E->screencols - len == render_len) {
abAppend(ab, render_status, render_len);
break;
} else {
abAppend(ab, " ", 1);
++len;
}
}
abAppend(ab, "\x1b[m", 3); // normal text mode
abAppend(ab, "\r\n", 2);
}
void editorDrawMessageBar(struct editorConfig *E, struct abuf *ab) {
int msg_len = strlen(E->status_msg);
abAppend(ab, ERASE_END_LINE, 3);
if (msg_len > E->screencols) {
msg_len = E->screencols;
}
if (msg_len && time(NULL) - E->status_msg_time < 5) {
abAppend(ab, E->status_msg, msg_len);
}
}
void editorRefreshScreen(struct editorConfig *E) {
editorScroll(E);
struct abuf ab = ABUF_INIT;
char buf[32];
@@ -49,8 +112,11 @@ void editorRefreshScreen(struct editorConfig *E) {
abAppend(&ab, CURSOR_TOP_LEFT, 3);
editorDrawRows(E, &ab);
editorDrawStatusBar(E, &ab);
editorDrawMessageBar(E, &ab);
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E->cursor_y + 1, E->cursor_x + 1);
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E->cursor_y - E->row_offset) + 1,
(E->rx - E->col_offset) + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, SHOW_CURSOR, 6);
@@ -58,3 +124,11 @@ void editorRefreshScreen(struct editorConfig *E) {
write(STDOUT_FILENO, ab.b, ab.len);
abFree(&ab);
}
void editorSetStatusMessage(struct editorConfig *E, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(E->status_msg, sizeof(E->status_msg), fmt, ap);
va_end(ap);
E->status_msg_time = time(NULL);
}