home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / screen.exp < prev    next >
Text File  |  1990-04-23  |  4KB  |  147 lines

  1. # QTAwk functions to manipulate display and cursor
  2. # adapted from Article in Computer Language, Vol. 7, No. 1, Jan. 1990
  3. # "Awk of Ages, Best for Me" by MIchael J. Gilchrist
  4. #
  5. # ANSI.SYS device driver for display must be installed for PC/MS-DOS
  6. #
  7. #  functions defined:
  8. #   cls() -- function to clear screen and home cursor
  9. #   cls_sv() -- function to clear screen and leave cursor in current position
  10. #   crsr_pos(row,col) -- function to move cursor to specified position
  11. #   crsr_up(n) -- function to move cursor up n row(s)
  12. #          will not go above top line
  13. #   crsr_dn(n) -- function to move cursor down n row(s)
  14. #          will not go below bottom line
  15. #   crsr_left(n) -- function to move cursor left n column(s)
  16. #            will not go beyond left edge
  17. #   crsr_right(n) -- function to move cursor right n column(s)
  18. #             will not go beyond right edge
  19. #   erase_end() -- function to erase to end of current line
  20. #           including cursor position
  21. #   set_color(attr,fore,back) -- function to set display colors
  22. #
  23. # define some command variables here
  24. BEGIN {
  25.       # define standard error file == screen
  26.     stderr = "stderr";
  27.     stdin  = "stdin";
  28.     stdout = "stdout";
  29.     srand();
  30.     fprintf(stderr,"\x01b[s");
  31.     fprintf(stderr,"\x01b[=7l");
  32.     for ( i = 1 ; i <= 100 ; i++ ) {
  33.     crsr_pos(randint(24),randint(79));
  34.     set_color(0,randint(7),randint(7));
  35.     fprintf(stderr,"\001");
  36.     }
  37.     fprintf(stderr,"\x01b[=7h");
  38.     fprintf(stderr,"\x01b[u");
  39.     fprintf(stderr,"<-------------- Exiting QTAwk ------------->");
  40.     crsr_pos(24,1);
  41.     set_color(1,1,4);
  42. }
  43.  
  44. # function to clear screen and home cursor
  45. function cls() {
  46.       # clear screen and home cursor string
  47.     local _cls_ = "\x01b[2J";
  48.  
  49.     fprintf(stderr,_cls_);
  50. }
  51.  
  52. # function to clear screen and leave cursor in current position
  53. function cls_sv() {
  54.       # clear screen and home cursor string
  55.     local _cls_ = "\x01b[2J";
  56.       # save cursor position
  57.     local _crsr_sv_ = "\x01b[s";
  58.       # restore cursor position
  59.     local _crsr_rs_ = "\x01b[u";
  60.  
  61.     fprintf(stderr,_crsr_sv_ ∩ _cls_ ∩ crsr_rs_);
  62. }
  63.  
  64. # function to move cursor to specified position
  65. function crsr_pos(row,col) {
  66.       # sets cursor position
  67.     local _crsr_pos_ = "\x01b[{irow};{icol}f";
  68.     local irow = int(row);  # insure integer value
  69.     local icol = int(col);  # insure integer value
  70.  
  71.     fprintf(stderr,replace(_crsr_pos_));
  72. }
  73.  
  74. # function to move cursor up n row(s) - will not go above top line
  75. function crsr_up(n) {
  76.       # move cursor up one row
  77.     local _crsr_up_ = "\x01b[{nn}A";
  78.     local nn = int(n);
  79.  
  80.     fprintf(stderr,replace(_crsr_up_));
  81. }
  82.  
  83. # function to move cursor down n row(s) - will not go below bottom line
  84. function crsr_dn(n) {
  85.       # move cursor down one row
  86.     local _crsr_dn_ = "\x01b[{nn}B";
  87.     local nn = int(n);
  88.  
  89.     fprintf(stderr,replace(_crsr_dn_));
  90. }
  91.  
  92. # function to move cursor left n column(s) - will not go beyond left edge
  93. function crsr_left(n) {
  94.       # cursor left n column(s)
  95.     local _crsr_left_ = "\x01b[{nn}C";
  96.     local nn = int(n);
  97.  
  98.     fprintf(stderr,replace(_crsr_left_));
  99. }
  100.  
  101. # function to move cursor right n column(s) - will not go beyond right edge
  102. function crsr_right(n) {
  103.       # cursor left n column(s)
  104.     local _crsr_right_ = "\x01b[{nn}D";
  105.     local nn = int(n);
  106.  
  107.     fprintf(stderr,replace(_crsr_right_));
  108. }
  109.  
  110. # function to erase to end of current line - including cursor position
  111. function erase_end() {
  112.       # erase to end of line, including cursor position
  113.     local _erase_end_line_ = "\x01b[K";
  114.  
  115.     fprintf(stderr,_erase_end_line_);
  116. }
  117.  
  118. # function to set display colors
  119. # attr --> sets display attribute
  120. #    0 --> leave attribute as is
  121. #    1 --> high intensity
  122. #    5 --> blink
  123. # fore --> sets foreground color
  124. # back --> sets background color
  125. # colors for both foreground and background are:
  126. #    0 --> Black
  127. #    1 --> Red
  128. #    2 --> Green
  129. #    3 --> Yellow
  130. #    4 --> Blue
  131. #    5 --> Magenta
  132. #    6 --> Cyan
  133. #    7 --> White
  134. function set_color(attr,fore,back) {
  135.     local _attr = int(attr) % 10;
  136.     local _fore = int(fore) % 8 + 30;
  137.     local _back = int(back) % 8 + 40;
  138.       # set display color string
  139.     local _set_display_color_ = _attr ? "\x01b[{_attr};{_fore};{_back}m" : "\x01b[{_fore};{_back}m";
  140.  
  141.     fprintf(stderr,replace(_set_display_color_));
  142. }
  143.  
  144. function randint(n) {
  145.     return int(n * rand()) + 1;
  146. }
  147.