#define _POSIX_C_SOURCE 200809L #include "../../tty.h" #include #include #include #include #include #define ANSI_BOLD_ON "1" #define ANSI_BOLD_OFF "22" #define ANSI_ITALIC_ON "3" #define ANSI_ITALIC_OFF "23" #define ANSI_UNDERLINE_ON "4" #define ANSI_UNDERLINE_OFF "24" #define ANSI_DEFAULTCOLOUR_FG "39" #define ANSI_DEFAULTCOLOUR_BG "49" #define ANSI_256COLOUR_FG "38;5" #define ANSI_256COLOUR_BG "48;5" #define ANSI_TRUECOLOUR_FG "38;2" #define ANSI_TRUECOLOUR_BG "48;2" enum tty_flags { FX_TTY_INIT = 0x01u, FX_TTY_INTERACTIVE = 0x02u, }; struct fx_tty { FILE *t_in, *t_out; enum tty_flags t_flags; struct fx_tty_vmode t_vmode; unsigned char t_mcount; struct tty_format_buf t_format_buf; }; static struct fx_tty std = {0}; static struct fx_tty err = {0}; static const char *ansi_colour16_fg[] = { [FX_TTY_COLOUR16_BLACK] = "30", [FX_TTY_COLOUR16_RED] = "31", [FX_TTY_COLOUR16_GREEN] = "32", [FX_TTY_COLOUR16_YELLOW] = "33", [FX_TTY_COLOUR16_FX] = "34", [FX_TTY_COLOUR16_MAGENTA] = "35", [FX_TTY_COLOUR16_CYAN] = "36", [FX_TTY_COLOUR16_WHITE] = "37", [FX_TTY_COLOUR16_BRIGHT_BLACK] = "90", [FX_TTY_COLOUR16_BRIGHT_RED] = "91", [FX_TTY_COLOUR16_BRIGHT_GREEN] = "92", [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "93", [FX_TTY_COLOUR16_BRIGHT_FX] = "94", [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "95", [FX_TTY_COLOUR16_BRIGHT_CYAN] = "96", [FX_TTY_COLOUR16_BRIGHT_WHITE] = "97", }; static const char *ansi_colour16_bg[] = { [FX_TTY_COLOUR16_BLACK] = "40", [FX_TTY_COLOUR16_RED] = "41", [FX_TTY_COLOUR16_GREEN] = "42", [FX_TTY_COLOUR16_YELLOW] = "43", [FX_TTY_COLOUR16_FX] = "44", [FX_TTY_COLOUR16_MAGENTA] = "45", [FX_TTY_COLOUR16_CYAN] = "46", [FX_TTY_COLOUR16_WHITE] = "47", [FX_TTY_COLOUR16_BRIGHT_BLACK] = "100", [FX_TTY_COLOUR16_BRIGHT_RED] = "101", [FX_TTY_COLOUR16_BRIGHT_GREEN] = "102", [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "103", [FX_TTY_COLOUR16_BRIGHT_FX] = "104", [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "105", [FX_TTY_COLOUR16_BRIGHT_CYAN] = "106", [FX_TTY_COLOUR16_BRIGHT_WHITE] = "107", }; static void init_tty(struct fx_tty *tty, FILE *in, FILE *out) { } struct fx_tty *z__fx_tty_get_std(void) { if (!(std.t_flags & FX_TTY_INIT)) { init_tty(&std, stdin, stdout); } return &std; } struct fx_tty *z__fx_tty_get_err(void) { if (!(err.t_flags & FX_TTY_INIT)) { init_tty(&err, NULL, stderr); } return &err; } struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty) { return &tty->t_format_buf; } void z__fx_tty_putc(struct fx_tty *tty, char c) { fputc(c, tty->t_out); } bool fx_tty_is_interactive(const struct fx_tty *tty) { return (tty->t_flags & FX_TTY_INTERACTIVE) == FX_TTY_INTERACTIVE; } void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode) { } void fx_tty_reset_vmode(struct fx_tty *tty) { } void fx_tty_flush(struct fx_tty *tty) { } static int compare_colour( const struct fx_tty_colour *a, const struct fx_tty_colour *b) { if (a->c_mode != b->c_mode) { return -1; } switch (a->c_mode) { case FX_TTY_COLOUR_16: if (a->c_16.value != b->c_16.value) { return -1; } break; case FX_TTY_COLOUR_256: if (a->c_256.value != b->c_256.value) { return -1; } break; case FX_TTY_COLOUR_TRUE: if (a->c_true.r != b->c_true.r) { return -1; } if (a->c_true.g != b->c_true.g) { return -1; } if (a->c_true.b != b->c_true.b) { return -1; } break; default: break; } return 0; } static int compare_vmode( const struct fx_tty_vmode *a, const struct fx_tty_vmode *b) { if (a->v_attrib != b->v_attrib) { return -1; } if (compare_colour(&a->v_fg, &b->v_fg) != 0) { return -1; } if (compare_colour(&a->v_bg, &b->v_bg) != 0) { return -1; } return 0; } void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode) { } fx_keycode fx_tty_read_key(struct fx_tty *tty) { return FX_KEY_EOF; } void fx_tty_move_cursor_x( struct fx_tty *tty, enum fx_tty_position_base base, int pos) { } void fx_tty_move_cursor_y( struct fx_tty *tty, enum fx_tty_position_base base, int pos) { } void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode) { } enum fx_status fx_tty_get_dimensions( struct fx_tty *tty, unsigned int *w, unsigned int *h) { return -1; } enum fx_status fx_tty_get_cursor_position( struct fx_tty *tty, unsigned int *w, unsigned int *h) { return -1; }