Make the live view work behind a pipe, and fit small terminals

A terminal can be a pipe: web terminals and IDE panels hand the program a
pipe for stdout, so isatty() says no, --live quietly fell back to printing
one frame after another and -f was ignored.  Add -t to say "there is a
terminal at the other end anyway" (and --no-tty for the opposite), and say
so on stderr when the fallback happens, so the flag can be found.

Frames no longer end with a newline, so a frame of N lines needs N rows
instead of N+1 and a 24 row terminal shows the whole compact frame.  The
size heuristics follow from that: shorter than 30 rows -> compact, size
unknown (behind a pipe) -> compact, LINES is honoured when the console
cannot be asked, and --full forces the tall frame.

Colour is now only switched off for a real console that cannot do VT, not
for every stream that is not a console: behind a pipe the far end may be
perfectly capable.
This commit is contained in:
starcells committed 2026-11-05 21:48:58 +08:00
1 parent b654bd644d
commit bda173b078
4 files changed
+116 -46

No files matched your search

+7 -2
View File
@@ -89,13 +89,18 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
| `-d`, `--dump` | 结束时打印一张静态快照 |
| `-e`, `--every <n>` | 每 n 条指令画一帧;`0`(默认)表示整段动画大约 300 帧 |
| `-f`, `--fps <n>` | 刷新率上限,默认 30;`0` 表示不限速 |
| `-k`, `--compact` | 精简画面:只画纸带,不画当前 Cell 与输出 |
| `-t`, `--tty` / `--no-tty` | 强制当成/不当成终端(见下) |
| `-k`, `--compact` / `--full` | 强制精简画面/完整画面 |
| `-c`, `--color` / `--no-color` | 强制开/关 ANSI 颜色(默认跟随终端) |
| `-h`, `--help` | 帮助 |
不加 `--live` 与 `--dump` 时行为与以前完全一致:直接跑完,只留下 `OUT` 的输出.\
`--live` 期间 `OUT` 会被画面接管,程序结束后再原样补打出来,所以重定向到文件依然拿到完整的输出.\
输出不是终端(被管道/文件接管)时,`--live` 会退化成逐帧打印,可以直接 `less` 着看.
输出不是终端(被管道/文件接管)时,`--live` 会退化成逐帧打印,可以直接 `less` 着看;程序也会在 stderr 上提一句.
画面高度是自动挑的:终端不足 30 行用 24 行的精简版,测不出高度时也先按 24 行来(猜你屏幕不大),`--full`
可以强制完整版. 用 `--tty` 之前那套原地重绘才生效,所以**如果你的终端其实是通过管道接进来的**
(网页版终端、某些 IDE 的面板,程序 `_isatty` 看不出来),画面会变成一帧帧往下滚 —— 加个 `-t` 就好了.
## 构建与测试
+5
View File
@@ -26,6 +26,7 @@ typedef struct rot8_viz_opts {
unsigned int every; /* instructions between frames, 0 = automatic */
int color; /* 1 = ANSI colour, 0 = plain, -1 = guess from out */
int compact; /* 1 = short frame, 0 = full, -1 = fit terminal */
int tty; /* 1 = speak terminal, 0 = plain frames, -1 = guess */
bool live; /* redraw while the program runs */
bool final_dump; /* static snapshot once the program terminates */
bool replay_output; /* mute OUT while live, print its values at the end */
@@ -34,6 +35,10 @@ typedef struct rot8_viz_opts {
rot8_viz *rot8_viz_new(const rot8_viz_opts *opts);
void rot8_viz_free(rot8_viz *viz);
/* True when the view talks to a terminal (redraws in place, paces frames).
* Worth telling the user about when it is false but live was asked for. */
bool rot8_viz_terminal(const rot8_viz *viz);
/* rot8_hook that drives the view; pass the rot8_viz as ctx. */
void rot8_viz_hook(rot8_iter *iter, void *ctx);
+87 -44
View File
@@ -55,6 +55,7 @@ struct rot8_viz {
bool auto_every;
size_t every;
size_t frames;
size_t lines;
unsigned long wait_ms;
rot8_data *outputs;
size_t output_count;
@@ -73,24 +74,36 @@ static bool viz_is_tty(FILE *stream) {
#endif
}
/* Lines the terminal has, or 0 when it cannot be found out. */
static int viz_term_height(FILE *stream) {
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO info;
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(stream));
if (handle == INVALID_HANDLE_VALUE) {
return (0);
if (handle != INVALID_HANDLE_VALUE &&
GetConsoleScreenBufferInfo(handle, &info)) {
return ((int)(info.srWindow.Bottom - info.srWindow.Top + 1));
}
if (!GetConsoleScreenBufferInfo(handle, &info)) {
return (0);
}
return ((int)(info.srWindow.Bottom - info.srWindow.Top + 1));
#else
struct winsize ws;
if (ioctl(fileno(stream), TIOCGWINSZ, &ws) != 0) {
return (0);
if (ioctl(fileno(stream), TIOCGWINSZ, &ws) == 0 && ws.ws_row > 0) {
return ((int)ws.ws_row);
}
return ((int)ws.ws_row);
#endif
/* Behind a pipe there is no console to ask, but plenty of terminals
* export LINES for exactly this reason. */
{
const char *lines = getenv("LINES");
char *end = NULL;
long value;
if (lines == NULL || *lines == '\0') {
return (0);
}
value = strtol(lines, &end, 10);
if (end == NULL || *end != '\0' || value <= 0 || value > 1000) {
return (0);
}
return ((int)value);
}
}
/* Windows consoles need to be told that we speak ANSI. Returns false when
@@ -138,7 +151,18 @@ typedef struct {
size_t width;
} viz_line;
static void line_open(viz_line *ln, FILE *out, bool color) {
/* Frames are written with the newline *in front* of every line but the
* first, so that a frame of N lines needs exactly N rows: a terminal that
* is exactly as tall as the frame does not scroll it away. */
static void line_start(rot8_viz *viz, FILE *out) {
if (viz->lines > 0) {
fputc('\n', out);
}
viz->lines++;
}
static void line_open(viz_line *ln, rot8_viz *viz, FILE *out, bool color) {
line_start(viz, out);
ln->out = out;
ln->color = color;
ln->width = 0;
@@ -172,11 +196,12 @@ static void line_close(viz_line *ln) {
/* width already counts the single space that follows the opening bar, so
* it is also the number of characters inside the frame. */
line_fill(ln, VIZ_INNER);
fputs("|\n", ln->out);
fputc('|', ln->out);
}
static void viz_rule(FILE *out, bool color) {
static void viz_rule(rot8_viz *viz, FILE *out, bool color) {
int i;
line_start(viz, out);
if (color) {
fputs(ANSI_DIM, out);
}
@@ -184,7 +209,7 @@ static void viz_rule(FILE *out, bool color) {
for (i = 0; i < VIZ_INNER; i++) {
fputc('-', out);
}
fputs("+\n", out);
fputc('+', out);
if (color) {
fputs(ANSI_RESET, out);
}
@@ -200,7 +225,7 @@ static void viz_caption(rot8_viz *viz, FILE *out, bool color) {
size_t used, room, length;
viz_line ln;
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_style(&ln, label, ANSI_TITLE);
used = ln.width;
room = (VIZ_INNER - 1) - used;
@@ -219,7 +244,7 @@ static void viz_caption(rot8_viz *viz, FILE *out, bool color) {
line_close(&ln);
}
static void viz_status(rot8_iter *iter, FILE *out, bool color) {
static void viz_status(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
char op[8];
char buf[96];
ptrdiff_t pc = rot8_ins_offset(iter);
@@ -234,7 +259,7 @@ static void viz_status(rot8_iter *iter, FILE *out, bool color) {
snprintf(buf, sizeof buf, "step %6lu pc %04lX/%04lX op %s",
(unsigned long)rot8_step_count(iter), (unsigned long)pc,
(unsigned long)length, op);
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_put(&ln, buf);
line_close(&ln);
}
@@ -245,12 +270,12 @@ static bool viz_ptr_on_tape(rot8_iter *iter, ptrdiff_t *offset) {
return (ptr >= 0 && ptr < (ptrdiff_t)MEM_SIZE);
}
static void viz_focus(rot8_iter *iter, FILE *out, bool color) {
static void viz_focus(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
char buf[96];
ptrdiff_t ptr;
viz_line ln;
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
if (viz_ptr_on_tape(iter, &ptr)) {
unsigned int value = *rot8_iter_cell(iter);
char shown = (value >= 0x20 && value < 0x7f) ? (char)value : '.';
@@ -264,7 +289,7 @@ static void viz_focus(rot8_iter *iter, FILE *out, bool color) {
line_close(&ln);
}
static void viz_bits(rot8_iter *iter, FILE *out, bool color) {
static void viz_bits(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
char field[8];
ptrdiff_t ptr;
unsigned int value = 0;
@@ -275,7 +300,7 @@ static void viz_bits(rot8_iter *iter, FILE *out, bool color) {
value = *rot8_iter_cell(iter);
}
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_put(&ln, " ");
for (bit = 7; bit >= 0; bit--) {
snprintf(field, sizeof field, " b%d", bit);
@@ -283,7 +308,7 @@ static void viz_bits(rot8_iter *iter, FILE *out, bool color) {
}
line_close(&ln);
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_put(&ln, " ");
for (bit = 7; bit >= 0; bit--) {
bool set = ((value >> bit) & 1u) != 0;
@@ -293,7 +318,7 @@ static void viz_bits(rot8_iter *iter, FILE *out, bool color) {
line_close(&ln);
}
static void viz_grid(rot8_iter *iter, FILE *out, bool color) {
static void viz_grid(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
rot8_data *mem = rot8_iter_mem(iter);
ptrdiff_t ptr;
int row, col;
@@ -301,7 +326,7 @@ static void viz_grid(rot8_iter *iter, FILE *out, bool color) {
(void)viz_ptr_on_tape(iter, &ptr);
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_put(&ln, " | ");
for (col = 0; col < GRID_COLS; col++) {
char label[8];
@@ -313,7 +338,7 @@ static void viz_grid(rot8_iter *iter, FILE *out, bool color) {
for (row = 0; row < GRID_ROWS; row++) {
char label[16];
snprintf(label, sizeof label, " %02X |", row * GRID_COLS);
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_style(&ln, label, ANSI_DIM);
line_put(&ln, " ");
for (col = 0; col < GRID_COLS; col++) {
@@ -337,7 +362,7 @@ static void viz_grid(rot8_iter *iter, FILE *out, bool color) {
/* A caret under the tape pointer, for terminals without colour. */
if (ptr >= 0 && (long)(ptr / GRID_COLS) == row) {
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_put(&ln, " | ");
for (col = 0; col < GRID_COLS; col++) {
bool here = (size_t)ptr == (size_t)(row * GRID_COLS + col);
@@ -374,28 +399,29 @@ static void viz_outputs(rot8_viz *viz, FILE *out, bool color) {
if (n > 0 && (size_t)n > VIZ_MAX_TEXT) {
buf[VIZ_MAX_TEXT] = '\0';
}
line_open(&ln, out, color);
line_open(&ln, viz, out, color);
line_style(&ln, buf, ANSI_OUT);
line_close(&ln);
}
static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
bool color) {
viz_rule(out, color);
viz->lines = 0;
viz_rule(viz, out, color);
viz_caption(viz, out, color);
viz_rule(out, color);
viz_status(iter, out, color);
viz_rule(out, color);
viz_rule(viz, out, color);
viz_status(viz, iter, out, color);
viz_rule(viz, out, color);
if (!viz->compact) {
viz_focus(iter, out, color);
viz_bits(iter, out, color);
viz_rule(out, color);
viz_focus(viz, iter, out, color);
viz_bits(viz, iter, out, color);
viz_rule(viz, out, color);
}
viz_grid(iter, out, color);
viz_rule(out, color);
viz_grid(viz, iter, out, color);
viz_rule(viz, out, color);
if (!viz->compact) {
viz_outputs(viz, out, color);
viz_rule(out, color);
viz_rule(viz, out, color);
}
}
@@ -438,16 +464,24 @@ rot8_viz *rot8_viz_new(const rot8_viz_opts *opts) {
viz->opts.out = stdout;
}
viz->out = viz->opts.out;
viz->tty = viz_is_tty(viz->out);
viz->tty = viz->opts.tty < 0 ? viz_is_tty(viz->out) : (viz->opts.tty != 0);
viz->color = viz->opts.color < 0 ? viz->tty : (viz->opts.color != 0);
if (viz->color && viz->tty && !viz_enable_ansi(viz->out)) {
viz->color = false; /* no VT: escapes would only be noise */
if (viz->color && viz_is_tty(viz->out) && !viz_enable_ansi(viz->out)) {
/* A real console that cannot do VT: escapes would only be noise. A
* stream that is not a console has no mode to set, so leave it be --
* the far end may well be a terminal emulator behind a pipe. */
viz->color = false;
}
height = viz_term_height(viz->out);
viz->compact =
viz->opts.compact >= 0
? (viz->opts.compact != 0)
: (height > 0 && height < FRAME_LINES);
if (viz->opts.compact >= 0) {
viz->compact = (viz->opts.compact != 0);
} else if (height > 0) {
viz->compact = (height < FRAME_LINES);
} else {
/* The size is unknown, so the terminal is somewhere behind a pipe.
* Bet on a small one and use the frame that fits in 24 rows. */
viz->compact = viz->tty;
}
viz->every = viz->opts.every;
viz->auto_every = (viz->opts.every == 0);
viz->wait_ms = viz->opts.fps != 0 ? 1000UL / viz->opts.fps : 0UL;
@@ -462,11 +496,16 @@ void rot8_viz_free(rot8_viz *viz) {
free(viz);
}
bool rot8_viz_terminal(const rot8_viz *viz) {
return (viz != NULL && viz->tty);
}
void rot8_viz_dump(rot8_viz *viz, rot8_iter *iter) {
if (viz == NULL || iter == NULL) {
return;
}
viz_render(viz, iter, viz->out, viz->color);
fputc('\n', viz->out);
fflush(viz->out);
}
@@ -504,8 +543,12 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
if (!viz->tty) {
fputc('\n', viz->out);
} else {
/* Drop whatever a taller frame left behind. */
/* Drop whatever a taller frame left behind, then leave the cursor
* below the frame once it is final. */
fputs("\x1b[J", viz->out);
if (done) {
fputc('\n', viz->out);
}
}
fflush(viz->out);
viz->frames++;
+17
View File
@@ -33,7 +33,11 @@ static void usage(const char *program) {
" (0 = as fast as the terminal can take it)\n"
" -c, --color force ANSI colour on\n"
" --no-color force ANSI colour off\n"
" -t, --tty assume the far end is a terminal even when stdout\n"
" is a pipe (use it when the view does not animate)\n"
" --no-tty never draw in place, just print frames\n"
" -k, --compact short frame: the tape, without the cell inspector\n"
" --full always draw the full frame\n"
" -h, --help show this text\n"
"\n"
"examples:\n"
@@ -69,6 +73,7 @@ int main(int argc, char **argv) {
opts.every = 0;
opts.color = -1;
opts.compact = -1;
opts.tty = -1;
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
@@ -85,6 +90,12 @@ int main(int argc, char **argv) {
opts.color = 0;
} else if (strcmp(arg, "-k") == 0 || strcmp(arg, "--compact") == 0) {
opts.compact = 1;
} else if (strcmp(arg, "--full") == 0) {
opts.compact = 0;
} else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--tty") == 0) {
opts.tty = 1;
} else if (strcmp(arg, "--no-tty") == 0) {
opts.tty = 0;
} else if (strcmp(arg, "-e") == 0 || strcmp(arg, "--every") == 0) {
if (i + 1 >= argc || !parse_uint(argv[++i], &opts.every)) {
fprintf(stderr, "%s: --every needs a number\n", program);
@@ -130,6 +141,12 @@ int main(int argc, char **argv) {
fprintf(stderr, "%s: out of memory\n", program);
return (1);
}
if (opts.live && !rot8_viz_terminal(viz) && opts.tty < 0) {
fprintf(stderr,
"%s: stdout is not a terminal, printing frames instead; "
"add -t if it is one behind a pipe\n",
program);
}
if (opts.live) {
rot8_io_set_quiet(true);