// ANSI Escape Sequence Test // -- written by a Spin programmer because a smug C programmer couldn't be bothered // -- translated from Quick Byte demo, originally written in Spin2 for the P2 // * this is a work-alike translation; many aspects of Spin differ from C // * https://www.parallax.com/colorful-terminal-output-with-ansi-escape-sequences #include #include #include "ansi_escapes.h" char Arrows[] = ">> "; char PressKey[] = "\rPress a key..."; void clear_screen(int crsr); void show_menu(); void fgnd_colors(); void fgnd_by_index(); void bgnd_colors(); void bgnd_by_index(); char wait_for_key(char *prompt); void main() { char k; while (true) { show_menu(); k = wait_for_key(Arrows); switch (k) { case '1': fgnd_colors(); break; case '2': fgnd_by_index(); break; case '3': bgnd_colors(); break; case '4': bgnd_by_index(); break; } } } void clear_screen(int crsr) { if (crsr) printf(CURSOR_ON); else printf(CURSOR_OFF); printf(HOME); printf(CLEAR_SCR); } void show_menu() { char k; printf(NORMAL); clear_screen(0); // Note: FOREGROUND does not work with BR_ colors; must use BR_FOREGROUND printf(BR_FOREGROUND, CYAN); printf("ANSI Output Demo%s\n", NORMAL); printf("%s[1]%s Foreground Colors by Name \r", REVERSE, NORMAL); printf("%s[2]%s Foreground Colors by Index\r", REVERSE, NORMAL); printf("%s[3]%s Background Colors by Name \r", REVERSE, NORMAL); printf("%s[4]%s Background Colors by Index\r\r", REVERSE, NORMAL); } void fgnd_colors() { printf(NORMAL); clear_screen(0); printf("ANSI Color Names (foreground)\r\r"); printf(FOREGROUND, BLACK); printf("BLACK\r"); printf(FOREGROUND, RED); printf("RED\r"); printf(FOREGROUND, GREEN); printf("GREEN\r"); printf(FOREGROUND, YELLOW); printf("YELLOW\r"); printf(FOREGROUND, BLUE); printf("BLUE\r"); printf(FOREGROUND, MAGENTA); printf("MAGENTA\r"); printf(FOREGROUND, CYAN); printf("CYAN\r"); printf(FOREGROUND, WHITE); printf("WHITE\r"); printf(BR_FOREGROUND, BLACK); printf("BLACK\r"); printf(BR_FOREGROUND, RED); printf("RED\r"); printf(BR_FOREGROUND, GREEN); printf("GREEN\r"); printf(BR_FOREGROUND, YELLOW); printf("YELLOW\r"); printf(BR_FOREGROUND, BLUE); printf("BLUE\r"); printf(BR_FOREGROUND, MAGENTA); printf("MAGENTA\r"); printf(BR_FOREGROUND, CYAN); printf("CYAN\r"); printf(BR_FOREGROUND, WHITE); printf("WHITE\r"); wait_for_key(PressKey); } void fgnd_by_index() { uint8_t x, y, idx; printf(NORMAL); clear_screen(0); printf("Extended ANSI Colors by Index (foreground)\r\r"); for (x = 0; x < 16; x++ ) { for (y = 0; y < 16; y++) { idx = (x << 4) + y; printf(EXT_FOREGROUND, idx); printf(" %3d", idx); } printf("\r"); } wait_for_key(PressKey); } void bgnd_colors() { printf(NORMAL); clear_screen(0); printf("ANSI Color Names (background)\r\r"); printf(FOREGROUND, WHITE); printf(BACKGROUND, BLACK); printf(" BLACK \r"); printf(FOREGROUND, BLACK); printf(BACKGROUND, RED); printf(" RED \r"); printf(BACKGROUND, GREEN); printf(" GREEN \r"); printf(BACKGROUND, YELLOW); printf(" YELLOW \r"); printf(BACKGROUND, BLUE); printf(" BLUE \r"); printf(BACKGROUND, MAGENTA); printf(" MAGENTA \r"); printf(BACKGROUND, CYAN); printf(" CYAN \r"); printf(BACKGROUND, WHITE); printf(" WHITE \r"); printf(FOREGROUND, WHITE); printf(BR_BACKGROUND, BLACK); printf(" BRIGHT BLACK \r"); printf(FOREGROUND, BLACK); printf(BR_BACKGROUND, RED); printf(" BRIGHT RED \r"); printf(BR_BACKGROUND, GREEN); printf(" BRIGHT GREEN \r"); printf(BR_BACKGROUND, YELLOW); printf(" BRIGHT YELLOW \r"); printf(BR_BACKGROUND, BLUE); printf(" BRIGHT BLUE \r"); printf(BR_BACKGROUND, MAGENTA); printf(" BRIGHT MAGENTA \r"); printf(BR_BACKGROUND, CYAN); printf(" BRIGHT CYAN \r"); printf(BR_BACKGROUND, WHITE); printf(" BRIGHT WHITE \r"); wait_for_key(PressKey); } void bgnd_by_index() { uint8_t x, y, idx; printf(NORMAL); clear_screen(0); printf("Extended ANSI Colors by Index (background)\r\r"); printf(FOREGROUND, BLACK); for (x = 0; x < 16; x++ ) { for (y = 0; y < 16; y++) { idx = (x << 4) + y; printf(EXT_BACKGROUND, idx); printf(" %3d ", idx); } printf("\r"); } wait_for_key(PressKey); } char wait_for_key(char *prompt) { printf(NORMAL); printf(prompt); return getchar(); }