Console below the frame: prompt and output echo, records inside
Two things were wrong with the first attempt:
* a tape that starts with INP never drew its opening frame, because the
prompt ran before the hook and there was no iterator to render with. The
input callback now receives the iterator, so it can paint the frame and
then ask -- the initial memory state is visible again.
* putting the prompt inside the frame left the cursor at the end of the
frame, which reads badly. The records stay inside:
| in (1) 13 |
| out (1) 13 |
and the prompt moved below the window, like a shell prompt under it:
output> 13
input> 13
output> grows with every OUT; input> is the prompt while INP waits and
becomes the value just given afterwards. The cursor sits right after
'input> ', outside the frame, so typing does not fight the drawing.
Tapes that never read input get none of the lines outside the frame, and the
existing tapes look exactly as before.
Checked: echo.rot8 first frame now shows step 0 / pc 0000 / op INP with the
memory grid, then input> below; --input 42 still prints 42; --dump and the
piped live run are unchanged.
This commit is contained in:
1 parent
2aa3f83ea4
commit
5a25c328d0
5 files changed
+88
-24
No files matched your search
@@ -110,23 +110,31 @@ 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> ` 会画在画面里、`out` 框的上一行(光标留在提示末尾,直接在
|
||||
那里敲数回车);喂进去的值之后记成 `in (n) ...`,和 `out` 框上下并排:
|
||||
live 模式下画面分两层,框里只**记录**已经产生的输入输出,提示在框**外面**:
|
||||
|
||||
```
|
||||
| in (1) 13 |
|
||||
| out (1) 13 |
|
||||
+--------------------------------------------------------+
|
||||
| in (1) 13 | <- 记录
|
||||
| out (1) 13 | <- 记录
|
||||
+--------------------------------------------------------+
|
||||
output> 13 <- 框外,像终端回显
|
||||
input> 13
|
||||
```
|
||||
|
||||
精简画面没有 `out` 框,但只要有输入也会补一行 `in`. 不读输入的 tape 不会多出这一行,所以
|
||||
原有画面完全不变.
|
||||
队列空了时最后一行变成 `input> `,光标就停在那里 —— 敲的数字落在窗口外面,不会和画面挤在
|
||||
一起;喂进去之后变成 `input> <刚才那个值>`. `output>` 那一行跟着 `OUT` 实时长.
|
||||
|
||||
一开头就是 `INP` 的 tape 也没问题:提示会先把第一帧画出来再问,所以内存的初始状态是看得到的
|
||||
(为此输入回调拿到了迭代器).
|
||||
|
||||
精简画面没有 `in`/`out` 框,但只要有输入仍会补一行. 不读输入的 tape 不会多出框外的行,原有
|
||||
画面完全不变.
|
||||
|
||||
stdin 已经关掉(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是
|
||||
拿来试这个的最小例子 —— 它只有两条指令,会在第一帧画出来之前就执行 `INP`,那时没有画面可以
|
||||
挂提示,于是退回在当前行打一个 `input> `;更长的 tape 才会把提示画进框里.
|
||||
拿来试这个的最小例子.
|
||||
|
||||
## 构建与测试
|
||||
|
||||
|
||||
@@ -13,7 +13,8 @@ bool rot8_io_quiet(void);
|
||||
/* INP takes its value from the queue pushed here first, then from the hook,
|
||||
* then from stdin. The queue is for scripts and tests (--input 1 2 3); the
|
||||
* hook is for the live view, which has to stop and ask the person watching. */
|
||||
typedef bool (*rot8_input_fn)(void *ctx, rot8_data *value);
|
||||
typedef bool (*rot8_input_fn)(rot8_iter *it, void *ctx,
|
||||
rot8_data *value);
|
||||
|
||||
void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx);
|
||||
void rot8_io_push_input(rot8_data value);
|
||||
|
||||
@@ -41,7 +41,7 @@ bool rot8_viz_terminal(const rot8_viz *viz);
|
||||
|
||||
/* Asks for one value below the view: the INP hook while a tape is running live.
|
||||
* Pass the rot8_viz as ctx. Returns false when stdin is exhausted. */
|
||||
bool rot8_viz_ask(void *ctx, rot8_data *value);
|
||||
bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value);
|
||||
|
||||
/* rot8_hook that drives the view; pass the rot8_viz as ctx. */
|
||||
void rot8_viz_hook(rot8_iter *iter, void *ctx);
|
||||
|
||||
+2
-1
@@ -53,7 +53,8 @@ void rot8_inp_io(rot8_iter *it) {
|
||||
if (input_head != input_tail) {
|
||||
value = input_queue[input_head];
|
||||
input_head = (input_head + 1) % INPUT_QUEUE_MAX;
|
||||
} else if (input_hook != NULL && input_hook(input_hook_ctx, &value)) {
|
||||
} else if (input_hook != NULL &&
|
||||
input_hook(it, input_hook_ctx, &value)) {
|
||||
/* the hook filled value in */
|
||||
} else {
|
||||
int n;
|
||||
|
||||
+66
-12
@@ -445,6 +445,61 @@ static void viz_inputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
line_close(&ln);
|
||||
}
|
||||
|
||||
/* Below the frame, like the scrollback of a terminal: the output the tape has
|
||||
* produced, then either the prompt it is waiting on or the value it was just
|
||||
* given. The cursor is left right after "input> " so the number is typed
|
||||
* outside the window instead of in the middle of it. */
|
||||
static void viz_console(rot8_viz *viz, FILE *out, bool color) {
|
||||
char buf[160];
|
||||
size_t first, i;
|
||||
int n;
|
||||
|
||||
if (!viz->opts.live) {
|
||||
return;
|
||||
}
|
||||
if (viz->output_count != 0) {
|
||||
first = viz->output_count > OUTPUT_SHOWN
|
||||
? viz->output_count - OUTPUT_SHOWN
|
||||
: 0;
|
||||
n = snprintf(buf, sizeof buf, "output>");
|
||||
if (first != 0) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " ..");
|
||||
}
|
||||
for (i = first; i < viz->output_count; i++) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
|
||||
(unsigned)viz->outputs[i]);
|
||||
}
|
||||
line_start(viz, out);
|
||||
if (color) {
|
||||
fputs(ANSI_OUT, out);
|
||||
}
|
||||
fputs(buf, out);
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
}
|
||||
if (viz->input_pending) {
|
||||
line_start(viz, out);
|
||||
if (color) {
|
||||
fputs(ANSI_IN, out);
|
||||
}
|
||||
fputs("input> ", out);
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
} else if (viz->input_count != 0) {
|
||||
line_start(viz, out);
|
||||
if (color) {
|
||||
fputs(ANSI_IN, out);
|
||||
}
|
||||
fprintf(out, "input> %u",
|
||||
(unsigned)viz->inputs[viz->input_count - 1]);
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
|
||||
bool color) {
|
||||
viz->lines = 0;
|
||||
@@ -464,12 +519,13 @@ static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
|
||||
viz_inputs(viz, out, color);
|
||||
viz_outputs(viz, out, color);
|
||||
viz_rule(viz, out, color);
|
||||
} else if (viz->input_pending || viz->input_count != 0) {
|
||||
} else if (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);
|
||||
}
|
||||
viz_console(viz, out, color);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
@@ -648,7 +704,7 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
|
||||
/* Called from rot8_inp_io while a tape runs under the live view: the person
|
||||
* watching gets a prompt under the frame instead of the program swallowing a
|
||||
* number off the terminal (or blocking on a pipe). */
|
||||
bool rot8_viz_ask(void *ctx, rot8_data *value) {
|
||||
bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) {
|
||||
rot8_viz *viz = (rot8_viz *)ctx;
|
||||
char line[64];
|
||||
char *end = NULL;
|
||||
@@ -657,21 +713,19 @@ bool rot8_viz_ask(void *ctx, rot8_data *value) {
|
||||
if (viz == NULL || value == NULL) {
|
||||
return (false);
|
||||
}
|
||||
/* 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 */
|
||||
/* the prompt lives below the frame, like a shell prompt under the window;
|
||||
* the frame itself keeps drawing itself with the records */
|
||||
viz->input_pending = true;
|
||||
if (viz->last_iter != NULL) {
|
||||
viz_redraw(viz, viz->last_iter, false);
|
||||
if (viz->opts.live) {
|
||||
viz_redraw(viz, 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);
|
||||
if (viz->opts.live) {
|
||||
viz_redraw(viz, iter, false);
|
||||
}
|
||||
fputc('\n', viz->out);
|
||||
fflush(viz->out);
|
||||
@@ -684,8 +738,8 @@ bool rot8_viz_ask(void *ctx, rot8_data *value) {
|
||||
*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);
|
||||
if (viz->opts.live) {
|
||||
viz_redraw(viz, iter, false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user