eb02994107c704ab7a1e9fd2b6a981424e0f553c — Ne02ptzero 1 year, 4 months ago ef2ae0e master
NEW: Terminal control codes

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>
1 files changed, 98 insertions(+), 4 deletions(-)

M term.c
M term.c => term.c +98 -4
@@ 257,6 257,90 @@ 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 @@ 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 @@ }
  
  check_ctrl_code:
-     return ;
- 
+     if (ctrl)
+     {
+         term_control_code(t, u);
+         return ;
+     }
+     else if (t->esc & ESC_START)
+     {
+         if (t->esc & ESC_CSI)
+         {
+             /* Colors */
+         }
+     }
  }