diff --git a/inc/format.h b/inc/format.h new file mode 100644 index 0000000..77bbc6f --- /dev/null +++ b/inc/format.h @@ -0,0 +1,94 @@ +/* ------------------------------------------------------------------ + * -- project: SCAN + * -- description: abstraction for terminal control sequences + * ------------------------------------------------------------------ + */ + +#ifndef __FORMAT_H +#define __FORMAT_H + +/* -- type definitions */ +typedef struct { + unsigned char black; + unsigned char red; + unsigned char green; + unsigned char yellow; + unsigned char blue; + unsigned char magenta; + unsigned char cyan; + unsigned char white; + unsigned char extended; + unsigned char normal; +} colours_t; + +typedef struct { + unsigned char bold; + unsigned char dim; + unsigned char italic; + unsigned char underlined; + unsigned char blinking; + unsigned char reversed; + unsigned char invisible; + unsigned char strikethrough; +} style_t; + + +/* -- constant definitions */ +const char* ESCAPE = "\x1b"; +const char* SEQUENCE_PREFIX = "\x1b["; +const char* SEQUENCE_SUFFIX = "m"; + +const style_t STYLE_SET = { + .bold = 1u, + .dim = 2u, + .italic = 3u, + .underlined = 4u, + .blinking = 5u, + .reversed = 7u, + .invisible = 8u, + .strikethrough = 9u, +}; + +const style_t STYLE_RESET = { + .bold = 22u, + .dim = 22u, + .italic = 23u, + .underlined = 24u, + .blinking = 25u, + .reversed = 27u, + .invisible = 28u, + .strikethrough = 29u, +}; + +const colours_t COLOUR_FG = { + .black = 30u, + .red = 31u, + .green = 32u, + .yellow = 33u, + .blue = 34u, + .magenta = 35u, + .cyan = 36u, + .white = 37u, + .extended = 38u, + .normal = 39u, +}; + +const colours_t COLOUR_BG = { + .black = 40u, + .red = 41u, + .green = 42u, + .yellow = 43u, + .blue = 44u, + .magenta = 45u, + .cyan = 46u, + .white = 47u, + .extended = 48u, + .normal = 49u, +}; + +/* -- function prototypes */ +void format_reset( void ); +void format_set_style( unsigned char style ); +void format_reset_style( unsigned char style ); + +#endif diff --git a/src/format.c b/src/format.c new file mode 100644 index 0000000..68775c3 --- /dev/null +++ b/src/format.c @@ -0,0 +1,19 @@ +#include +#include "format.h" + +void format_reset( void ) +{ + printf( "%s0%s", SEQUENCE_PREFIX, SEQUENCE_SUFFIX ); +} + +void format_set_style( unsigned char style ) +{ + if ( COLOUR_FG.extended == style ) return; + if ( COLOUR_BG.extended == style ) return; + + printf( "%s%d%s", SEQUENCE_PREFIX, style, SEQUENCE_SUFFIX ); +} + +// void format_reset_style( unsigned char style ) +// { +// }