Use 4 spaces for indentation.
[debian/pforth] / fth / termio.fth
1 \ Terminal I/O
2 \
3 \ Requires an ANSI compatible terminal.
4 \
5 \ To get Windows computers to use ANSI mode in their DOS windows,
6 \ Add this line to "C:\CONFIG.SYS" then reboot.
7 \
8 \  device=c:\windows\command\ansi.sys
9 \
10 \ Author: Phil Burk
11 \ Copyright 1988 Phil Burk
12 \ Revised 2001 for pForth
13
14 ANEW TASK-TERMIO.FTH
15 decimal
16
17 $ 08 constant ASCII_BACKSPACE
18 $ 7F constant ASCII_DELETE
19 $ 1B constant ASCII_ESCAPE
20 $ 01 constant ASCII_CTRL_A
21 $ 05 constant ASCII_CTRL_E
22 $ 18 constant ASCII_CTRL_X
23
24 \ ANSI Terminal Control
25 : ESC[ ( send ESCAPE and [ )
26     ASCII_ESCAPE emit
27     ascii [ emit
28 ;
29
30 : CLS ( -- , clear screen )
31     ESC[ ." 2J"
32 ;
33
34 : TIO.BACKWARDS ( n -- , move cursor backwards )
35     ESC[
36     base @ >r decimal
37     0 .r
38     r> base !
39     ascii D emit
40 ;
41
42 : TIO.FORWARDS ( n -- , move cursor forwards )
43     ESC[
44     base @ >r decimal
45     0 .r
46     r> base !
47     ascii C emit
48 ;
49
50 : TIO.ERASE.EOL ( -- , erase to the end of the line )
51     ESC[
52     ascii K emit
53 ;
54
55
56 : BELL ( -- , ring the terminal bell )
57     7 emit
58 ;
59
60 : BACKSPACE ( -- , backspace action )
61     8 emit  space  8 emit
62 ;
63
64 0 [IF] \ for testing
65
66 : SHOWKEYS  ( -- , show keys pressed in hex )
67     BEGIN
68         key
69         dup .
70         ." , $ " dup .hex cr
71         ascii q =
72     UNTIL
73 ;
74
75 : AZ ascii z 1+ ascii a DO i emit LOOP ;
76
77 : TEST.BACK1
78     AZ 5 tio.backwards
79     1000 msec
80     tio.erase.eol
81 ;
82 : TEST.BACK2
83     AZ 10 tio.backwards
84     1000 msec
85     ." 12345"
86     1000 msec
87 ;
88 [THEN]