diff options
Diffstat (limited to 'include/escape.h')
| -rwxr-xr-x | include/escape.h | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/include/escape.h b/include/escape.h new file mode 100755 index 0000000..af616b3 --- /dev/null +++ b/include/escape.h @@ -0,0 +1,272 @@ +// escape.h - ESC escape code macros and functions +// +// Thread safety: Safe for concurrent use (no shared state). The macros +// produce string literals; the functions write to the provided FILE*. +// Portability: ANSI escape codes are not supported on Windows Console API +// (use Windows 10+ VT mode or a translation layer). + +#ifndef ESCAPE_H +#define ESCAPE_H + +#include "type.h" +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define CSI "\x1B[" + +#define ESC_CURSOR_UP(n) CSI #n "A" +void esc_cursor_up(u32 n); +void esc_fcursor_up(FILE *, u32 n); + +#define ESC_CURSOR_DOWN(n) CSI #n "B" +void esc_cursor_down(u32 n); +void esc_fcursor_down(FILE *, u32 n); + +#define ESC_CURSOR_LEFT(n) CSI #n "D" +void esc_cursor_left(u32 n); +void esc_fcursor_left(FILE *, u32 n); + +#define ESC_CURSOR_RIGHT(n) CSI #n "C" +void esc_cursor_right(u32 n); +void esc_fcursor_right(FILE *, u32 n); + +#define ESC_CURSOR_LINE_NEXT(n) CSI #n "E" +void esc_cursor_line_next(u32 n); +void esc_fcursor_line_next(FILE *, u32 n); + +#define ESC_CURSOR_PREVIOUS(n) CSI #n "F" +void esc_cursor_previous(u32 n); +void esc_fcursor_previous(FILE *, u32 n); + +#define ESC_CURSOR_POSITION(col, row) CSI #row ";" #col "H" +void esc_cursor_position(u32 col, u32 row); +void esc_fcursor_position(FILE *, u32 col, u32 row); + +#define ESC_CURSOR_COLUMN_HOME CSI "G" + +#define ESC_CLEAR_SCREEN CSI "2J" +void esc_clear_screen(void); +void esc_fclear_screen(FILE *); + +#define ESC_CLEAR_SCREEN_START CSI "1J" +void esc_clear_screen_start(void); +void esc_fclear_screen_start(FILE *); + +#define ESC_CLEAR_SCREEN_END CSI "0J" +void esc_clear_screen_end(void); +void esc_fclear_screen_end(FILE *); + +#define ESC_CLEAR_LINE CSI "2K" +void esc_clear_line(void); +void esc_fclear_line(FILE *); + +#define ESC_CLEAR_LINE_START CSI "1K" +void esc_clear_line_start(void); +void esc_fclear_line_start(FILE *); + +#define ESC_CLEAR_LINE_END CSI "0K" +void esc_clear_line_end(void); +void esc_fclear_line_end(FILE *); + +#define ESC_SCROLL_UP(n) CSI #n "S" +void esc_scroll_up(u32 n); +void esc_fscroll_up(FILE *, u32 n); + +#define ESC_SCROLL_DOWN(n) CSI #n "T" +void esc_scroll_down(u32 n); +void esc_fscroll_down(FILE *, u32 n); + +#define ESC_SGR "m" + +#define ESC_RESET CSI "0" ESC_SGR +#define ESC_BOLD CSI "1" ESC_SGR +#define ESC_DIM CSI "2" ESC_SGR +#define ESC_ITALIC CSI "3" ESC_SGR +#define ESC_UNDERLINE CSI "4" ESC_SGR +#define ESC_BLINK CSI "5" ESC_SGR +#define ESC_REVERSE CSI "7" ESC_SGR +#define ESC_CONCEAL CSI "8" ESC_SGR +#define ESC_REVEAL CSI "28" ESC_SGR +#define ESC_STRIKETHROUGH CSI "9" ESC_SGR +#define ESC_OVERLINE CSI "53" ESC_SGR + +#define ESC_COLOR_FG_RGB(r, g, b) CSI "38;2;" #r ";" #g ";" #b ESC_SGR +void esc_color_fg_rgb(u32 r, u32 g, u32 b); +void esc_fcolor_fg_rgb(FILE *, u32 r, u32 g, u32 b); + +#define ESC_COLOR_BG_RGB(r, g, b) CSI "48;2;" #r ";" #g ";" #b ESC_SGR +void esc_color_bg_rgb(u32 r, u32 g, u32 b); +void esc_fcolor_bg_rgb(FILE *, u32 r, u32 g, u32 b); + +#define ESC_COLOR_FG_256(n) CSI "38;5;" #n ESC_SGR +void esc_color_fg_256(u32 n); +void esc_fcolor_fg_256(FILE *, u32 n); + +#define ESC_COLOR_BG_256(n) CSI "48;5;" #n ESC_SGR +void esc_color_bg_256(u32 n); +void esc_fcolor_bg_256(FILE *, u32 n); + +#define ESC_COLOR_FG(n) CSI "3" #n ESC_SGR +#define ESC_COLOR_BG(n) CSI "4" #n ESC_SGR +#define ESC_COLOR_FG_BRIGHT(n) CSI "9" #n ESC_SGR +#define ESC_COLOR_BG_BRIGHT(n) CSI "10" #n ESC_SGR + +/* Named standard colors (ANSI 0-7). */ +#define ESC_COLOR_BLACK ESC_COLOR_FG(0) +#define ESC_COLOR_RED ESC_COLOR_FG(1) +#define ESC_COLOR_GREEN ESC_COLOR_FG(2) +#define ESC_COLOR_YELLOW ESC_COLOR_FG(3) +#define ESC_COLOR_BLUE ESC_COLOR_FG(4) +#define ESC_COLOR_MAGENTA ESC_COLOR_FG(5) +#define ESC_COLOR_CYAN ESC_COLOR_FG(6) +#define ESC_COLOR_WHITE ESC_COLOR_FG(7) + +#define ESC_CURSOR_HIDE CSI "?25l" +void esc_cursor_hide(void); +void esc_fcursor_hide(FILE *); + +#define ESC_CURSOR_SHOW CSI "?25h" +void esc_cursor_show(void); +void esc_fcursor_show(FILE *); + +#define ESC_CURSOR_SAVE CSI "s" +void esc_cursor_save(void); +void esc_fcursor_save(FILE *); + +#define ESC_CURSOR_RESTORE CSI "u" +void esc_cursor_restore(void); +void esc_fcursor_restore(FILE *); + +#define ESC_BELL "\x07" +void esc_bell(void); +void esc_fbell(FILE *); + +#define ESC_ALT_SCREEN_ON CSI "?1049h" +void esc_alt_screen_on(void); +void esc_falt_screen_on(FILE *); + +#define ESC_ALT_SCREEN_OFF CSI "?1049l" +void esc_alt_screen_off(void); +void esc_falt_screen_off(FILE *); + +#define ESC_BRACKETED_PASTE_ON CSI "?2004h" +void esc_bracketed_paste_on(void); +void esc_fbracketed_paste_on(FILE *); + +#define ESC_BRACKETED_PASTE_OFF CSI "?2004l" +void esc_bracketed_paste_off(void); +void esc_fbracketed_paste_off(FILE *); + +#define ESC_KITTY_ON CSI ">1u" +void esc_kitty_on(void); +void esc_fkitty_on(FILE *); + +#define ESC_KITTY_OFF CSI ">0u" +void esc_kitty_off(void); +void esc_fkitty_off(FILE *); + +#ifdef __cplusplus +} +#endif + +#endif // ESCAPE_H + +#ifdef ESCAPE_IMPLEMENTATION + +#include <stdio.h> + +void esc_fcursor_up(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uA", (unsigned)n); } +void esc_fcursor_down(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uB", (unsigned)n); } +void esc_fcursor_left(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uD", (unsigned)n); } +void esc_fcursor_right(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uC", (unsigned)n); } +void esc_fcursor_line_next(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uE", (unsigned)n); } +void esc_fcursor_previous(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uF", (unsigned)n); } +void esc_fcursor_position(FILE *f, u32 col, u32 row) { + if (!f) return; + fprintf(f, CSI "%u;%uH", (unsigned)row, (unsigned)col); +} +void esc_fclear_screen(FILE *f) { if (!f) return; fputs(CSI "2J", f); } +void esc_fclear_screen_start(FILE *f) { if (!f) return; fputs(CSI "1J", f); } +void esc_fclear_screen_end(FILE *f) { if (!f) return; fputs(CSI "0J", f); } +void esc_fclear_line(FILE *f) { if (!f) return; fputs(CSI "2K", f); } +void esc_fclear_line_start(FILE *f) { if (!f) return; fputs(CSI "1K", f); } +void esc_fclear_line_end(FILE *f) { if (!f) return; fputs(CSI "0K", f); } +void esc_fscroll_up(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uS", (unsigned)n); } +void esc_fscroll_down(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uT", (unsigned)n); } + +void esc_fcolor_fg_rgb(FILE *f, u32 r, u32 g, u32 b) { + if (!f) return; + fprintf(f, CSI "38;2;%u;%u;%u" ESC_SGR, (unsigned)r, (unsigned)g, (unsigned)b); +} +void esc_fcolor_bg_rgb(FILE *f, u32 r, u32 g, u32 b) { + if (!f) return; + fprintf(f, CSI "48;2;%u;%u;%u" ESC_SGR, (unsigned)r, (unsigned)g, (unsigned)b); +} +void esc_fcolor_fg_256(FILE *f, u32 n) { + if (!f) return; + fprintf(f, CSI "38;5;%u" ESC_SGR, (unsigned)n); +} +void esc_fcolor_bg_256(FILE *f, u32 n) { + if (!f) return; + fprintf(f, CSI "48;5;%u" ESC_SGR, (unsigned)n); +} + +void esc_fcursor_hide(FILE *f) { if (!f) return; fputs(CSI "?25l", f); } +void esc_fcursor_show(FILE *f) { if (!f) return; fputs(CSI "?25h", f); } +void esc_fcursor_save(FILE *f) { if (!f) return; fputs(CSI "s", f); } +void esc_fcursor_restore(FILE *f) { if (!f) return; fputs(CSI "u", f); } +void esc_fbell(FILE *f) { if (!f) return; fputc('\a', f); } +void esc_falt_screen_on(FILE *f) { if (!f) return; fputs(CSI "?1049h", f); } +void esc_falt_screen_off(FILE *f) { if (!f) return; fputs(CSI "?1049l", f); } +void esc_fbracketed_paste_on(FILE *f) { if (!f) return; fputs(CSI "?2004h", f); } +void esc_fbracketed_paste_off(FILE *f) { if (!f) return; fputs(CSI "?2004l", f); } +void esc_fkitty_on(FILE *f) { if (!f) return; fputs(CSI ">1u", f); } +void esc_fkitty_off(FILE *f) { if (!f) return; fputs(CSI ">0u", f); } + +void esc_cursor_up(u32 n) { esc_fcursor_up(stdout, n); } +void esc_cursor_down(u32 n) { esc_fcursor_down(stdout, n); } +void esc_cursor_left(u32 n) { esc_fcursor_left(stdout, n); } +void esc_cursor_right(u32 n) { esc_fcursor_right(stdout, n); } +void esc_cursor_line_next(u32 n) { esc_fcursor_line_next(stdout, n); } +void esc_cursor_previous(u32 n) { esc_fcursor_previous(stdout, n); } +void esc_cursor_position(u32 col, u32 row) { + esc_fcursor_position(stdout, col, row); +} +void esc_clear_screen(void) { esc_fclear_screen(stdout); } +void esc_clear_screen_start(void) { esc_fclear_screen_start(stdout); } +void esc_clear_screen_end(void) { esc_fclear_screen_end(stdout); } +void esc_clear_line(void) { esc_fclear_line(stdout); } +void esc_clear_line_start(void) { esc_fclear_line_start(stdout); } +void esc_clear_line_end(void) { esc_fclear_line_end(stdout); } +void esc_scroll_up(u32 n) { esc_fscroll_up(stdout, n); } +void esc_scroll_down(u32 n) { esc_fscroll_down(stdout, n); } + +void esc_color_fg_rgb(u32 r, u32 g, u32 b) { + esc_fcolor_fg_rgb(stdout, r, g, b); +} +void esc_color_bg_rgb(u32 r, u32 g, u32 b) { + esc_fcolor_bg_rgb(stdout, r, g, b); +} +void esc_color_fg_256(u32 n) { + esc_fcolor_fg_256(stdout, n); +} +void esc_color_bg_256(u32 n) { + esc_fcolor_bg_256(stdout, n); +} + +void esc_cursor_hide(void) { esc_fcursor_hide(stdout); } +void esc_cursor_show(void) { esc_fcursor_show(stdout); } +void esc_cursor_save(void) { esc_fcursor_save(stdout); } +void esc_cursor_restore(void) { esc_fcursor_restore(stdout); } +void esc_bell(void) { esc_fbell(stdout); } +void esc_alt_screen_on(void) { esc_falt_screen_on(stdout); } +void esc_alt_screen_off(void) { esc_falt_screen_off(stdout); } +void esc_bracketed_paste_on(void) { esc_fbracketed_paste_on(stdout); } +void esc_bracketed_paste_off(void) { esc_fbracketed_paste_off(stdout); } +void esc_kitty_on(void) { esc_fkitty_on(stdout); } +void esc_kitty_off(void) { esc_fkitty_off(stdout); } + +#endif // ESCAPE_IMPLEMENTATION
\ No newline at end of file |