Put the input prompt inside the frame, above the out box

Printing 'input> ' where the cursor happened to be made a mess of the frame, so
the prompt is now part of it:

    | in (1) 13                                              |
    | out (1) 13                                             |

While INP waits the line reads 'input> ' and the cursor is left at its end, so
the number is typed in place; afterwards the line turns into the record of what
was fed in, mirroring the out box.  A tape that never reads input gets no extra
line at all, so nothing changes for the existing tapes.

The frame also got a redraw helper, because the prompt has to repaint the frame
before asking; the live hook now goes through the same helper.

One case cannot use the frame: a tape like tapes/echo.rot8 (just ',.') reaches
INP before the first frame is drawn, so there is nothing to hang the prompt
above and it falls back to a plain 'input> ' on the current line.  Longer tapes
show it inside the frame.

Checked: --full frame shows in above out, compact frame shows the in line too,
wave/and/or/full_adder are unchanged, and the value still reaches the program.
This commit is contained in:
starcells committed 2026-11-06 20:41:29 +08:00
1 parent 6475938e6c
commit 2aa3f83ea4
2 files changed
+124 -27

No files matched your search

+15 -4
View File
@@ -110,12 +110,23 @@ stdin —— 所以不带 `--input`、也不开 `--live` 时行为与以前完
```sh
rot8_test --input 7,5 tapes/full_adder.rot8 # 脚本/测试:一次喂完
rot8_test --live tapes/echo.rot8 # 现场:画面下方问 input>
rot8_test --live tapes/echo.rot8 # 现场:画面里问 input>
```
live 模式下如果队列空了,画面底下会出现 `input> ` 提示,敲一个数回车就继续;stdin 已经关掉
(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是拿来试这个的
最小例子.
live 模式下如果队列空了,`input> ` 会画在画面里、`out` 框的上一行(光标留在提示末尾,直接在
那里敲数回车);喂进去的值之后记成 `in (n) ...`,和 `out` 框上下并排:
```
| in (1) 13 |
| out (1) 13 |
```
精简画面没有 `out` 框,但只要有输入也会补一行 `in`. 不读输入的 tape 不会多出这一行,所以
原有画面完全不变.
stdin 已经关掉(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是
拿来试这个的最小例子 —— 它只有两条指令,会在第一帧画出来之前就执行 `INP`,那时没有画面可以
挂提示,于是退回在当前行打一个 `input> `;更长的 tape 才会把提示画进框里.
## 构建与测试
+109 -23
View File
@@ -41,6 +41,7 @@
#define ANSI_BIT_ON "\x1b[1;32m"
#define ANSI_BIT_OFF "\x1b[90m"
#define ANSI_OUT "\x1b[36m"
#define ANSI_IN "\x1b[33m"
#define ANSI_CLEAR "\x1b[2J\x1b[H"
#define ANSI_HOME "\x1b[H"
@@ -60,6 +61,11 @@ struct rot8_viz {
rot8_data *outputs;
size_t output_count;
size_t output_cap;
rot8_data *inputs; /* what INP has been given so far */
size_t input_count;
size_t input_cap;
bool input_pending; /* the frame should show the input prompt */
rot8_iter *last_iter; /* so the prompt can redraw the frame */
};
/* ------------------------------------------------------------------ *
@@ -404,6 +410,41 @@ static void viz_outputs(rot8_viz *viz, FILE *out, bool color) {
line_close(&ln);
}
/* The input line sits right above the out box: while INP waits it is the
* prompt (the cursor is left at its end, so typing happens inside the frame),
* afterwards it shows what has been fed in, mirroring the out line. */
static void viz_inputs(rot8_viz *viz, FILE *out, bool color) {
char buf[160];
size_t first, i;
int n;
viz_line ln;
if (!viz->input_pending && viz->input_count == 0) {
return; /* a tape that never reads input keeps the old frame */
}
line_open(&ln, viz, out, color);
if (viz->input_pending) {
line_style(&ln, "input> ", ANSI_IN);
line_close(&ln);
return;
}
n = snprintf(buf, sizeof buf, "in (%lu)",
(unsigned long)viz->input_count);
first = viz->input_count > OUTPUT_SHOWN ? viz->input_count - OUTPUT_SHOWN : 0;
if (first != 0) {
n += snprintf(buf + n, sizeof buf - (size_t)n, " ..");
}
for (i = first; i < viz->input_count; i++) {
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
(unsigned)viz->inputs[i]);
}
if (n > 0 && (size_t)n > VIZ_MAX_TEXT) {
buf[VIZ_MAX_TEXT] = '\0';
}
line_style(&ln, buf, ANSI_IN);
line_close(&ln);
}
static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
bool color) {
viz->lines = 0;
@@ -420,8 +461,14 @@ static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
viz_grid(viz, iter, out, color);
viz_rule(viz, out, color);
if (!viz->compact) {
viz_inputs(viz, out, color);
viz_outputs(viz, out, color);
viz_rule(viz, out, color);
} else if (viz->input_pending || viz->input_count != 0) {
/* the compact frame has no out box, but a tape that reads input still
* deserves to show what it was given */
viz_rule(viz, out, color);
viz_inputs(viz, out, color);
}
}
@@ -493,6 +540,7 @@ void rot8_viz_free(rot8_viz *viz) {
return;
}
free(viz->outputs);
free(viz->inputs);
free(viz);
}
@@ -509,6 +557,39 @@ void rot8_viz_dump(rot8_viz *viz, rot8_iter *iter) {
fflush(viz->out);
}
static void viz_push_input(rot8_viz *viz, rot8_data value) {
if (viz->input_count == viz->input_cap) {
size_t cap = viz->input_cap != 0 ? viz->input_cap * 2 : 32;
rot8_data *grown = realloc(viz->inputs, cap * sizeof(*grown));
if (grown == NULL) {
return;
}
viz->inputs = grown;
viz->input_cap = cap;
}
viz->inputs[viz->input_count++] = value;
}
/* Puts one frame on the terminal, in place when we own it. */
static void viz_redraw(rot8_viz *viz, rot8_iter *iter, bool done) {
if (viz->tty) {
fputs(viz->screen ? ANSI_HOME : ANSI_CLEAR, viz->out);
viz->screen = true;
}
viz_render(viz, iter, viz->out, viz->color);
if (!viz->tty) {
fputc('\n', viz->out);
} else {
/* 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);
}
void rot8_viz_hook(rot8_iter *iter, void *ctx) {
rot8_viz *viz = ctx;
size_t length;
@@ -518,6 +599,7 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
if (viz == NULL || iter == NULL) {
return;
}
viz->last_iter = iter; /* the input prompt redraws with the same frame */
length = rot8_ins_length(iter);
if (viz->every == 0) {
@@ -535,22 +617,7 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
* tape is over, which is the only moment the run loop stops. */
done = (size_t)pc >= length;
if (viz->opts.live && (done || rot8_step_count(iter) % viz->every == 0)) {
if (viz->tty) {
fputs(viz->screen ? ANSI_HOME : ANSI_CLEAR, viz->out);
viz->screen = true;
}
viz_render(viz, iter, viz->out, viz->color);
if (!viz->tty) {
fputc('\n', viz->out);
} else {
/* 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_redraw(viz, iter, done);
viz->frames++;
if (viz->auto_every && viz->frames >= FRAMES_TARGET &&
viz->every < (1u << 20)) {
@@ -584,23 +651,42 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
bool rot8_viz_ask(void *ctx, rot8_data *value) {
rot8_viz *viz = (rot8_viz *)ctx;
char line[64];
char *end = NULL;
long parsed;
if (viz == NULL || value == NULL) {
return (false);
}
fprintf(viz->out, "\ninput> ");
fflush(viz->out);
/* the prompt is part of the frame, right above the out box, and the cursor
* is left at its end so the number is typed in place */
viz->input_pending = true;
if (viz->last_iter != NULL) {
viz_redraw(viz, viz->last_iter, false);
} else {
/* the tape asked for input before the first frame was drawn: there is
* no frame to put the prompt above yet, so just ask */
fputs("input> ", viz->out);
fflush(viz->out);
}
if (fgets(line, sizeof line, stdin) == NULL) {
viz->input_pending = false;
if (viz->last_iter != NULL) {
viz_redraw(viz, viz->last_iter, false);
}
fputc('\n', viz->out);
fflush(viz->out);
return (false);
}
char *end = NULL;
long parsed = strtol(line, &end, 0);
if (end == line) { /* nothing usable typed: feed 0 and keep going */
*value = 0;
return (true);
parsed = strtol(line, &end, 0);
if (end == line) {
parsed = 0; /* nothing usable typed: feed 0 and keep going */
}
*value = (rot8_data)parsed;
viz_push_input(viz, *value);
viz->input_pending = false;
if (viz->last_iter != NULL) {
viz_redraw(viz, viz->last_iter, false);
}
return (true);
}
size_t rot8_viz_output_count(const rot8_viz *viz) {