Console is an event log: one line per INP and per OUT
requested change: keep the in (n) / out (n) inside the frame as single line
summaries, but log every event outside the frame so the interleaving of inputs
and outputs is readable:
| in (2) 123 65 |
| out (2) 123 65 |
input> 123
output> 123
input> 65
output> 65
The log keeps the last 6 events (older ones show as '..') so a long run does not
grow the console without bound; the prompt is still a separate empty 'input> '
line at the very bottom, which keeps the cursor the last thing on screen.
Tapes that only produce output now get the output> log too, which is what
replaces the replay that used to be printed after the run.
This commit is contained in:
1 parent
5783769db3
commit
90fe5ec84a
2 files changed
+60
-43
No files matched your search
@@ -117,25 +117,27 @@ live 模式下画面分两层,框里只**记录**已经产生的输入输出,提
|
||||
|
||||
```
|
||||
+--------------------------------------------------------+
|
||||
| in (2) 123 65 | <- 记录
|
||||
| out (2) 123 65 | <- 记录
|
||||
| in (2) 123 65 | <- 框里: 单行汇总
|
||||
| out (2) 123 65 | <- 框里: 单行汇总
|
||||
+--------------------------------------------------------+
|
||||
input> 123 65 <- 框外,按发生顺序
|
||||
output> 123 65
|
||||
input> 123 <- 框外: 逐事件日志
|
||||
output> 123
|
||||
input> 65
|
||||
output> 65
|
||||
```
|
||||
|
||||
框外那两行都按发生顺序排,并且记录**全部**值(`input>` 记喂进去的,`output>` 记出来的),所以
|
||||
有了它们之后 `--live` 结束时就不再重复打印一遍原始输出.
|
||||
框里那两行是**单行汇总**(各记全部值),框外是**逐事件日志** —— 每次 `INP`/`OUT` 各占一行,
|
||||
所以输入和输出的先后顺序看得一清二楚. 因为日志已经记了输出,`--live` 结束时不再重复打印一遍
|
||||
原始输出.
|
||||
|
||||
轮到输入时,最下面会多出一行空的 `input> ` 当提示,光标停在那里 —— 敲的数字落在窗口外面,
|
||||
不会和画面挤在一起;回车之后提示消失,值进到上面的记录行里. 因为提示永远在最下面,不会出现
|
||||
"后来的输出跑到提示上面"这种次序错乱.
|
||||
不会和画面挤在一起;回车之后提示消失,值进到上面日志里. 提示永远在最下面,所以不会出现
|
||||
"后来的输出跑到提示上面"这种次序错乱. 日志只留最近 6 条,更早的用 `..` 表示.
|
||||
|
||||
一开头就是 `INP` 的 tape 也没问题:提示会先把第一帧画出来再问,所以内存的初始状态是看得到的
|
||||
(为此输入回调拿到了迭代器).
|
||||
|
||||
精简画面没有 `in`/`out` 框,但只要有输入仍会补一行. 不读输入的 tape 不会多出框外的行,原有
|
||||
画面完全不变.
|
||||
精简画面没有 `in`/`out` 框,框外的日志照旧. 只产生输出、不读输入的 tape 也会有 `output>` 日志.
|
||||
|
||||
stdin 已经关掉(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是
|
||||
拿来试这个的最小例子.
|
||||
|
||||
+48
-33
@@ -46,6 +46,14 @@
|
||||
#define ANSI_CLEAR "\x1b[2J\x1b[H"
|
||||
#define ANSI_HOME "\x1b[H"
|
||||
|
||||
#define EVENTS_SHOWN 6 /* console lines below the frame */
|
||||
#define EVENTS_KEPT 256 /* remember this many, so a long run stays cheap */
|
||||
|
||||
struct viz_event {
|
||||
bool is_output;
|
||||
rot8_data value;
|
||||
};
|
||||
|
||||
struct rot8_viz {
|
||||
rot8_viz_opts opts;
|
||||
FILE *out;
|
||||
@@ -65,6 +73,9 @@ struct rot8_viz {
|
||||
size_t input_count;
|
||||
size_t input_cap;
|
||||
bool input_pending; /* the frame should show the input prompt */
|
||||
struct viz_event *events; /* inputs and outputs in the order they happened */
|
||||
size_t event_count;
|
||||
size_t event_cap;
|
||||
rot8_iter *last_iter; /* so the prompt can redraw the frame */
|
||||
};
|
||||
|
||||
@@ -450,54 +461,33 @@ static void viz_inputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
* 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;
|
||||
}
|
||||
/* records first, in the order things happened: what went in, then what came
|
||||
* out. The prompt is a separate line at the bottom, so the cursor never
|
||||
* sits above something that was printed later. */
|
||||
if (viz->input_count != 0) {
|
||||
first = viz->input_count > OUTPUT_SHOWN
|
||||
? viz->input_count - OUTPUT_SHOWN
|
||||
: 0;
|
||||
n = snprintf(buf, sizeof buf, "input>");
|
||||
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]);
|
||||
}
|
||||
/* One line per event, in the order they happened, so the interleaving of
|
||||
* INP and OUT is readable. The prompt is a separate empty line at the
|
||||
* bottom, which also keeps the cursor as the last thing on screen. */
|
||||
first = viz->event_count > EVENTS_SHOWN ? viz->event_count - EVENTS_SHOWN : 0;
|
||||
if (first != 0) {
|
||||
line_start(viz, out);
|
||||
if (color) {
|
||||
fputs(ANSI_IN, out);
|
||||
fputs(ANSI_DIM, out);
|
||||
}
|
||||
fputs(buf, out);
|
||||
fputs(" ..", out);
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
}
|
||||
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]);
|
||||
}
|
||||
for (i = first; i < viz->event_count; i++) {
|
||||
const struct viz_event *ev = &viz->events[i];
|
||||
line_start(viz, out);
|
||||
if (color) {
|
||||
fputs(ANSI_OUT, out);
|
||||
fputs(ev->is_output ? ANSI_OUT : ANSI_IN, out);
|
||||
}
|
||||
fputs(buf, out);
|
||||
fprintf(out, ev->is_output ? "output> %u" : "input> %u",
|
||||
(unsigned)ev->value);
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
@@ -546,6 +536,8 @@ static void viz_render(rot8_viz *viz, rot8_iter *iter, FILE *out,
|
||||
* public interface
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
static void viz_log(rot8_viz *viz, bool is_output, rot8_data value);
|
||||
|
||||
static void viz_push_output(rot8_viz *viz, rot8_data value) {
|
||||
if (viz->output_count == viz->output_cap) {
|
||||
size_t cap = viz->output_cap != 0 ? viz->output_cap * 2 : 32;
|
||||
@@ -558,6 +550,7 @@ static void viz_push_output(rot8_viz *viz, rot8_data value) {
|
||||
}
|
||||
viz->outputs[viz->output_count] = value;
|
||||
viz->output_count++;
|
||||
viz_log(viz, true, value);
|
||||
}
|
||||
|
||||
static void viz_replay_output(rot8_viz *viz) {
|
||||
@@ -611,6 +604,7 @@ void rot8_viz_free(rot8_viz *viz) {
|
||||
}
|
||||
free(viz->outputs);
|
||||
free(viz->inputs);
|
||||
free(viz->events);
|
||||
free(viz);
|
||||
}
|
||||
|
||||
@@ -627,6 +621,26 @@ void rot8_viz_dump(rot8_viz *viz, rot8_iter *iter) {
|
||||
fflush(viz->out);
|
||||
}
|
||||
|
||||
static void viz_log(rot8_viz *viz, bool is_output, rot8_data value) {
|
||||
if (viz->event_count == viz->event_cap) {
|
||||
size_t cap = viz->event_cap != 0 ? viz->event_cap * 2 : 32;
|
||||
struct viz_event *grown = realloc(viz->events, cap * sizeof(*grown));
|
||||
if (grown == NULL) {
|
||||
return;
|
||||
}
|
||||
viz->events = grown;
|
||||
viz->event_cap = cap;
|
||||
}
|
||||
if (viz->event_count == EVENTS_KEPT) { /* drop the oldest */
|
||||
memmove(viz->events, viz->events + 1,
|
||||
(EVENTS_KEPT - 1) * sizeof(*viz->events));
|
||||
viz->event_count--;
|
||||
}
|
||||
viz->events[viz->event_count].is_output = is_output;
|
||||
viz->events[viz->event_count].value = value;
|
||||
viz->event_count++;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -751,6 +765,7 @@ bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) {
|
||||
}
|
||||
*value = (rot8_data)parsed;
|
||||
viz_push_input(viz, *value);
|
||||
viz_log(viz, false, *value);
|
||||
viz->input_pending = false;
|
||||
if (viz->opts.live) {
|
||||
viz_redraw(viz, iter, false);
|
||||
|
||||
Reference in new issue
Block a user