Formatting TexT
The terminal application that you use to communicate with your Scamp computer is most likely compatible with the old VT100 terminals used in days of old, and therefore will support ANSI escape codes. By printing the right combination of characters, you can change text attributes on your screen. They start with an escape character, followed by defined codes. Ever wondered why your keyboard has an ESC key? It's an historical artefact dating back to the days of serial terminals and mainframe computers.
|
|
You can use your favourite search engine to find a complete list of ANSI escape codes.
Your Scamp computer has a few ANSI commands predefined. These are plain, bold, underline, wink (blinking text) and reverse.
Your Scamp computer has a few ANSI commands predefined. These are plain, bold, underline, wink (blinking text) and reverse.
Note that these words are used before text is printed. As an example:
: fancytext
." This is "
bold
." dramatic"
plain
." and "
underline
." important"
plain
." text. "
;
When fancytext is run, it will give an output of:
This is dramatic and important text.
Colors
There are two words that allow you to change text colors. Eight colors are supported by ANSI terminals. Place a value on the stack representing the color, then call fg to set foreground color or bg to set the background color.
You can use fg and bg with a number, or you can define the following constants to make it easier to use:
You can use fg and bg with a number, or you can define the following constants to make it easier to use:
0 constant black
1 constant red
2 constant green
3 constant yellow
4 constant blue
5 constant magenta
6 constant cyan
7 constant white
As an example of how to use colors (with the defined constants):
: alert
red bg
white fg
wink \ blink the text
cr ." Computer Malfunction!"
plain
;
Moving Around the Screen
The word goto will position the cursor at a particular co-ordinate on the screen. The upper left of the screen is position 1,1, known as home.
For example to move the cursor to column 2, row 6:
2 6 goto
The maximum horizontal and vertical size depends on how large the terminal window is on your host computer. Resizing your window changes the number of rows and columns. There is no simple way for Forth to query the terminal app to get this size, unfortunately. Be aware that if you go outside the available space, any text you print will simply wrap back to the onscreen area.
The word hide will hide the cursor, and show will make the cursor visible again. Hiding the cursor can be useful if you're redrawing parts of the screen and don't want to see the cursor jumping around.
The word hide will hide the cursor, and show will make the cursor visible again. Hiding the cursor can be useful if you're redrawing parts of the screen and don't want to see the cursor jumping around.
DEMO
A quick demo for Scamp3 and Scamp3e:
: star
random $ffff 8 u/ u/ fg \ pick a random color
42 emit \ print an asterisk
;
: jump
random $ffff 80 u/ u/ \ random x location (80 columns)
10 ms \ give random a chance to reseed
random $ffff 20 u/ u/ \ random y location (20 rows)
goto \ jump there
;
: pattern
cls \ clear the screen
hide \ hide the cursor
begin
jump \ jump to a random location
star \ print a colorful star
key? \ repeat until a key is typed
until
show \ restore the cursor
;