diff options
Diffstat (limited to 'include/escape.h')
| -rwxr-xr-x | include/escape.h | 209 |
1 files changed, 136 insertions, 73 deletions
diff --git a/include/escape.h b/include/escape.h index 13bd996..db46694 100755 --- a/include/escape.h +++ b/include/escape.h @@ -1,91 +1,110 @@ // 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 <stdint.h> - +#include "type.h" +#include <stdio.h> #ifdef __cplusplus extern "C" { #endif +#define CSI "\x1B[" -#define CSI "\x1B[" - - -#define ESC_CURSOR_UP( n ) CSI #n "A" -void esc_cursor_up( uint32_t n ); +#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( uint32_t 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 "C" -void esc_cursor_left( uint32_t 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 "D" -void esc_cursor_right( uint32_t 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( uint32_t 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_POSITION( x, y ) CSI #y ";" #x "H" -void esc_cursor_position( uint32_t x, uint32_t y ); +#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_CLEAR_SCREEN CSI "2J" -void esc_clear_screen( void ); +#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_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_clear_screen_end(void); +void esc_fclear_screen_end(FILE *); #define ESC_CLEAR_LINE CSI "2K" -void esc_clear_line( void ); +void esc_clear_line(void); +void esc_fclear_line(FILE *); -#define ESC_CLEAR_LINE_START CSI "1L" -void esc_clear_line_start( void ); +#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 "0L" -void esc_clear_line_end( void ); +#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( uint32_t n ); +#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( uint32_t 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_SGR "m" -#define ESC_RESET CSI "0" ESC_SGR -#define ESC_BOLD CSI "1" 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_RESET CSI "0" ESC_SGR +#define ESC_BOLD CSI "1" 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( uint32_t r, uint32_t g, uint32_t b ); +#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( uint32_t r, uint32_t g, uint32_t 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( uint32_t r, uint32_t g, uint32_t 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( uint32_t r, uint32_t g, uint32_t b ); - -#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 +#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 #ifdef __cplusplus } @@ -93,29 +112,73 @@ void esc_color_bg_256( uint32_t r, uint32_t g, uint32_t b ); #endif // ESCAPE_H - #ifdef ESCAPE_IMPLEMENTATION #include <stdio.h> -void esc_cursor_up( uint32_t n ) { fprintf( stdout, CSI "%dA", n ); } -void esc_cursor_down( uint32_t n ) { fprintf( stdout, CSI "%dB", n ); } -void esc_cursor_left( uint32_t n ) { fprintf( stdout, CSI "%dC", n ); } -void esc_cursor_right( uint32_t n ) { fprintf( stdout, CSI "%dD", n ); } -void esc_cursor_line_next( uint32_t n ) { fprintf( stdout, CSI "%dE", n ); } -void esc_cursor_position( uint32_t x, uint32_t y ) { fprintf( stdout, CSI "%d;%dH", y, x ); } -void esc_clear_screen( void ) { fputs( CSI "2J", stdout ); } -void esc_clear_screen_start( void ) { fputs( CSI "1J", stdout ); } -void esc_clear_screen_end( void ) { fputs( CSI "0J", stdout ); } -void esc_clear_line( void ) { fputs( CSI "2L", stdout ); } -void esc_clear_line_start( void ) { fputs( CSI "1L", stdout ); } -void esc_clear_line_end( void ) { fputs( CSI "0L", stdout ); } -void esc_scroll_up( uint32_t n ) { fprintf( stdout, "%dS", n ); } -void esc_scroll_down( uint32_t n ) { fprintf( stdout, "%dT", n ); } +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_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_color_fg_rgb( uint32_t r, uint32_t g, uint32_t b ) { fprintf( stdout, CSI "38;2;%d;%d;%d" ESC_SGR, r, g, b ); } -void esc_color_bg_rgb( uint32_t r, uint32_t g, uint32_t b ) { fprintf( stdout, CSI "48;2;%d;%d;%d" ESC_SGR, r, g, b ); } -void esc_color_fg_256( uint32_t r, uint32_t g, uint32_t b ) { fprintf( stdout, CSI "38;5;%d;%d;%d" ESC_SGR, r, g, b ); } -void esc_color_bg_256( uint32_t r, uint32_t g, uint32_t b ) { fprintf( stdout, CSI "48;5;%d;%d;%d" ESC_SGR, r, g, b ); } +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_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_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); +} #endif // ESCAPE_IMPLEMENTATION
\ No newline at end of file |