Commit: 8dfed6621fa3f75adc05275031b3f3c40d7fcab3
Parent: 62abcc3db668320844d3128d653fb5a7bf9c10a2
Author: Randy Palamar
Date: Sun, 11 Aug 2024 11:20:48 -0600
add some debug functions for dumping raw lines/fb
Diffstat:
A | debug.c | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | util.h | | | 4 | ++++ |
M | vtgl.c | | | 11 | +++++++++++ |
3 files changed, 114 insertions(+), 0 deletions(-)
diff --git a/debug.c b/debug.c
@@ -0,0 +1,99 @@
+#include "util.h"
+
+static Cursor
+simulate_line(Term *t, Line *line)
+{
+ Cursor saved_cursor = t->cursor;
+ s8 l = line_to_s8(line);
+
+ t->cursor.state = line->cursor_state;
+
+ while (l.len) {
+ u32 cp;
+ if (line->has_unicode) cp = get_utf8(&l);
+ else cp = get_ascii(&l);
+
+ u32 advance = g_tabstop - (t->cursor.col % g_tabstop);
+
+ switch (cp) {
+ case 0x1B: check_if_escape_moves_cursor(t, &l); break;
+ case '\r': t->cursor.col = 0; break;
+ case '\n': cursor_move_to(t, t->cursor.row + 1, t->cursor.col); break;
+ case '\t': cursor_move_to(t, t->cursor.row, t->cursor.col + advance); break;
+ case '\b': cursor_move_to(t, t->cursor.row, t->cursor.col - 1); break;
+ default: cursor_move_to(t, t->cursor.row, t->cursor.col + 1); break;
+ }
+ }
+
+ Cursor result = t->cursor;
+ t->cursor = saved_cursor;
+
+ return result;
+}
+
+static void
+fput_cursor_info(FILE *f, CellStyle c)
+{
+ fprintf(f, "\tFG: 0x%08x\n", c.fg.rgba);
+ fprintf(f, "\tBG: 0x%08x\n", c.bg.rgba);
+ fprintf(f, "\tAttr: 0x%08x\n", c.attr);
+}
+
+static void
+fput_line_info(FILE *f, Term *t, Line *l)
+{
+ fprintf(f, "Line Info:\n");
+ fprintf(f, "\tLength: %ld\n", line_length(l));
+ fprintf(f, "\tHas Unicode: %d\n", l->has_unicode);
+ fput_cursor_info(f, l->cursor_state);
+ Cursor end = simulate_line(t, l);
+ fprintf(f, "After Line Cursor State:\n");
+ fput_cursor_info(f, end.state);
+}
+
+static void
+dump_last_line_to_file(Term *t)
+{
+ char *fname = "last_line.bin";
+ FILE *f = fopen(fname, "w");
+ if (!f) return;
+
+ printf("dumping line to %s\n", fname);
+
+ fputs("Line Idx -1:\n", f);
+ Line *line = t->log_lines.buf + get_line_idx(&t->log_lines, -1);
+ s8 l = line_to_s8(line);
+ fput_line_info(f, t, line);
+ fwrite(l.data, 1, l.len, f);
+
+ fputs("\nLine Idx 0:\n", f);
+ line = t->log_lines.buf + get_line_idx(&t->log_lines, 0);
+ l = line_to_s8(line);
+ fput_line_info(f, t, line);
+ fwrite(l.data, 1, l.len, f);
+
+ fclose(f);
+}
+
+static void
+fput_cell(FILE *f, Cell *c)
+{
+ s8 s = utf8_encode(c->cp);
+ fwrite(s.data, 1, s.len, f);
+}
+
+static void
+dump_fb_to_file(Term *t)
+{
+ char *fname = "fb.bin";
+ FILE *f = fopen(fname, "w");
+ if (!f) return;
+ printf("dumping fb to %s\n", fname);
+ for (u32 r = 0; r < t->size.h; r++) {
+ for (u32 c = 0; c < t->size.w; c++)
+ fput_cell(f, &t->fb.rows[r][c]);
+ fputc('\n', f);
+ }
+ fclose(f);
+}
+
diff --git a/util.h b/util.h
@@ -277,4 +277,8 @@ typedef struct {
#include "font.c"
#include "terminal.c"
+#ifdef _DEBUG
+#include "debug.c"
+#endif
+
#endif /* _UTIL_H_ */
diff --git a/vtgl.c b/vtgl.c
@@ -299,6 +299,17 @@ key_callback(GLFWwindow *win, i32 key, i32 sc, i32 act, i32 mods)
{
Term *t = glfwGetWindowUserPointer(win);
+ #ifdef _DEBUG
+ if (key == GLFW_KEY_F1 && act == GLFW_PRESS) {
+ dump_last_line_to_file(t);
+ return;
+ }
+ if (key == GLFW_KEY_F2 && act == GLFW_PRESS) {
+ dump_fb_to_file(t);
+ return;
+ }
+ #endif
+
if (mods & GLFW_MOD_CONTROL && key < 0x80) {
if (act == GLFW_RELEASE)
return;