history: document ANSI escape sequences used
[pforth] / fth / termio.fth
index ef6d19b..fa9682e 100644 (file)
@@ -1,88 +1,98 @@
-\ Terminal I/O\r
-\\r
-\ Requires an ANSI compatible terminal.\r
-\\r
-\ To get Windows computers to use ANSI mode in their DOS windows,\r
-\ Add this line to "C:\CONFIG.SYS" then reboot.\r
-\  \r
-\  device=c:\windows\command\ansi.sys\r
-\\r
-\ Author: Phil Burk\r
-\ Copyright 1988 Phil Burk\r
-\ Revised 2001 for pForth\r
-\r
-ANEW TASK-TERMIO.FTH\r
-decimal\r
-\r
-$ 08 constant ASCII_BACKSPACE\r
-$ 7F constant ASCII_DELETE\r
-$ 1B constant ASCII_ESCAPE\r
-$ 01 constant ASCII_CTRL_A\r
-$ 05 constant ASCII_CTRL_E\r
-$ 18 constant ASCII_CTRL_X\r
-\r
-\ ANSI Terminal Control\r
-: ESC[ ( send ESCAPE and [ )\r
-       ASCII_ESCAPE emit\r
-       ascii [ emit\r
-;\r
-\r
-: CLS ( -- , clear screen )\r
-       ESC[ ." 2J"\r
-;\r
-\r
-: TIO.BACKWARDS ( n -- , move cursor backwards )\r
-       ESC[\r
-       base @ >r decimal\r
-       0 .r\r
-       r> base !\r
-       ascii D emit\r
-;\r
-\r
-: TIO.FORWARDS ( n -- , move cursor forwards )\r
-       ESC[\r
-       base @ >r decimal\r
-       0 .r\r
-       r> base !\r
-       ascii C emit\r
-;\r
-\r
-: TIO.ERASE.EOL ( -- , erase to the end of the line )\r
-       ESC[\r
-       ascii K emit\r
-;\r
-\r
-\r
-: BELL ( -- , ring the terminal bell )\r
-       7 emit\r
-;\r
-\r
-: BACKSPACE ( -- , backspace action )\r
-       8 emit  space  8 emit\r
-;\r
-\r
-0 [IF] \ for testing\r
-\r
-: SHOWKEYS  ( -- , show keys pressed in hex )\r
-       BEGIN\r
-               key\r
-               dup .\r
-               ." , $ " dup .hex cr\r
-               ascii q =\r
-       UNTIL\r
-;\r
-\r
-: AZ ascii z 1+ ascii a DO i emit LOOP ;\r
-\r
-: TEST.BACK1\r
-       AZ 5 tio.backwards\r
-       1000 msec\r
-       tio.erase.eol\r
-;\r
-: TEST.BACK2\r
-       AZ 10 tio.backwards\r
-       1000 msec\r
-       ." 12345"\r
-       1000 msec\r
-;\r
-[THEN]\r
+\ Terminal I/O
+\
+\ Requires an ANSI compatible terminal.
+\
+\ To get Windows computers to use ANSI mode in their DOS windows,
+\ Add this line to "C:\CONFIG.SYS" then reboot.
+\
+\  device=c:\windows\command\ansi.sys
+\
+\ Author: Phil Burk
+\ Copyright 1988 Phil Burk
+\ Revised 2001 for pForth
+
+ANEW TASK-TERMIO.FTH
+decimal
+
+$ 08 constant ASCII_BACKSPACE
+$ 7F constant ASCII_DELETE
+$ 1B constant ASCII_ESCAPE
+$ 01 constant ASCII_CTRL_A
+$ 05 constant ASCII_CTRL_E
+$ 18 constant ASCII_CTRL_X
+
+\ ANSI arrow key sequences
+\ ESC [ 0x41 is UP
+\ ESC [ 0x42 is DOWN
+\ ESC [ 0x43 is RIGHT
+\ ESC [ 0x44 is LEFT
+
+\ ANSI terminal control
+\ ESC [ 2J is clear screen
+\ ESC [ {n} D is move left
+\ ESC [ {n} C is move right
+\ ESC [ K is erase to end of line
+
+: ESC[ ( send ESCAPE and [ )
+    ASCII_ESCAPE emit
+    ascii [ emit
+;
+
+: CLS ( -- , clear screen )
+    ESC[ ." 2J"
+;
+
+: TIO.BACKWARDS ( n -- , move cursor backwards )
+    ESC[
+    base @ >r decimal
+    0 .r
+    r> base !
+    ascii D emit
+;
+
+: TIO.FORWARDS ( n -- , move cursor forwards )
+    ESC[
+    base @ >r decimal
+    0 .r
+    r> base !
+    ascii C emit
+;
+
+: TIO.ERASE.EOL ( -- , erase to the end of the line )
+    ESC[
+    ascii K emit
+;
+
+: BELL ( -- , ring the terminal bell )
+    7 emit
+;
+
+: BACKSPACE ( -- , backspace action )
+    8 emit  space  8 emit
+;
+
+0 [IF] \ for testing
+
+: SHOWKEYS  ( -- , show keys pressed in hex )
+    BEGIN
+        key
+        dup .
+        ." , $ " dup .hex cr
+        ascii q =
+    UNTIL
+;
+
+: AZ ascii z 1+ ascii a DO i emit LOOP ;
+
+: TEST.BACK1
+    AZ 5 tio.backwards
+    1000 msec
+    tio.erase.eol
+;
+: TEST.BACK2
+    AZ 10 tio.backwards
+    1000 msec
+    ." 12345"
+    1000 msec
+;
+[THEN]