A colour reaches a terminal as well as the screen

The console's attribute has always meant something to the screen and
nothing to the serial line: its low nibble picks one of sixteen ink and
paper pairs, and only videoPutCell ever read it. So the fault screen's
red was red in the window and grey down the wire, and Examples/colours
printed " ordinary  highlighted " with nothing to tell them apart.

It is said in ANSI now, on the same terms the cursor is said in: a
register write only marks it and the next character sends it, so setting
a scheme and printing nothing says nothing, and setting the same scheme
twice costs one sequence rather than two. Only the scheme nibble crosses
- the page bits say which tiles a cell draws from, which is a fact about
the screen's own art.

THE ORDER WAS ALREADY RIGHT, which is worth saying because it looks like
a borrowing and is not. Both sets enumerate a three-bit colour, red green
blue counted in binary: one is red in both, three is yellow in both, six
is cyan in both. The same arithmetic done twice, forty years apart. The
one place they differ is slot 0, and that difference is forced - this
screen is ink on black, so ink cannot be black, and slot 0 is grey where
ANSI's is black.

Every sequence begins with a reset, so going from bank 8 to bank 1 does
not write red on the grey paper bank 8 left behind.

Scheme 0 is a bare reset rather than grey on black, and a terminal is
assumed to start plain - so a machine that never asks for a colour says
nothing at all, and one that does put the terminal back on its way out.
Two ways out, because the two endings have different rules: stopping on
purpose goes through stdio, since atexit runs BEFORE the buffer is
flushed and a reset written to the file descriptor would arrive in front
of the text it is meant to follow. Dying on a signal writes the four
bytes directly and accepts that the buffer may be lost.

colourTest walks all sixteen and then halts WITH ONE STILL SET, so the
recording shows the reset after the halt line rather than before it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
Anachronaut
2026-09-06 17:24:44 -04:00
co-authored by Claude Opus 5
parent 94bdf71356
commit b83ba5bf7a
11 changed files with 255 additions and 25 deletions
+107
View File
@@ -61,6 +61,51 @@ static void consoleReleaseTerminal(void) {
}
}
// ---- And giving back the colour along with the terminal ----
//
// A machine that stops with a scheme set would leave the shell that started it tinted, which
// is the same class of failure as leaving it with no echo: a state this program chose,
// outliving the program, with nothing to connect it back here.
//
// ---- Two of them, because the two endings have different rules ----
//
// A machine STOPPING ON PURPOSE has a buffer of output still in stdio, and atexit handlers
// run BEFORE that buffer is flushed - so a reset written straight to the file descriptor
// would arrive in front of the text it is meant to follow. That one goes through the same
// buffer as everything else and comes out in order.
//
// A machine DYING ON A SIGNAL may be inside stdio at the moment the signal arrived, so it
// writes the four bytes directly and accepts that whatever was buffered may be lost. The
// process is ending either way, and a terminal left tinted is the failure being prevented.
//
// A TERMINAL IS ASSUMED TO START PLAIN, which is why this begins at nought rather than at
// "nothing said yet". A machine that never colours anything then says nothing at all, and
// the alternative was every session opening with a reset nobody asked for - CosmOS sets the
// scheme to nought on its way to the prompt, so the first character would have announced it.
// If the assumption is ever wrong, the first colour puts it right: every sequence begins
// with a reset.
//
// Setting it back to nought here is what makes a machine that carries on - resumed after a
// stop - say its colour again on the next character rather than believing it already has.
static uint8_t attributeTold = 0;
static void consolePlainAgain(void) {
if (attributeTold == 0) {
return;
}
fputs("\033[0m", stdout);
attributeTold = 0;
}
static void consolePlainAgainDying(void) {
if (attributeTold == 0) {
return;
}
ssize_t wrote = write(STDOUT_FILENO, "\033[0m", 4);
(void)wrote;
attributeTold = 0;
}
static void consoleTakeTerminal(void) {
if (consoleTerminalRaw || !consoleTerminalSaved) {
return;
@@ -105,6 +150,7 @@ void consoleRestore(void) {
// is waiting sees the signal it expected rather than a machine that exited quietly.
static void consoleFatalSignal(int signalNumber) {
consoleReleaseTerminal();
consolePlainAgainDying();
signal(signalNumber, SIG_DFL);
raise(signalNumber);
}
@@ -116,6 +162,7 @@ static void consoleContinueSignal(int signalNumber);
// entitled to have its keys again when it is resumed.
static void consoleStopSignal(int signalNumber) {
consoleReleaseTerminal();
consolePlainAgainDying();
signal(SIGCONT, consoleContinueSignal);
signal(signalNumber, SIG_DFL);
raise(signalNumber);
@@ -302,6 +349,65 @@ static void consoleSayCursor(void) {
consoleTellTerminal(sequence);
}
// ---- Sixteen schemes, and what a terminal can make of them ----
//
// The attribute's low nibble picks one of sixteen ink and paper pairs. Banks 0 to 7 are ink
// on black; banks 8 to 15 are those same colours as PAPER with black ink, which is the
// machine's one-bit highlight - attribute XOR 8 turns any of them inside out. ANSI says both
// of those, so what is needed here is a table rather than a translation.
//
// AND THE ORDER WAS ALREADY RIGHT, which is worth saying because it looks like a borrowing
// and is not. Both sets are an enumeration of a three bit colour, red green blue counted in
// binary: one is red in both, three is yellow in both, six is cyan in both. The same
// arithmetic was done twice, forty years apart.
//
// The one place they differ is slot 0, and that difference is forced: this screen is ink on
// black, so ink cannot be black, and slot 0 is grey where ANSI's is black.
//
// EVERY SEQUENCE BEGINS WITH A RESET. Setting only a foreground would leave the paper from
// whatever bank came before it, so going from bank 8 to bank 1 would have written red on
// grey. One extra byte, and the sequences stop depending on each other.
//
// Scheme 0 is sent as a bare reset rather than as grey on black, because plain text on
// somebody's terminal means THEIR plain text. Forcing grey on black would fight a terminal
// set up the other way round, which is the kind of thing this console exists not to do.
static const char *const schemeSequences[16] = {
"\033[0m",
"\033[0;31m", "\033[0;32m", "\033[0;33m",
"\033[0;34m", "\033[0;35m", "\033[0;36m",
"\033[0;97m",
"\033[0;47;30m",
"\033[0;41;30m", "\033[0;42;30m", "\033[0;43;30m",
"\033[0;44;30m", "\033[0;45;30m", "\033[0;46;30m",
"\033[0;107;30m",
};
// ---- Told once, and only when it matters ----
//
// The same bargain the cursor gets, for the same reason: a register write only marks it and
// the next character sends it. A program that sets a scheme and prints nothing has said
// nothing, and one that sets the same scheme twice costs one sequence rather than two.
//
// Only the SCHEME nibble is looked at. The page bits say which tiles a cell draws from,
// which is a fact about the screen's own art and means nothing to a terminal.
static void consoleSayAttribute(void) {
const uint8_t scheme = consoleAttribute & VIDEO_ATTRIBUTE_SCHEME;
if (scheme == attributeTold) {
return;
}
// Installed on the first colour rather than at startup, so a run that never asks for one
// registers nothing at all - the same bargain the terminal guards make.
if (scheme != 0) {
static int plainGuardInstalled = 0;
if (!plainGuardInstalled) {
plainGuardInstalled = 1;
atexit(consolePlainAgain);
}
}
attributeTold = scheme;
consoleTellTerminal(schemeSequences[scheme]);
}
static void consoleDraw(uint8_t byte) {
// ---- Nowhere to put a glyph ----
//
@@ -1551,6 +1657,7 @@ uint8_t OutputHandler(uint8_t DataByte, uint8_t Address) {
// since it was moved. Here rather than at the move, so setting a row and a
// column costs one sequence rather than two.
consoleSayCursor();
consoleSayAttribute();
consoleDraw(DataByte);
putchar(DataByte);
break;