Autres articles / Other articles

Manage the Tera Term terminal screen

published: 19 September 2020 / updated 20 September 2020

Lire cette page en français

 


ANSI sequences

The VT100 terminal emulated by the Tera Term application displays ASCII characters, Line break. But not that...

A VT100 terminal (and higher) accepts ANSI sequences. These sequences were popularized with the appearance of the VT100 terminal in 1978. This is the first attempt to provide a standardized display control format.

The Tera Term application which communicates with the ARDUINO card manages these sequences.

The particularity of these sequences is that they always start with the character ESC escape, 27 in decimal.

Beforehand, we need to define a word that will transform a number in decimal ASCII string:

\ convert int to char sequence and display it 
: .#c ( n ---) 
    base @ >r 
    decimal 
    0           \ convert to unsigned double integer 
    <# #s #>    \ convert to string 
    type        \ display string 
    r> base ! ; 

In an ANSI sequence, numeric values are always in text format and in decimal:

Then, we define a word that will send the ESC code to the terminal:

\ start escape sequence 
: .esc ( ---) 
    27 emit  ;  \ send ESC char 

Many ANSI sequences begin with ESC[. We therefore define a word for this sequence:

\ start escape[ sequence 
: .esc[ ( ---) 
    .esc 
    [char] [ emit  ;  \ send ESC[ char 

Positioning the cursor in XY

The positioning of the cursor is controlled by an ANSI sequence: ESC[yy;xxH

The xx and yy values are always strings. Example: ESC[20;6H will place the cursor in column 20, row 6. To put the cursor back in the upper corner left of the terminal screen, we will generate the sequence: ESC[0;0H

To manage a cursor positioning sequence, we will define the word at-xy:

\ set cursor at x y position on screen terminal 
: at-xy ( x y ---) 
    .esc[ 
    .#c             \ send y position 
    [char] ; emit 
    .#c             \ send x position 
    [char] H emit ; 

In FORTH, we position the cursor in the upper left corner of the VT100 terminal emulated by Tera Term: 0 0 at-xy

Note:the word at-xy is already defined in gForth and works identically.

Screen Clear

Screen clearing is performed with an ANSI sequence: ESC[2J

Definition of the word page to clear the screen:

\ clear entire screen 
: page ( ---) 
    .esc[ ." 2J" 
    0 0 at-xy ; 

Note: the word page is already defined in gForth and works identically.

Text coloring

On the VT100 terminal emulated by the Tera Term application, you can color the text with ANSI ESC[3cm sequences where 'c' is a color code:

0 constant black 
1 constant red 
2 constant green 
3 constant yellow 
4 constant blue 
5 constant magenta 
6 constant cyan 
7 constant white 

Coloring of the text background:

: background ( color ---) 
    .esc[ 
    [char] 4 emit 
    .#c 
    [char] m emit ; 

Text coloring:

: foreground ( color ---) 
    .esc[ 
    [char] 3 emit 
    .#c 
    [char] m emit ; 

Example: yellow background blue foreground .chars normal

Other attributes

Display in bold, underlined, negative and normal characters:

: bold  ( ---) 
    .esc[ ." 1m" ; 
 
: underline ( ---) 
    .esc[ ." 4m" ; 
 
: negative ( ---) 
    .esc[ ." 7m" ; 
 
: normal ( ---) 
    .esc[ ." 0m" ; 

The word negative inverts the background and character colors.

The word normal overrides all coloring and styling attributes.

Selecting an alternate character set

You can select a set of alternative characters:

: alternate-font ( --- ) 
    .esc 40 emit ." 0" ; 
 
: ascii-font ( --- ) 
    .esc 40 emit ." B" ; 
 
\ display loop all characters between 32..127 
: .chars ( ---) 
    128 32 - for 
        r@ 32 + emit 
    next ; 

Example: page .chars cr alternate-font .chars cr ascii-font