From c0ca4fdd70f0dd53dd51fb4676850f35d006d3e4 Mon Sep 17 00:00:00 2001 From: Ben Siraphob Date: Wed, 28 May 2025 13:29:15 -0700 Subject: [PATCH] block on terminal not wide enough --- gate.c.template | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/gate.c.template b/gate.c.template index 33b8895..8b4dd58 100644 --- a/gate.c.template +++ b/gate.c.template @@ -1,6 +1,9 @@ #include #include #include +#include +#include +#include typedef uint64_t cell; @@ -30,6 +33,30 @@ void fatal(const char* message) { exit(1); } +// Get current terminal column count (0 on failure) +static size_t get_terminal_width(void) { + struct winsize ws; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) return 0; + return (size_t)ws.ws_col; +} + +// Block until the terminal is at least required columns wide +static void wait_for_terminal_width(size_t required) { + // delay for 100 ms + const struct timespec delay = {0, 100 * 1000 * 1000}; + while (1) { + size_t cols = get_terminal_width(); + if (cols >= required) return; + fprintf(stderr, + "\033[2J\033[H" + "Terminal width is %zu cols; need at least %zu. " + "Resize the window to continue…\n", + cols, required); + fflush(stderr); + nanosleep(&delay, NULL); + } +} + board_t board_new(size_t width, size_t height) { if (width % 64 != 0) { fatal("board width must be multiple of 64"); } @@ -167,6 +194,7 @@ int main() { for (size_t count = 0; count < 100000; count++) { // vvv comment out for benchmarking + wait_for_terminal_width(board.width * 2); printf("\033[H"); board_debug(&board); printf("Step: %zu\n", count);