REBOL Forces

Console

Author: REBOL Technologies
Source: extracted from notes.html for REBOL/core v2.2 release

Contents

The REBOL command line console has been completely redesigned for 2.2. The new console now provides "virtual terminal" capability that allows you to perform operations such as cursor movement, cursor addressing, line editing, screen clearing, control key input, and cursor position querying. In addition, on platforms like Windows the console style will be more standard in its appearance and will operate several times faster. The control sequences follow the ANSI standard. These features provide you with all the capability you need to write your own platform-independent terminal programs such as text editors, email clients, or Telnet emulators.

The new console features apply to both input and output. On input, special control keys will be converted to multiple-character escape sequences. On output, multiple-character escape sequences can be used to control the display of text in the console window. Both the input and output sequences begin with an escape character, 27 decimal (1B hex). The next character in the sequence indicates the control keys on input or the terminal control operation on output.

Note that the characters are case-sensitive requiring an upper case character

Keyboard Input Sequences

The special keys and second character in the sequence are included in the following table:

KEY    ^(1B)[
 
UP A
DOWN B
RIGHT C
LEFT D
HOME 1~
INSERT 2~
END 4~
PG UP 5~
PG DN 6~

Terminal Output Sequences

There are several variations in the terminal control output character sequences. Some command codes are preceded by a count sent in ASCII form indicating that the operation is to be performed the specified number of times. The cursor motion command may be preceded by two numbers separated by a semicolon to indicate the row and column position to move to. The cursor command characters (upper case required) are included in the following table:

^(1B)[     Use this escape code prior to the following codes
 
D Moves cursor one space left
C Moves cursor one space right
A Moves cursor one space up
B Moves cursor one space down
n D Moves cursor n spaces left
n C Moves cursor n spaces right
n A Moves cursor n spaces up
n B Moves cursor n spaces down
r ; c H Moves cursor to row r, column c*
H Moves cursor to top left corner (home)*
P Deletes one character to the right at current location
n P Deletes n characters to the right at current location
@ Inserts one blank space at current location
n @ Inserts n blank spaces at current location
J Clears screen and moves cursor to top left corner (home)*
K Clears from current position to end of current line
6n Places the current cursor position in the input buffer
7n Places screen dimensions in the input buffer
 
* Top left corner is defined as row 1, column 1

Examples

Moving cursor to the right ten spaces

print "^(1B)[10CHi!"
Hi!

Moving cursor to the left seven spaces and clearing the remainder of the line

cursor: func [parm [string!]][join "^(1B)[" parm]
print ["How are you" cursor "7D" cursor "K"]
How a

Finding current console dimensions:

cons: open/binary [scheme: 'console]
print cursor "7n" screen-dimensions: next next to-string copy cons
"33;105R"
close cons

The example opens the console, sends a control character to the input buffer and copies the return value, then reads the value (screen-dimensions) that is returned after (next) the control character. It then closes the console. The return is the height and width separated by a semicolon (;) and followed by an R: 33 high x 105 wide. Your console screen dimensions will vary.

Note: Printing a character to the bottom-right corner of some terminals will cause a new line, which will scroll the screen. Others will not. This inconsistency between console terminal types must be considered when writing REBOL scripts intended to be cross-platform.