lt

simple terminal emulator
Log | Files | Refs | git clone https://git.ne02ptzero.me/git/lt

commit eb02994107c704ab7a1e9fd2b6a981424e0f553c
parent ef2ae0ec8d506bdd6262512b063d176a23d4339c
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 12 Jun 2018 15:23:59 +0200

NEW: Terminal control codes

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>

Diffstat:
Mterm.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 98 insertions(+), 4 deletions(-)

diff --git a/term.c b/term.c @@ -257,6 +257,90 @@ int term_write(term_t *term, const char *buf, int len, bool show_ctrl) return n; } +static void term_put_tab(term_t *t, int n) +{ + int32_t x = t->c.x; + + if (n > 0) + { + while (x < t->col && n--) + { + for (x++; x < t->col && !t->tabs[x]; x++) + ; + } + } + else if (n < 0) + { + while (x > 0 && n++) + { + for (x--; x > 0 && !t->tabs[x]; x--) + ; + } + } + + t->c.x = LIMIT(x, 0, t->col - 1); +} + +static void term_scroll_up(term_t *t, int orig, int n) +{ + int i; + char_t *tmp; + + LIMIT(n, 0, t->bot - orig + 1); + + term_clear_region(t, 0, orig, t->col - 1, orig + n + 1); + term_set_dirt(t, orig + n, t->bot); + + for (i = orig; i <= t->bot - n; i++) + { + tmp = t->line[i]; + t->line[i] = t->line[i + n]; + t->line[i + n] = tmp; + } +} + +static void term_new_line(term_t *t, int first_col) +{ + int y = t->c.y; + + if (y == t->bot) + term_scroll_up(t, t->top, 1); + else + y++; + + term_move_to(t, first_col ? 0 : t->c.x, y); +} + +static void term_control_code(term_t *t, uint8_t ascii) +{ + switch (ascii) + { + case '\t': + term_put_tab(t, 1); + return; + case '\b': + term_move_to(t, t->c.x - 1, t->c.y); + return; + case '\r': + term_move_to(t, 0, t->c.y); + return ; + case '\f': + case '\v': + case '\n': + term_new_line(t, IS_SET(t->mode, MODE_CRLF)); + return ; + case '\a': + /* XXX: BELL */ + return; + case '\033': + t->esc &= ~(ESC_CSI | ESC_ALTCHARSET | ESC_TEST); + t->esc |= ESC_START; + return; + } + + t->esc &= ~(ESC_STR_END | ESC_STR); +} + void term_putc(term_t *t, uint_least32_t u) { char c[UTF_SIZ]; @@ -269,13 +353,13 @@ void term_putc(term_t *t, uint_least32_t u) if (!IS_SET(t->mode, MODE_UTF8) && !IS_SET(t->mode, MODE_SIXEL)) { c[0] = u; - width = len - 1; + width = len = 1; } else { len = utf8_encode(u, c); width = wcwidth(u); - if (!ctrl && width == 01) + if (!ctrl && width == -1) { memcpy(c, STR_UTF_INVALID, sizeof(STR_UTF_INVALID)); width = 1; @@ -293,6 +377,16 @@ void term_putc(term_t *t, uint_least32_t u) } check_ctrl_code: - return ; - + if (ctrl) + { + term_control_code(t, u); + return ; + } + else if (t->esc & ESC_START) + { + if (t->esc & ESC_CSI) + { + /* Colors */ + } + } }