Terminal control

Terminal control sequences can of course be hardwired into PRINT statements, and this is a common thing in particular in programs converted from Commodore BASIC. This totally messes up the output of list, however.

Do not embed terminal control sequences into programs, and prefer converting Commodore BASIC programs to not do that either.

In other BASICs, one would issue PRINT CHR$(…); and in BBC BASIC there was a general‐purpose VDU statement. This dialect of BASIC combines and builds upon both of those ideas.

In general, embedded control characters for colours and cursor movements should be replaced by VDU$, CLS$, COLOUR$ and TAB$. Not only does this prevent listings from being messed up, but it also accounts for Unix terminal capability differences at runtime, something that hardwired control sequences do not do. Terminals differ amongst themselves in such fundamentals as what colour mechanisms they even support and how the Control Sequence Introducer (CSI) character is encoded.

CLS$ is not only capable of clearing the screen. It follows Sinclar SuperBASIC's CLS statement and takes a parameter that allows a program to clear the whole of, the beginning of, or the end of, the current line or the entire screen.

TAB$ is not like the old PRINT TAB, which comes from a world of line printers, emitting SPC and sometimes even newline to advance to the selected position. In the world of video terminals, TAB$ generates control sequences to position the cursor both horizontally and (with 2 arguments) vertically. It does not need to mess about thinking that the world is a line printer. (Ironically, some of the Home Computer Era BASICs could position the cursor directly, but still tried to pretend that a CRT Video Display Unit was fan‐fold tractor‐fed paper with a print‐head, nonetheless.)

COLOUR$, depending from the number of its arguments, takes either an 256‐colour palette index, or a 24‐bit RGB direct colour value. It knows various terminal types's colour mechanisms, and tries to pick the best way of doing indexed or direct colour on the terminal type set by the conventional environment variables.

VDU$ is a Swiss Army Knife, albeit not as extreme a one as BBC BASIC's. With 1 argument, it translates an 8‐bit single byte character set control code to whatever escape sequence (if any) encodes that control code on the terminal. In particular, VDU$(&H9B) is a useful way of decoding the initial CSI in the control sequences returned by GET$ for function keys and extended keys, because that CSI encoding can vary by terminal type too. With 2 arguments, it allows control of ECMA-48 ‘graphic renditions’ such as boldface, overline, strikethrough, italics, et al..

WTF8$ complements VDU$. The 1 argument VDU$ is only well defined (for reasons related to ECMA-48 7‐bit aliases for control codes) for control codes. It should not be given non‐control code codepoints. Conversely, WTF8$ always uses WTF-8, that being in its name, which is not necessarily how a terminal encodes control characters (for the aforementioned reasons). It should not be given control code codepoints when the goal is to PRINT them or to compare them to the results of GET$ or INKEY$.