ansi-terminal
Provides helper words for ANSI compatible terminals.
ansi-terminal - An ANSI Terminal Library for Forth
This is a simple library for basic text formatting and the like in ANSI compliant terminals. It has support for:
- Setting text properties
- Foreground/Background colors
- Black
- Red
- Green
- Yellow
- Blue
- Magenta
- Cyan
- White
- Default
- Typeface Variants
- Bold
- Dim
- Italic
- Underline
- Blink
- Inverse
- Hidden
- Strikethrough
- Resetting text properties
- Foreground/Background colors
- Clearing the screen/line
- Full clear
- After cursor clear
- Before cursor clear
- Setting cursor visibility
- Setting cursor position
The library also defines the words at-xy and page using ANSI escape sequences.
Assumptions
There are three primary assumptions made:
- Support for single quote syntax to get character-codes (e.g.
'a'leaves the ASCII for the letter a on the stack.) - Support for
$syntax to leave hex values on the stack without changing thebasevalue. - There is no need to set the cursor position to greater than 99 in either dimension.
All of these assumptions could be corrected easily enough, but the code looks nicer, and is more minimal with these assumptions in place.
Definitions
Properties
Text
fg-blackfg-redfg-greenfg-yellowfg-bluefg-magentafg-cyanfg-whitefg-defaultbg-blackbg-redbg-greenbg-yellowbg-bluebg-magentabg-cyanbg-whitebg-defaultproperty-boldproperty-dimproperty-italicproperty-underlineproperty-blinkproperty-inverseproperty-hiddenproperty-strikethroughproperty-reset
Clear
clear-fullclear-before-cursorclear-after-cursor
Cursor
cursor-visiblecursor-invisible
Setters
property-setscreen-clearline-clearcursor-type-setcursor-position-setpageat-xy
Helper Words
escapestart-sequenceint-to-ascii
Examples
Setting Text Properties
To set the properties for the text being printed you need only put the property on the stack, and then let it be consumed by the property-set word.
fg-green property-set
bg-black property-set
property-bold property-set
To reset the properties of any text to be printed to the terminal's defaults, just use the property-reset property.
Setting the Cursor Visibility
To set the cursor visibility, just put the cursor property you are looking for on the stack, and have it consumed by the cursor-type-set word.
cursor-visible cursor-type-set
cursor-invisible cursor-type-set
Clearing the Display
If you just wish to clear the whole display and reset the cursor position use page. If you are looking for a more specialized clear however, you need to put the clear type on the stack, and then let it be consumed by either the line-clear or screen-clear word.
( Clears screen and re-homes cursor )
page
( Clears the line starting at the cursor position )
clear-after-cursor line-clear
( Clears the screen ending at the cursor position )
clear-before-cursor screen-clear
Setting the Cursor Position
To set the cursor position feed the x and y coordinates (as integers) into the at-xy word.
8 8 at-xy
Please note: My implementation of at-xy only supports up to the coordinates (99, 99). This is a hard limitation created by simplifications made in several underlying helper words.
by sparkingcircuit
Versions
Tags
publicdomain, unlicense, ansi-terminal
Dependencies
None
Dependents
None