From 300f83e66b08590a600a5356d6c69096fd6a3452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Mon, 20 Jan 2020 18:34:32 +0100 Subject: [PATCH] term: factor out character printing to new function term_print() --- terminal.c | 75 +++++++++++++++++++++++++++++++++++++++++++ terminal.h | 2 ++ vt.c | 94 +++--------------------------------------------------- 3 files changed, 81 insertions(+), 90 deletions(-) diff --git a/terminal.c b/terminal.c index ffc2dbbf..a809dfd6 100644 --- a/terminal.c +++ b/terminal.c @@ -1840,3 +1840,78 @@ term_disable_app_sync_updates(struct terminal *term) term->render.app_sync_updates.timer_fd, 0, &(struct itimerspec){{0}}, NULL); } + +static inline void +print_linewrap(struct terminal *term) +{ + if (likely(!term->cursor.lcf)) { + /* Not and end of line */ + return; + } + + if (unlikely(!term->auto_margin)) { + /* Auto-wrap disabled */ + return; + } + + if (term->cursor.point.row == term->scroll_region.end - 1) { + term_scroll(term, 1); + term_cursor_to(term, term->cursor.point.row, 0); + } else + term_cursor_to(term, min(term->cursor.point.row + 1, term->rows - 1), 0); +} + +static inline void +print_insert(struct terminal *term, int width) +{ + assert(width > 0); + + if (unlikely(term->insert_mode)) { + struct row *row = term->grid->cur_row; + const size_t move_count = max(0, term->cols - term->cursor.point.col - width); + + memmove( + &row->cells[term->cursor.point.col + width], + &row->cells[term->cursor.point.col], + move_count * sizeof(struct cell)); + + /* Mark moved cells as dirty */ + for (size_t i = term->cursor.point.col + width; i < term->cols; i++) + row->cells[i].attrs.clean = 0; + } +} + +void +term_print(struct terminal *term, wchar_t wc, int width) +{ + if (unlikely(width <= 0)) + return; + + struct row *row = term->grid->cur_row; + struct cell *cell = &row->cells[term->cursor.point.col]; + + print_linewrap(term); + print_insert(term, width); + + cell->wc = term->vt.last_printed = wc; + cell->attrs = term->vt.attrs; + + row->dirty = true; + cell->attrs.clean = 0; + + /* Advance cursor the 'additional' columns while dirty:ing the cells */ + for (int i = 1; i < width && term->cursor.point.col < term->cols - 1; i++) { + term_cursor_right(term, 1); + + assert(term->cursor.point.col < term->cols); + struct cell *cell = &row->cells[term->cursor.point.col]; + cell->wc = 0; + cell->attrs.clean = 0; + } + + /* Advance cursor */ + if (term->cursor.point.col < term->cols - 1) + term_cursor_right(term, 1); + else + term->cursor.lcf = true; +} diff --git a/terminal.h b/terminal.h index 8c56648e..7494efba 100644 --- a/terminal.h +++ b/terminal.h @@ -382,6 +382,8 @@ void term_cursor_blink_enable(struct terminal *term); void term_cursor_blink_disable(struct terminal *term); void term_cursor_blink_restart(struct terminal *term); +void term_print(struct terminal *term, wchar_t wc, int width); + void term_scroll(struct terminal *term, int rows); void term_scroll_reverse(struct terminal *term, int rows); diff --git a/vt.c b/vt.c index 19044339..8eeefda2 100644 --- a/vt.c +++ b/vt.c @@ -94,49 +94,6 @@ esc_as_string(struct terminal *term, uint8_t final) } #endif -static inline void -pre_print(struct terminal *term) -{ - if (likely(!term->cursor.lcf)) - return; - if (unlikely(!term->auto_margin)) - return; - - if (term->cursor.point.row == term->scroll_region.end - 1) { - term_scroll(term, 1); - term_cursor_to(term, term->cursor.point.row, 0); - } else - term_cursor_to(term, min(term->cursor.point.row + 1, term->rows - 1), 0); -} - -static inline void -post_print(struct terminal *term) -{ - if (term->cursor.point.col < term->cols - 1) - term_cursor_right(term, 1); - else - term->cursor.lcf = true; -} - -static inline void -print_insert(struct terminal *term, int width) -{ - assert(width > 0); - if (unlikely(term->insert_mode)) { - struct row *row = term->grid->cur_row; - const size_t move_count = max(0, term->cols - term->cursor.point.col - width); - - memmove( - &row->cells[term->cursor.point.col + width], - &row->cells[term->cursor.point.col], - move_count * sizeof(struct cell)); - - /* Mark moved cells as dirty */ - for (size_t i = term->cursor.point.col + width; i < term->cols; i++) - row->cells[i].attrs.clean = 0; - } -} - static void action_ignore(struct terminal *term) { @@ -252,16 +209,6 @@ action_execute(struct terminal *term, uint8_t c) static void action_print(struct terminal *term, uint8_t c) { - pre_print(term); - - struct row *row = term->grid->cur_row; - struct cell *cell = &row->cells[term->cursor.point.col]; - - row->dirty = true; - cell->attrs.clean = 0; - - print_insert(term, 1); - /* 0x60 - 0x7e */ static const wchar_t vt100_0[] = { L'◆', L'▒', L'␉', L'␌', L'␍', L'␊', L'°', L'±', /* ` - g */ @@ -273,14 +220,10 @@ action_print(struct terminal *term, uint8_t c) if (unlikely(term->charsets.set[term->charsets.selected] == CHARSET_GRAPHIC) && c >= 0x60 && c <= 0x7e) { - cell->wc = vt100_0[c - 0x60]; + term_print(term, vt100_0[c - 0x60], 1); } else { - // LOG_DBG("print: ASCII: %c (0x%04x)", c, c); - cell->wc = c; + term_print(term, c, 1); } - - cell->attrs = term->vt.attrs; - post_print(term); } static void @@ -528,46 +471,17 @@ action_utf8_4_entry(struct terminal *term, uint8_t c) static void action_utf8_print(struct terminal *term, uint8_t c) { - pre_print(term); - - struct row *row = term->grid->cur_row; - struct cell *cell = &row->cells[term->cursor.point.col]; - /* Convert to wchar */ mbstate_t ps = {0}; wchar_t wc; if (mbrtowc(&wc, (const char *)term->vt.utf8.data, term->vt.utf8.idx, &ps) < 0) wc = 0; - int width = wcwidth(wc); - if (width > 0) - print_insert(term, width); - - row->dirty = true; - cell->wc = wc; - cell->attrs.clean = 0; - cell->attrs = term->vt.attrs; - /* Reset VT utf8 state */ term->vt.utf8.idx = 0; - if (width <= 0) { - /* Skip post_print() below - i.e. don't advance cursor */ - return; - } - - /* Advance cursor the 'additional' columns (last step is done - * by post_print()) */ - for (int i = 1; i < width && term->cursor.point.col < term->cols - 1; i++) { - term_cursor_right(term, 1); - - assert(term->cursor.point.col < term->cols); - struct cell *cell = &row->cells[term->cursor.point.col]; - cell->wc = 0; - cell->attrs.clean = 0; - } - - post_print(term); + int width = wcwidth(wc); + term_print(term, wc, width); } static enum state