Remove infinite loop and clean up after yourself

This commit is contained in:
Joseph Ferano 2023-11-11 15:04:46 +07:00
parent 983440079a
commit 107ac6b2db
2 changed files with 107 additions and 108 deletions

View File

@ -4,7 +4,7 @@ CFLAGS=-g -Wall -Wextra -pedantic -O0
LDLIBS=-lSDL2 -lSDL2_ttf LDLIBS=-lSDL2 -lSDL2_ttf
RM=rm -vf RM=rm -vf
bt: clean build: clean
$(CC) $(CFLAGS) $(LDLIBS) $(P).c -o $(P) $(CC) $(CFLAGS) $(LDLIBS) $(P).c -o $(P)
clean: clean:

213
bt.c
View File

@ -14,6 +14,18 @@
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_keycode.h> #include <SDL2/SDL_keycode.h>
const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
const SDL_Color WHITE = {255, 255, 255, 255};
const SDL_Color BLACK = {0, 0, 0, 255};
const SDL_Color RED = {255, 0, 0, 255};
const SDL_Color GREEN = {0, 255, 0, 255};
const SDL_Color BLUE = {0, 0, 255, 255};
const SDL_Color YELLOW = {255, 255, 0, 255};
const SDL_Color MAGENTA = {255, 0, 255, 255};
const SDL_Color SKYBLUE = {0, 255, 255, 255};
const SDL_Color RAYWHITE = {200, 200, 200, 255};
typedef struct scrollback { typedef struct scrollback {
float height; float height;
float ypos; float ypos;
@ -45,18 +57,6 @@ void spawn(file_descriptors *fds) {
} }
} }
const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
const SDL_Color WHITE = {255, 255, 255, 255};
const SDL_Color BLACK = {0, 0, 0, 255};
const SDL_Color RED = {255, 0, 0, 255};
const SDL_Color GREEN = {0, 255, 0, 255};
const SDL_Color BLUE = {0, 0, 255, 255};
const SDL_Color YELLOW = {255, 255, 0, 255};
const SDL_Color MAGENTA = {255, 0, 255, 255};
const SDL_Color SKYBLUE = {0, 255, 255, 255};
const SDL_Color RAYWHITE = {200, 200, 200, 255};
int read_pty(file_descriptors *fds, scrollback *sb) { int read_pty(file_descriptors *fds, scrollback *sb) {
ssize_t nread; ssize_t nread;
char readbuf[256]; char readbuf[256];
@ -150,18 +150,16 @@ int main(void) {
char key = e.key.keysym.sym; char key = e.key.keysym.sym;
// TODO: Translate this all to SDL2 // TODO: Translate this all to SDL2
// int key = GetCharPressed(); // int key = GetCharPressed();
while (key > 0) { // TODO: Don't remember why I was doing this
if ((key >= 32) && (key <= 125)) { // key = GetCharPressed();
buf[buf_len] = (char)key; // new_char = true;
buf[buf_len + 1] = '\0'; if ((key >= 32) && (key <= 125)) {
buf_len++; buf[buf_len] = (char)key;
sb.buf[sb.length] = (char)key; buf[buf_len + 1] = '\0';
sb.buf[sb.length + 1] = '\0'; buf_len++;
sb.length++; sb.buf[sb.length] = (char)key;
} sb.buf[sb.length + 1] = '\0';
// TODO: Don't remember why I was doing this sb.length++;
// key = GetCharPressed();
// new_char = true;
} }
if (key == SDLK_RETURN) { if (key == SDLK_RETURN) {
@ -213,99 +211,100 @@ int main(void) {
// Drawing // Drawing
{ int nread = read_pty(&fds, &sb);
int nread = read_pty(&fds, &sb); if (nread > 0) {
if (nread > 0) { sb.length += nread;
sb.length += nread; new_read = true;
new_read = true; }
} SDL_RenderClear(renderer);
SDL_RenderClear(renderer); SDL_Color current_color = WHITE;
SDL_Color current_color = WHITE; int col_max = 200;
int col_max = 200; char row_buf[col_max];
char row_buf[col_max]; int nrow = 0;
int nrow = 0; int ncol = 0;
int ncol = 0; int row_height = 18;
int row_height = 18; float row_posx = 0;
float row_posx = 0; for (int c = 0; c <= sb.length; c++) {
for (int c = 0; c <= sb.length; c++) { if (sb.buf[c] == '\r') {
if (sb.buf[c] == '\r') { continue;
continue; } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) {
} else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { row_buf[ncol] = '\0';
row_buf[ncol] = '\0'; // TODO: Render new line or something?
// TODO: Render new line or something? // Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); nrow++;
nrow++; ncol = 0;
ncol = 0; row_posx = 0;
row_posx = 0; } else if (sb.buf[c] == '\x1b') {
} else if (sb.buf[c] == '\x1b') { int c2 = c + 1;
int c2 = c + 1; // Control Sequence Introducer
// Control Sequence Introducer int csi_args[16];
int csi_args[16]; char csi_code;
char csi_code; int nargs = 0;
int nargs = 0; if (sb.buf[c2] == '[') {
if (sb.buf[c2] == '[') { char *esc_buf = sb.buf + c2 + 1;
char *esc_buf = sb.buf + c2 + 1; while (true) {
while (true) { char *substr;
char *substr; long num = strtol(esc_buf, &substr, 10);
long num = strtol(esc_buf, &substr, 10); if (esc_buf != substr) {
if (esc_buf != substr) { csi_args[nargs++] = num;
csi_args[nargs++] = num; esc_buf = substr;
esc_buf = substr; if (substr[0] == ';') {
if (substr[0] == ';') { esc_buf++;
esc_buf++;
}
continue;
} }
csi_code = substr[0]; continue;
SDL_Color new_color = WHITE;
switch (csi_code) {
case 'm':
for (int i = 0; i < nargs; i++) {
if (csi_args[i] == 31) {
new_color = RED;
} else if (csi_args[i] == 32) {
new_color = GREEN;
} else if (csi_args[i] == 33) {
new_color = YELLOW;
} else if (csi_args[i] == 34) {
new_color = BLUE;
} else if (csi_args[i] == 35) {
new_color = MAGENTA;
} else if (csi_args[i] == 36) {
new_color = SKYBLUE;
} else if (csi_args[i] == 0) {
new_color = RAYWHITE;
}
}
row_buf[ncol] = '\0';
// TODO: This looks like the actual place where we
// draw the text at a line
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
current_color = new_color;
ncol = 0;
c += (substr) - (sb.buf + c);
// TODO: Get the text height
// int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
// row_posx += width + 0.85;
break;
}
break;
} }
csi_code = substr[0];
SDL_Color new_color = WHITE;
switch (csi_code) {
case 'm':
for (int i = 0; i < nargs; i++) {
if (csi_args[i] == 31) {
new_color = RED;
} else if (csi_args[i] == 32) {
new_color = GREEN;
} else if (csi_args[i] == 33) {
new_color = YELLOW;
} else if (csi_args[i] == 34) {
new_color = BLUE;
} else if (csi_args[i] == 35) {
new_color = MAGENTA;
} else if (csi_args[i] == 36) {
new_color = SKYBLUE;
} else if (csi_args[i] == 0) {
new_color = RAYWHITE;
}
}
row_buf[ncol] = '\0';
// TODO: This looks like the actual place where we
// draw the text at a line
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
current_color = new_color;
ncol = 0;
c += (substr) - (sb.buf + c);
// TODO: Get the text height
// int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
// row_posx += width + 0.85;
break;
}
break;
} }
} else {
row_buf[ncol++] = sb.buf[c];
} }
} else {
row_buf[ncol++] = sb.buf[c];
} }
} }
SDL_RenderPresent(renderer);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surfaceMessage);
} }
// TODO: Figure out when to free this thing free(sb.buf);
// SDL_FreeSurface(texture);
TTF_CloseFont(font); TTF_CloseFont(font);
TTF_Quit(); TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
} }