Terminal Support

This document describes extensions to Basic Text adding ANSI-style terminal features. It is experimental.

Terminal Output

Terminal Output uses Basic Text's "strict" conversions.

Output Feature Sets

Terminal output features are grouped into sets, which can be supported independently or in combination:

Line-oriented output

This feature adds line-oriented editing features.

The following control codes are recognized:

CodeMeaning
U+7Alert
U+8Move cursor back one column
U+9Tab
U+AEnd of line
U+CFF Terminal Compatibility
U+DCarriage Return
U+7FNo Effect

The following escape sequences are recognized:

SequenceMeaning
␛[KClear to end of line
␛[0KClear to end of line
␛[2KClear entire line
Alert

Produce an acoustic indication or a visual indication if possible, without modifying the state of the terminal.

Move cursor back one column

Move the cursor back one column, but not past the first column.

Tab

Move the cursor back one column, but not past the first column.

End of line

Move the cursor to the beginning of the next line, scrolling the output if needed.

FF Terminal Compatibility

Move the cursor to the next line without changing the column.

Carriage Return

Move the cursor to the first column of the current line.

No Effect

Leave the state of the terminal unmodified.

Clear to end of line

Clear to the end of the line, leaving the cursor where it is.

Clear entire line

Clear the entire line, and move the cursor to the first column.

Full-screen output

This feature set adds a "full screen" mode which may be enabled at runtime, which supports two-dimensional cursor positioning, scrolling, screen clearing, and related features.

In the default mode, the following escape sequences are recognized:

SequenceMeaningNotes
␛[?1049hEnter full-screen mode, with a clear screen and default settings

The following escape sequences are recognized within full-screen mode:

SequenceMeaningNotes
␛7save_cursorTODO: Do we need this?
␛8restore_cursorDitto
␛Hset_tabDitto
␛Mscroll_reverseTODO: Is this different on Windows?
␛[«n»@parm_ich(«n»)«n» may be omitted and defaults to 1
␛[«n»Aparm_up_cursor(«n»)Ditto
␛[«n»Bparm_down_cursor(«n»)Ditto
␛[«n»Cparm_right_cursor(«n»)Ditto
␛[«n»Dparm_left_cursor(«n»)Ditto
␛[«n»Gcolumn_address(«n»)Ditto
␛[«line»;«column»Hcursor_address(«row», «column»)«line»;«column» may be omitted and default to 1;1
␛[«n»Itab(«n»)«n» may be omitted and defaults to 1
␛[0Jclr_eosThe 0 is optional
␛[1JClear the screen from the beginning to the current cursor position
␛[2JClear the screenUnlike clear_screen, this doesn't change the cursor position
␛[«n»Linsert_line(«n»)«n» may be omitted and defaults to 1
␛[«n»Mparm_delete_line(«n»)Ditto
␛[«n»Pparm_dch(«n»)Ditto
␛[«n»Sparm_index(«n»)Ditto
␛[«n»Tparm_rindex(«n»)Ditto
␛[«n»Xerase_chars(«n»)Ditto
␛[«n»Zcbt(«n»)Ditto
␛[«n»drow_address(«n»)Ditto
␛[«line»;«column»fSame as the similar sequence ending in H
␛[3gclear_all_tabsTODO: do we need this?
␛[?25hcursor_visible
␛[?1049hClear the screen and reset full-screen settings to defaults
␛[?2004hEnable bracketed paste mode
␛[?25lcursor_invisible
␛[?1049lExit full-screen mode and restore the terminal to its prior state
␛[?2004lDisable bracketed paste mode
␛[!pReset the terminal to default settings, without clearing the screen
␛[«top»;«bottom»rchange_scroll_region(«top», «bottom»)«top»;«bottom» may be omitted and default to 1;«viewpoint-height»

TODO: Describe the behavior on on the rightmost column and bottom-most line, and other traditionally underspecified things.

TODO: Describe parameters in more detail, including the syntax for numeric and string parameters, and min/max valid values for numeric parameters.

Color

This feature set adds color and display attributes such as bold, underline, and italics.

This feature defines the following escape sequences:

SequenceMeaningNotes
␛[…mset_attributes(…)Set text attributes; see below for the meaning of
␛[38;2;«r»;«g»;«b»mSet foreground color to RGB «r», «g», «b»Values are from 0-255
␛[48;2;«r»;«g»;«b»mSet background color to RGB «r», «g», «b»Ditto

In the form above, the may be replaced by up to 16 ;-separated sequences from the following:

SequenceMeaningNotes
0Normal (default)
1Bold
2FaintFaint may not appear visually distinct on some platforms
4UnderlinedMay be "simulated with color". Applications may wish to use U+332 instead.
7Inverse
22Not bold or faint
23Not italicized
24Not underlined (any kind)
27Not inverse
29Not crossed-out
30Foreground Black
31Foreground Red
32Foreground Green
33Foreground YellowMay appear brown on some platforms
34Foreground Blue
35Foreground Magenta
36Foreground Cyan
37Foreground White
39Foreground default
40Background Black
41Background Red
42Background Green
43Background Yellow
44Background Blue
45Background Magenta
46Background Cyan
47Background White
49Background default
90Foreground bright BlackBright colors may not appear visually distinct on some platforms
91Foreground bright Red
92Foreground bright Green
93Foreground bright Yellow
94Foreground bright Blue
95Foreground bright Magenta
96Foreground bright Cyan
97Foreground bright White
100Background bright Black
101Background bright Red
102Background bright Green
103Background bright Yellow
104Background bright Blue
105Background bright Magenta
106Background bright Cyan
107Background bright White

Not all terminal support all colors; when a requested color is unavailable, terminals may substitute the closest available color.

Custom Title

This feature set adds the ability to set a custom window title.

This feature defines the following escape sequences:

SequenceMeaningNotes
␛]0;«string»␇Sets the terminal's title to «string»Implementations may implicitly add a prefix and/or truncate the string
␛]2;«string»␇Sets the terminal's title to «string»Ditto

Terminal input

Terminal Input uses Basic Text's normal (not "strict") conversions.

Most keys have obvious mappings to Unicode scalar value sequences. This section describes mapping for special keys read from a terminal.

Three modifiers are recognized: Ctrl, Alt, and Shift. In environments with Meta keys, Meta is mapped to Alt.

Terminal input control codes

The following control codes are recognized:

CodeMeaningNotes
U+0Ctrl-Space
U+8Ctrl-HDespite U+8 being historically called "backspace" in ASCII, it isn't the backspace key
U+9Tab
U+AEnterU+A means "end of line"
U+CCtrl-LThis is only transmitted in immediate mode, and requests applications refresh the screen
U+11Ctrl-QWhen enabled in the terminal input mode
U+13Ctrl-SWhen enabled in the terminal input mode
U+1BEscapeWhen read in in immediate input mode
U+1CCtrl-\When enabled in the terminal input mode
U+1DCtrl-]
U+1ECtrl-^
U+1FCtrl-_
U+7FBackspaceThis is the backspace key

The following control codes are interpreted by the implementation and not passed on to applications:

CodeCommonly typed asBehavior
U+3Ctrl-CTerminate the program, when not enabled in the terminal input mode
U+9TabNo effect when modifiers include Alt
U+DCtrl-MSend U+A to the program, when read in a single input call in immediate input mode
U+11Ctrl-QNo effect when not enabled in the terminal input mode
U+13Ctrl-SNo effect when not enabled in the terminal input mode
U+1ACtrl-ZSuspend the program
U+1CCtrl-\Terminate the program, when not enabled in the terminal input mode
U+60`No effect when modifiers include Alt

Except as specified otherwise above, U+1 through U+1A are recognized as Ctrl-A through Ctrl-Z, respectively.

Codes with values U+0 through U+7F, except for U+5B ([) and U+5D (]), may be preceded by U+1B indicating the Alt modifier.

When a program is resumed from being suspended, any streams open in immediate input mode are passed a U+C (Ctrl-L). Applications are encouraged to interpret Ctrl-L as a command to redraw the screen.

Terminal input escape sequences

The following escape sequences are recognized when they are read as a single input call in immediate input mode:

SequenceMeaningNotes
␛[AUp
␛[BDown
␛[CRight
␛[DLeft
␛[FEnd
␛[HHome
␛[1«m»AUpSame as above, but with modifiers
␛[1«m»BDownDitto
␛[1«m»CRightDitto
␛[1«m»DLeftDitto
␛[ZShift-Tab
␛[1«m»~HomeSame as above, but with modifiers
␛[2«m?»~Insert
␛[3«m?»~Delete
␛[4«m»~EndSame as above, but with modifiers
␛[5«m?»~Page Up
␛[6«m?»~Page Down
␛[11«m?»~F1These use the "old xterm"/CSI values, rather than vt102/vt220/SS3/Windows values
␛[12«m?»~F2
␛[13«m?»~F3
␛[14«m?»~F4
␛[15«m?»~F5
␛[17«m?»~F6(yes, 16 really is skipped)
␛[18«m?»~F7
␛[19«m?»~F8
␛[20«m?»~F9
␛[21«m?»~F10
␛[23«m?»~F11(yes, 22 really is skipped)
␛[24«m?»~F12
␛[200«m?»~Begin PasteOnly emitted when bracketed paste mode is activated
␛[201«m?»~End PasteDitto

«m» is a modifier sequence:

SequenceShiftAltCtrl
;2
;3
;4
;5
;6
;7
;8

and «m?» is an optional modifier sequence.

In environments with keys F13 through F24, they are mapped to F1 through F12 with the shift modifier.

As special cases, Delete, Insert, Home, End, Page Up and Down, and F1 and F12 with the Ctrl-Alt or Ctrl-Alt-Shift modifiers are reserved and not passed on to the application.

Input Modes

The following options are added to the options for Basic Text:

NameTypeApplicability
Immediate modeBooleanInput
Hidden modeBooleanInput

Immediate mode

In Immediate input mode, each keypress is treated as if it were followed by U+A and immediately sent to the application without the extra U+A. And as a special case, U+C (FF) is not replaced in immediate mode.

Hidden mode

In Hidden input mode, terminal implementations should not echo input characters back to the terminal.