From dc2c990c0feacb21e49cbb8743b2861beac2418b Mon Sep 17 00:00:00 2001 From: fang Date: Thu, 4 Feb 2021 01:05:48 +0100 Subject: [PATCH] term: avoid touching prompt during printf We want to decouple drum state & semantics from term.c. But we also want to continue showing printfs without mangling whatever the arvo side is doing. Short of formalizing various "terminal modes", the best we can do it assume that the main session is always in drum mode, and use that assumption to squeeze into the actual content stream when doing printfs. In essence, we insert the printf between the prompt and content, pushing the content up into terminal scrollback. Both the prompt and cursor position are maintained without knowledge of their original states. Because this logic relies on knowing the (accurate) terminal height, and this logic being the *only* thing in term.c that reads from the height, we no longer initialize the terminal size with a sane height. Instead, we set it to zero, and check for that to determine whether we're ready to use this logic or not. Due to the way this inserting works, trailing newlines are no longer required from the output. For consistency, we manually add trailing newlines when this logic cannot be used, both in the above-described circumstance, and the less-common case of u3l_log. This will of course need to be accounted for in all existing calls to u3l_log, but we move that into the next commit for readability's sake. --- pkg/urbit/noun/log.c | 2 ++ pkg/urbit/vere/io/term.c | 35 +++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/urbit/noun/log.c b/pkg/urbit/noun/log.c index 8d58fef29..acaf9abeb 100644 --- a/pkg/urbit/noun/log.c +++ b/pkg/urbit/noun/log.c @@ -22,6 +22,8 @@ u3l_log(const char* format, ...) // this process did not set a logging function, fallback to stderr // vfprintf(stderr, format, myargs); + fprintf(stderr, "\r\n"); + fflush(stderr); } va_end(myargs); diff --git a/pkg/urbit/vere/io/term.c b/pkg/urbit/vere/io/term.c index b4f11afd7..5dc3663bc 100644 --- a/pkg/urbit/vere/io/term.c +++ b/pkg/urbit/vere/io/term.c @@ -233,7 +233,7 @@ u3_term_log_init(void) // { uty_u->tat_u.siz.col_l = 80; - uty_u->tat_u.siz.row_l = 24; + uty_u->tat_u.siz.row_l = 0; } // initialize spinner state @@ -1452,7 +1452,7 @@ u3_term_io_hija(void) { u3_utty* uty_u = _term_main(); - if ( uty_u ) { + if ( uty_u && uty_u->tat_u.siz.row_l ) { if ( uty_u->fid_i > 2 ) { // We *should* in fact, produce some kind of fake FILE* for // non-console terminals. If we use this interface enough... @@ -1477,11 +1477,16 @@ u3_term_io_hija(void) perror("hija-fcntl-0"); c3_assert(!"hija-fcntl"); } - _write(uty_u->fid_i, "\r", 1); - { - uv_buf_t* buf_u = &uty_u->ufo_u.out.el_u; - _write(uty_u->fid_i, buf_u->base, buf_u->len); - } + + // save cursor position, + // set scroll region to exclude the prompt, + // scroll up one line to make space, + // and move the cursor onto that space. + // + _term_it_send_csi(uty_u, 's', 0); + _term_it_send_csi(uty_u, 'r', 2, 1, uty_u->tat_u.siz.row_l - 1); + _term_it_send_csi(uty_u, 'S', 1, 1); + _term_it_send_csi(uty_u, 'H', 2, uty_u->tat_u.siz.row_l - 1, 1); } return stdout; } @@ -1496,7 +1501,7 @@ u3_term_io_loja(int x) { u3_utty* uty_u = _term_main(); - if ( uty_u ) { + if ( uty_u && uty_u->tat_u.siz.row_l ) { if ( uty_u->fid_i > 2 ) { // We *should* in fact, produce some kind of fake FILE* for // non-console terminals. If we use this interface enough... @@ -1524,10 +1529,18 @@ u3_term_io_loja(int x) perror("hija-fcntl-0"); c3_assert(!"loja-fcntl"); } - _term_it_refresh_line(uty_u); + + // clear the scrolling region we set previously, + // and restore cursor to its original position. + // + _term_it_send_csi(uty_u, 'r', 0); + _term_it_send_csi(uty_u, 'u', 0); } } } + else { + fprintf(stdout, "\n"); + } } /* u3_term_it_log(): writes a log message @@ -1536,7 +1549,9 @@ void u3_term_io_log(c3_c* line) { FILE* stream = u3_term_io_hija(); - u3_term_io_loja(fprintf(stream, "%s", line)); + int x = fprintf(stream, "%s", line); + fflush(stream); + u3_term_io_loja(x); //TODO remove arg? unused... } /* u3_term_tape_to(): dump a tape to a file.