Draw scrollback by rows rather than full textbox

This commit is contained in:
Joseph Ferano 2022-09-14 18:27:32 +07:00
parent 9a929773c7
commit 58d77943f6

39
bt.c
View File

@ -78,18 +78,9 @@ int read_pty(scrollback *sb) {
if (readbuf[nread - 1] == '\n') { if (readbuf[nread - 1] == '\n') {
nread--; nread--;
} }
int nreturns = 0; readbuf[nread] = '\0';
for (int i = 0; i < nread; i++) {
if (readbuf[i] == '\r') {
nreturns++;
for (int j = i; j < nread; j++) {
readbuf[j] = readbuf[j + 1];
}
}
}
readbuf[nread - nreturns] = '\0';
strcat(sb->buf, readbuf); strcat(sb->buf, readbuf);
return nread - nreturns; return nread;
} }
} }
@ -103,7 +94,7 @@ int main(void) {
InitWindow(screenWidth, screenHeight, "basic term"); InitWindow(screenWidth, screenHeight, "basic term");
SetTargetFPS(120); SetTargetFPS(60);
Font *fontDefault = load_font(); Font *fontDefault = load_font();
@ -126,7 +117,7 @@ int main(void) {
bool new_read = false; bool new_read = false;
bool new_char = false; bool new_char = false;
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
float scroll_speed = 15.5f; float scroll_speed = 35.5f;
sb.ypos += GetMouseWheelMoveV().y * scroll_speed; sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y; sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
@ -185,8 +176,26 @@ int main(void) {
sb.length += nread; sb.length += nread;
new_read = true; new_read = true;
} }
ClearBackground(RAYWHITE); ClearBackground(BLACK);
DrawTextEx(*fontDefault, sb.buf, (Vector2){ 0, sb.ypos }, fontsize, 1, BLACK); int col_max = 200;
char row_buf[col_max];
int nrow = 0;
int ncol = 0;
int row_height = 18;
for (int c = 0; c <= sb.length; c++) {
if (sb.buf[c] == '\r') {
continue;
} else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) {
row_buf[ncol] = '\0';
Vector2 pos = { 0, nrow * row_height + sb.ypos };
DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, RAYWHITE);
nrow++;
ncol = 0;
} else {
row_buf[ncol] = sb.buf[c];
ncol++;
}
}
} }
EndDrawing(); EndDrawing();
} }