term: update module to work on linux and darwin

This commit is contained in:
2024-11-22 22:29:40 +00:00
parent 753d3ea9d3
commit d88b42f50e
6 changed files with 545 additions and 352 deletions

View File

@@ -1,5 +1,6 @@
#include "../../../line-ed/tty.h"
#include "../../tty.h"
#include <blue/term/tty.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -24,67 +25,78 @@
#define ANSI_TRUECOLOUR_BG "48;2"
enum tty_flags {
TTY_INIT = 0x01u,
TTY_INTERACTIVE = 0x02u,
B_TTY_INIT = 0x01u,
B_TTY_INTERACTIVE = 0x02u,
};
struct s_tty {
struct b_tty {
FILE *t_in, *t_out;
enum tty_flags t_flags;
struct termios t_ios_raw, t_ios_default;
struct s_tty_vmode t_vmode;
struct b_tty_vmode t_vmode;
unsigned char t_mcount;
struct tty_format_buf t_format_buf;
};
static struct s_tty tty = {};
static struct b_tty std = {0};
static struct b_tty err = {0};
static const char *ansi_colour16_fg[] = {
[TTY_COLOUR16_BLACK] = "30",
[TTY_COLOUR16_RED] = "31",
[TTY_COLOUR16_GREEN] = "32",
[TTY_COLOUR16_YELLOW] = "33",
[TTY_COLOUR16_BLUE] = "34",
[TTY_COLOUR16_MAGENTA] = "35",
[TTY_COLOUR16_CYAN] = "36",
[TTY_COLOUR16_WHITE] = "37",
[TTY_COLOUR16_GREY] = "90",
[TTY_COLOUR16_BRIGHT_RED] = "91",
[TTY_COLOUR16_BRIGHT_GREEN] = "92",
[TTY_COLOUR16_BRIGHT_YELLOW] = "93",
[TTY_COLOUR16_BRIGHT_BLUE] = "94",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
[TTY_COLOUR16_BRIGHT_CYAN] = "96",
[TTY_COLOUR16_BRIGHT_WHITE] = "97",
[B_TTY_COLOUR16_BLACK] = "30",
[B_TTY_COLOUR16_RED] = "31",
[B_TTY_COLOUR16_GREEN] = "32",
[B_TTY_COLOUR16_YELLOW] = "33",
[B_TTY_COLOUR16_BLUE] = "34",
[B_TTY_COLOUR16_MAGENTA] = "35",
[B_TTY_COLOUR16_CYAN] = "36",
[B_TTY_COLOUR16_WHITE] = "37",
[B_TTY_COLOUR16_BRIGHT_BLACK] = "90",
[B_TTY_COLOUR16_BRIGHT_RED] = "91",
[B_TTY_COLOUR16_BRIGHT_GREEN] = "92",
[B_TTY_COLOUR16_BRIGHT_YELLOW] = "93",
[B_TTY_COLOUR16_BRIGHT_BLUE] = "94",
[B_TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
[B_TTY_COLOUR16_BRIGHT_CYAN] = "96",
[B_TTY_COLOUR16_BRIGHT_WHITE] = "97",
};
static const char *ansi_colour16_bg[] = {
[TTY_COLOUR16_BLACK] = "40",
[TTY_COLOUR16_RED] = "41",
[TTY_COLOUR16_GREEN] = "42",
[TTY_COLOUR16_YELLOW] = "43",
[TTY_COLOUR16_BLUE] = "44",
[TTY_COLOUR16_MAGENTA] = "45",
[TTY_COLOUR16_CYAN] = "46",
[TTY_COLOUR16_WHITE] = "47",
[TTY_COLOUR16_GREY] = "100",
[TTY_COLOUR16_BRIGHT_RED] = "101",
[TTY_COLOUR16_BRIGHT_GREEN] = "102",
[TTY_COLOUR16_BRIGHT_YELLOW] = "103",
[TTY_COLOUR16_BRIGHT_BLUE] = "104",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
[TTY_COLOUR16_BRIGHT_CYAN] = "106",
[TTY_COLOUR16_BRIGHT_WHITE] = "107",
[B_TTY_COLOUR16_BLACK] = "40",
[B_TTY_COLOUR16_RED] = "41",
[B_TTY_COLOUR16_GREEN] = "42",
[B_TTY_COLOUR16_YELLOW] = "43",
[B_TTY_COLOUR16_BLUE] = "44",
[B_TTY_COLOUR16_MAGENTA] = "45",
[B_TTY_COLOUR16_CYAN] = "46",
[B_TTY_COLOUR16_WHITE] = "47",
[B_TTY_COLOUR16_BRIGHT_BLACK] = "100",
[B_TTY_COLOUR16_BRIGHT_RED] = "101",
[B_TTY_COLOUR16_BRIGHT_GREEN] = "102",
[B_TTY_COLOUR16_BRIGHT_YELLOW] = "103",
[B_TTY_COLOUR16_BRIGHT_BLUE] = "104",
[B_TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
[B_TTY_COLOUR16_BRIGHT_CYAN] = "106",
[B_TTY_COLOUR16_BRIGHT_WHITE] = "107",
};
static void init_tty(struct s_tty *tty, FILE *in, FILE *out)
static void init_tty(struct b_tty *tty, FILE *in, FILE *out)
{
tty->t_in = in;
tty->t_out = out;
int fd = fileno(in);
int fd = -1;
if (in) {
fd = fileno(in);
} else if (out) {
fd = fileno(out);
}
if (fd == -1) {
return;
}
if (isatty(fd)) {
tty->t_flags |= TTY_INTERACTIVE;
tty->t_flags |= B_TTY_INTERACTIVE;
}
tcgetattr(fd, &tty->t_ios_default);
@@ -94,69 +106,124 @@ static void init_tty(struct s_tty *tty, FILE *in, FILE *out)
tty->t_ios_raw.c_oflag &= ~(OPOST);
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
tty->t_flags |= TTY_INIT;
tty->t_flags |= B_TTY_INIT;
}
struct s_tty *s_get_tty(void)
struct b_tty *z__b_tty_get_std(void)
{
if (!(tty.t_flags & TTY_INIT)) {
init_tty(&tty, stdin, stdout);
if (!(std.t_flags & B_TTY_INIT)) {
init_tty(&std, stdin, stdout);
}
return &tty;
return &std;
}
bool s_tty_is_interactive(const struct s_tty *tty)
struct b_tty *z__b_tty_get_err(void)
{
return (tty->t_flags & TTY_INTERACTIVE) == TTY_INTERACTIVE;
if (!(err.t_flags & B_TTY_INIT)) {
init_tty(&err, NULL, stderr);
}
return &err;
}
void s_tty_set_raw(struct s_tty *tty)
struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_raw);
return &tty->t_format_buf;
}
void s_tty_set_canon(struct s_tty *tty)
void z__b_tty_putc(struct b_tty *tty, char c)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_default);
fputc(c, tty->t_out);
}
void s_tty_reset_vmode(struct s_tty *tty)
bool b_tty_is_interactive(const struct b_tty *tty)
{
if (tty->t_vmode.v_fg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_bg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_attrib == TTY_ATTRIB_NORMAL) {
return (tty->t_flags & B_TTY_INTERACTIVE) == B_TTY_INTERACTIVE;
}
static void set_raw(struct b_tty *tty)
{
int fd = -1;
if (tty->t_in) {
fd = fileno(tty->t_in);
} else if (tty->t_out) {
fd = fileno(tty->t_out);
}
if (fd == -1) {
return;
}
fprintf(tty->t_out, "\033[0;39;49m");
tcsetattr(fd, TCSAFLUSH, &tty->t_ios_raw);
}
static void set_canon(struct b_tty *tty)
{
int fd = -1;
if (tty->t_in) {
fd = fileno(tty->t_in);
} else if (tty->t_out) {
fd = fileno(tty->t_out);
}
if (fd == -1) {
return;
}
tcsetattr(fd, TCSAFLUSH, &tty->t_ios_default);
}
void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode)
{
switch (mode) {
case B_TTY_CANONICAL:
set_canon(tty);
break;
case B_TTY_RAW:
set_raw(tty);
break;
default:
break;
}
}
void b_tty_reset_vmode(struct b_tty *tty)
{
if (tty->t_vmode.v_fg.c_mode == B_TTY_COLOUR_NONE
&& tty->t_vmode.v_bg.c_mode == B_TTY_COLOUR_NONE
&& tty->t_vmode.v_attrib == B_TTY_ATTRIB_NORMAL) {
return;
}
fprintf(tty->t_out, "\033[0m");
memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode);
tty->t_vmode.v_fg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_bg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
tty->t_vmode.v_fg.c_mode = B_TTY_COLOUR_NONE;
tty->t_vmode.v_bg.c_mode = B_TTY_COLOUR_NONE;
tty->t_vmode.v_attrib = B_TTY_ATTRIB_NORMAL;
}
static int compare_colour(
const struct s_tty_colour *a, const struct s_tty_colour *b)
const struct b_tty_colour *a, const struct b_tty_colour *b)
{
if (a->c_mode != b->c_mode) {
return -1;
}
switch (a->c_mode) {
case TTY_COLOUR_16:
case B_TTY_COLOUR_16:
if (a->c_16.value != b->c_16.value) {
return -1;
}
break;
case TTY_COLOUR_256:
case B_TTY_COLOUR_256:
if (a->c_256.value != b->c_256.value) {
return -1;
}
break;
case TTY_COLOUR_TRUE:
case B_TTY_COLOUR_TRUE:
if (a->c_true.r != b->c_true.r) {
return -1;
}
@@ -176,7 +243,7 @@ static int compare_colour(
return 0;
}
static int compare_vmode(const struct s_tty_vmode *a, const struct s_tty_vmode *b)
static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode *b)
{
if (a->v_attrib != b->v_attrib) {
return -1;
@@ -193,7 +260,7 @@ static int compare_vmode(const struct s_tty_vmode *a, const struct s_tty_vmode *
return 0;
}
static void put_ansi_attrib(struct s_tty *tty, const char *s)
static void put_ansi_attrib(struct b_tty *tty, const char *s)
{
if (tty->t_mcount > 0) {
fputs(";", tty->t_out);
@@ -204,46 +271,46 @@ static void put_ansi_attrib(struct s_tty *tty, const char *s)
}
static void set_attrib(
struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
struct b_tty *tty, enum b_tty_attrib old, enum b_tty_attrib new)
{
if (old == new) {
return;
}
/* Bold ON */
if (!(old & TTY_ATTRIB_BOLD) && new &TTY_ATTRIB_BOLD) {
if (!(old & B_TTY_ATTRIB_BOLD) && new &B_TTY_ATTRIB_BOLD) {
put_ansi_attrib(tty, ANSI_BOLD_ON);
}
/* Bold OFF */
if (old & TTY_ATTRIB_BOLD && !(new &TTY_ATTRIB_BOLD)) {
if (old & B_TTY_ATTRIB_BOLD && !(new &B_TTY_ATTRIB_BOLD)) {
put_ansi_attrib(tty, ANSI_BOLD_OFF);
}
/* Underline ON */
if (!(old & TTY_ATTRIB_UNDERLINE) && new &TTY_ATTRIB_UNDERLINE) {
if (!(old & B_TTY_ATTRIB_UNDERLINE) && new &B_TTY_ATTRIB_UNDERLINE) {
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
}
/* Underline OFF */
if (old & TTY_ATTRIB_UNDERLINE && !(new &TTY_ATTRIB_UNDERLINE)) {
if (old & B_TTY_ATTRIB_UNDERLINE && !(new &B_TTY_ATTRIB_UNDERLINE)) {
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
}
/* Italic ON */
if (!(old & TTY_ATTRIB_ITALIC) && new &TTY_ATTRIB_ITALIC) {
if (!(old & B_TTY_ATTRIB_ITALIC) && new &B_TTY_ATTRIB_ITALIC) {
put_ansi_attrib(tty, ANSI_ITALIC_ON);
}
/* Italic OFF */
if (old & TTY_ATTRIB_ITALIC && !(new &TTY_ATTRIB_ITALIC)) {
if (old & B_TTY_ATTRIB_ITALIC && !(new &B_TTY_ATTRIB_ITALIC)) {
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
}
}
static void set_fg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
struct b_tty *tty, const struct b_tty_colour *old,
const struct b_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
@@ -252,18 +319,18 @@ static void set_fg(
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
case B_TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG);
break;
case TTY_COLOUR_16:
case B_TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]);
break;
case TTY_COLOUR_256:
case B_TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
case B_TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
@@ -278,8 +345,8 @@ static void set_fg(
}
static void set_bg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
struct b_tty *tty, const struct b_tty_colour *old,
const struct b_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
@@ -288,18 +355,18 @@ static void set_bg(
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
case B_TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG);
break;
case TTY_COLOUR_16:
case B_TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]);
break;
case TTY_COLOUR_256:
case B_TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
case B_TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
@@ -313,7 +380,7 @@ static void set_bg(
}
}
void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode)
void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode)
{
if (compare_vmode(&tty->t_vmode, vmode) == 0) {
return;
@@ -332,7 +399,7 @@ void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode)
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
}
s_keycode s_tty_read_key(struct s_tty *tty)
b_keycode b_tty_read_key(struct b_tty *tty)
{
char c;
int v;
@@ -341,19 +408,19 @@ s_keycode s_tty_read_key(struct s_tty *tty)
while (1) {
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
return B_KEY_EOF;
}
if (c == '\r' || c == '\n') {
return S_KEY_RETURN;
return B_KEY_RETURN;
}
if (c == '\b' || c == 0x7F) {
return S_KEY_BACKSPACE;
return B_KEY_BACKSPACE;
}
if (c >= 1 && c <= 26) {
return S_TTY_CTRL_KEY(c + 'a' - 1);
return B_TTY_CTRL_KEY(c + 'a' - 1);
}
if (c != 0x1b) {
@@ -362,7 +429,7 @@ s_keycode s_tty_read_key(struct s_tty *tty)
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
return B_KEY_EOF;
}
if (c != '[') {
@@ -371,18 +438,18 @@ s_keycode s_tty_read_key(struct s_tty *tty)
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
return B_KEY_EOF;
}
switch (c) {
case 'A':
return S_KEY_ARROW_UP;
return B_KEY_ARROW_UP;
case 'B':
return S_KEY_ARROW_DOWN;
return B_KEY_ARROW_DOWN;
case 'C':
return S_KEY_ARROW_RIGHT;
return B_KEY_ARROW_RIGHT;
case 'D':
return S_KEY_ARROW_LEFT;
return B_KEY_ARROW_LEFT;
default:
continue;
}
@@ -391,9 +458,9 @@ s_keycode s_tty_read_key(struct s_tty *tty)
return c;
}
void s_tty_move_cursor_x(struct s_tty *tty, enum s_tty_position_base base, int pos)
void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int pos)
{
if (base == TTY_POS_CURSOR && pos < 0 && pos >= -4) {
if (base == B_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
for (int i = 0; i > pos; i--) {
fputc('\b', tty->t_out);
}
@@ -401,7 +468,7 @@ void s_tty_move_cursor_x(struct s_tty *tty, enum s_tty_position_base base, int p
return;
}
if (base == TTY_POS_START) {
if (base == B_TTY_POS_START) {
if (pos == 0) {
fputs("\033[G", tty->t_out);
} else {
@@ -420,9 +487,9 @@ void s_tty_move_cursor_x(struct s_tty *tty, enum s_tty_position_base base, int p
}
}
void s_tty_move_cursor_y(struct s_tty *tty, enum s_tty_position_base base, int pos)
void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int pos)
{
if (base == TTY_POS_START) {
if (base == B_TTY_POS_START) {
/* we don't need this functionality right now */
abort();
}
@@ -438,24 +505,48 @@ void s_tty_move_cursor_y(struct s_tty *tty, enum s_tty_position_base base, int p
}
}
void s_tty_clear(struct s_tty *tty, enum s_tty_clear_mode mode)
void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode)
{
const char *arg;
if (mode & TTY_CLEAR_ALL) {
if (mode & B_TTY_CLEAR_ALL) {
arg = "2";
} else if (mode & TTY_CLEAR_TO_CURSOR) {
} else if (mode & B_TTY_CLEAR_TO_CURSOR) {
arg = "1";
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
} else if (mode & B_TTY_CLEAR_FROM_CURSOR) {
arg = "";
} else {
abort();
}
if (mode & TTY_CLEAR_SCREEN) {
if (mode & B_TTY_CLEAR_SCREEN) {
fprintf(tty->t_out, "\033[%sJ", arg);
} else if (mode & TTY_CLEAR_LINE) {
} else if (mode & B_TTY_CLEAR_LINE) {
fprintf(tty->t_out, "\033[%sK", arg);
} else {
abort();
}
}
enum b_status b_tty_get_dimensions(
struct b_tty *tty, unsigned int *w, unsigned int *h)
{
if (!(tty->t_flags & B_TTY_INTERACTIVE)) {
return -1;
}
int fd = fileno(tty->t_out);
struct winsize ws;
if (ioctl(fd, TIOCGWINSZ, &ws) == -1) {
return -1;
}
if (w) {
*w = ws.ws_col;
}
if (h) {
*h = ws.ws_row;
}
return 0;
}