MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve multiline wrapping

+67 -4
+67 -4
src/repl.c
··· 14 14 #define STDIN_FILENO 0 15 15 #define mkdir_p(path) _mkdir(path) 16 16 #else 17 + #include <sys/ioctl.h> 17 18 #include <termios.h> 18 19 #include <unistd.h> 19 20 #define mkdir_p(path) mkdir(path, 0755) ··· 535 536 536 537 static crprintf_compiled *hl_prog = NULL; 537 538 static highlight_state hl_line_state = HL_STATE_INIT; 539 + static int repl_last_render_rows = 1; 540 + 541 + static int repl_terminal_cols(void) { 542 + int cols = 80; 543 + #ifdef _WIN32 544 + CONSOLE_SCREEN_BUFFER_INFO csbi; 545 + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { 546 + cols = csbi.srWindow.Right - csbi.srWindow.Left + 1; 547 + } 548 + #else 549 + struct winsize ws; 550 + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0) { 551 + cols = ws.ws_col; 552 + } 553 + #endif 554 + return cols > 0 ? cols : 80; 555 + } 556 + 557 + static void repl_move_to_line_start(const char *prompt, int pos, int cols) { 558 + int prompt_len = (int)strlen(prompt); 559 + int cursor_cols = prompt_len + pos; 560 + int cursor_row = cursor_cols / cols; 561 + if (cursor_cols > 0 && cursor_cols % cols == 0) cursor_row--; 562 + 563 + if (cursor_row > 0) { 564 + char move_buf[32]; 565 + snprintf(move_buf, sizeof(move_buf), "\033[%dA", cursor_row); 566 + fputs(move_buf, stdout); 567 + } 568 + fputs("\r", stdout); 569 + } 538 570 539 571 static void refresh_line(const char *line, int len, int pos, const char *prompt) { 540 - fputs("\r\033[2K", stdout); 572 + int cols = repl_terminal_cols(); 573 + int prompt_len = (int)strlen(prompt); 574 + int line_cols = prompt_len + len; 575 + int current_rows = line_cols > 0 ? (line_cols - 1) / cols + 1 : 1; 576 + int rows = repl_last_render_rows > current_rows ? repl_last_render_rows : current_rows; 577 + 578 + repl_move_to_line_start(prompt, pos, cols); 579 + for (int i = 0; i < rows; i++) { 580 + fputs("\033[K", stdout); 581 + if (i < rows - 1) fputs("\033[B\r", stdout); 582 + } 583 + for (int i = 0; i < rows - 1; i++) fputs("\033[A", stdout); 584 + fputs("\r", stdout); 585 + 541 586 fputs(prompt, stdout); 542 587 543 588 if (crprintf_get_color() && len > 0 && len <= 2048) { ··· 555 600 556 601 fputs(rendered, stdout); 557 602 } else if (len > 0) fwrite(line, 1, (size_t)len, stdout); 603 + 604 + int end_cols = prompt_len + len; 605 + int end_row = end_cols > 0 ? end_cols / cols : 0; 606 + int cursor_cols = prompt_len + pos; 607 + int cursor_row = cursor_cols > 0 ? cursor_cols / cols : 0; 608 + int cursor_col = cursor_cols > 0 ? cursor_cols % cols : 0; 609 + int up_rows = end_row - cursor_row; 610 + 611 + if (up_rows > 0) { 612 + char move_buf[32]; 613 + snprintf(move_buf, sizeof(move_buf), "\033[%dA", up_rows); 614 + fputs(move_buf, stdout); 615 + } 558 616 559 - if (pos < len) { 560 - size_t prompt_len = strlen(prompt); 561 - printf("\r\033[%zuC", prompt_len + (size_t)pos); 617 + fputs("\r", stdout); 618 + if (cursor_col > 0) { 619 + char move_buf[32]; 620 + snprintf(move_buf, sizeof(move_buf), "\033[%dC", cursor_col); 621 + fputs(move_buf, stdout); 562 622 } 623 + 624 + repl_last_render_rows = end_cols > 0 ? end_cols / cols + 1 : 1; 563 625 564 626 fflush(stdout); 565 627 } ··· 679 741 static char *read_line_with_history(history_t *hist, ant_t *js, const char *prompt) { 680 742 char *line = malloc(MAX_LINE_LENGTH); 681 743 int pos = 0, len = 0; line[0] = '\0'; 744 + repl_last_render_rows = 1; 682 745 683 746 #ifndef _WIN32 684 747 struct termios new_tio;