/fth/termio.fth

https://github.com/philburk/pforth · Forth · 98 lines · 83 code · 15 blank · 0 comment · 2 complexity · 6348dbbab0df9a7b339531ec4d6a5d4a MD5 · raw file

  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. ANEW TASK-TERMIO.FTH
  14. decimal
  15. $ 08 constant ASCII_BACKSPACE
  16. $ 7F constant ASCII_DELETE
  17. $ 1B constant ASCII_ESCAPE
  18. $ 01 constant ASCII_CTRL_A
  19. $ 05 constant ASCII_CTRL_E
  20. $ 18 constant ASCII_CTRL_X
  21. \ ANSI arrow key sequences
  22. \ ESC [ 0x41 is UP
  23. \ ESC [ 0x42 is DOWN
  24. \ ESC [ 0x43 is RIGHT
  25. \ ESC [ 0x44 is LEFT
  26. \ ANSI terminal control
  27. \ ESC [ 2J is clear screen
  28. \ ESC [ {n} D is move left
  29. \ ESC [ {n} C is move right
  30. \ ESC [ K is erase to end of line
  31. : ESC[ ( send ESCAPE and [ )
  32. ASCII_ESCAPE emit
  33. ascii [ emit
  34. ;
  35. : CLS ( -- , clear screen )
  36. ESC[ ." 2J"
  37. ;
  38. : TIO.BACKWARDS ( n -- , move cursor backwards )
  39. ESC[
  40. base @ >r decimal
  41. 0 .r
  42. r> base !
  43. ascii D emit
  44. ;
  45. : TIO.FORWARDS ( n -- , move cursor forwards )
  46. ESC[
  47. base @ >r decimal
  48. 0 .r
  49. r> base !
  50. ascii C emit
  51. ;
  52. : TIO.ERASE.EOL ( -- , erase to the end of the line )
  53. ESC[
  54. ascii K emit
  55. ;
  56. : BELL ( -- , ring the terminal bell )
  57. 7 emit
  58. ;
  59. : BACKSPACE ( -- , backspace action )
  60. 8 emit space 8 emit
  61. ;
  62. 0 [IF] \ for testing
  63. : SHOWKEYS ( -- , show keys pressed in hex )
  64. BEGIN
  65. key
  66. dup .
  67. ." , $ " dup .hex cr
  68. ascii q =
  69. UNTIL
  70. ;
  71. : AZ ascii z 1+ ascii a DO i emit LOOP ;
  72. : TEST.BACK1
  73. AZ 5 tio.backwards
  74. 1000 msec
  75. tio.erase.eol
  76. ;
  77. : TEST.BACK2
  78. AZ 10 tio.backwards
  79. 1000 msec
  80. ." 12345"
  81. 1000 msec
  82. ;
  83. [THEN]