aboutsummaryrefslogtreecommitdiffstats
path: root/usr/ansi.c
blob: 0e73ff822149c66c39fa48a56ede5f0fe49e0eb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "gbos.h"
/* ansi - show the terminal's 7-color ANSI support. Also stress-tests per-glyph
 * coloring: adjacent characters (which share an 8x8 tile) get different colors.
 *
 * NB: no initialized statics / pointer arrays here - SDCC would place them in
 * the _INITIALIZED area at $A000 (the RAM window), overflowing the 16K image. */

static void sgr(unsigned char n) {   /* emit ESC [ n m */
    putc(27);
    putc('[');
    putu(n);
    putc('m');
}

void main(void) {
    unsigned char i, c;
    const char *s;

    puts("ANSI 7-color test"); nl();

    /* each color name printed in its own color (31..37) */
    sgr(31); puts("red ");
    sgr(32); puts("green ");
    sgr(33); puts("yellow ");
    sgr(34); puts("blue ");
    sgr(35); puts("magenta ");
    sgr(36); puts("cyan ");
    sgr(37); puts("white");
    sgr(0); nl();

    /* per-glyph stress: 40 bars, color changes every single character, so the
     * left/right halves of one tile differ - impossible with a per-tile palette */
    c = 31;
    for (i = 0; i < 40; i++) {
        sgr(c);
        putc('#');
        if (++c > 37) c = 31;
    }
    sgr(0); nl();

    /* one word, one color per letter */
    c = 31;
    s = "RAINBOW!";
    for (i = 0; s[i]; i++) {
        sgr(c);
        putc(s[i]);
        if (++c > 37) c = 31;
    }
    sgr(0); nl();

    /* background colors: a block of each (SGR 41..47), default white text */
    puts("backgrounds:"); nl();
    c = 41;
    for (i = 0; i < 7; i++) {
        putc(27); putc('['); putc('4'); putc((char)('0' + (c - 40))); putc('m');
        puts(" bg ");
        if (++c > 47) c = 41;
    }
    sgr(0); nl();

    /* foreground-on-background combos (one CSI carries both: ESC[fg;bg m) */
    puts("\033[37;41m wht/red \033[33;44m yel/blu \033[34;43m blu/yel "
         "\033[32;45m grn/mag \033[36;41m cyn/red \033[0m"); nl();

    /* per-glyph fg over a shared bg: adjacent letters differ, bg stays green */
    puts("\033[42m\033[31mR\033[33mA\033[34mI\033[35mN\033[37mBOW\033[0m"); nl();
}