home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / AnsiInteract.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  3.1 KB  |  74 lines

  1. -----------------------------------------------------------------------------
  2. -- Library of functions for writing interactive programs with screen-oriented
  3. -- I/O (assumes Ansi screen).
  4. --
  5. -- Suitable for use with Hugs 98.
  6. -----------------------------------------------------------------------------
  7.  
  8. module AnsiInteract(
  9.     module AnsiInteract,
  10.     module Interact,
  11.     module AnsiScreen
  12.     ) where
  13.  
  14. import AnsiScreen
  15. import Interact
  16.  
  17. -- Screen oriented input/output functions:
  18.  
  19. clearScreen       :: Interact -> Interact
  20. writeAt           :: Pos -> String -> Interact -> Interact
  21. moveTo            :: Pos -> Interact -> Interact
  22. readAt            :: Pos                  ->  -- Start coordinates
  23.                      Int                  ->  -- Maximum input length
  24.                      (String -> Interact) ->  -- How to use entered string
  25.                      Interact
  26. defReadAt         :: Pos                  ->  -- Start coordinates        
  27.                      Int                  ->  -- Maximum input length     
  28.                      String               ->  -- Default string value     
  29.                      (String -> Interact) ->  -- How to use entered string
  30.                      Interact
  31. promptReadAt      :: Pos                  -> -- Start coordinates        
  32.                      Int                  -> -- Maximum input length     
  33.                      String               -> -- Prompt
  34.                      (String -> Interact) -> -- How to use entered string
  35.                      Interact
  36. defPromptReadAt   :: Pos                  -> -- Start coordinates        
  37.                      Int                  -> -- Maximum input length     
  38.                      String               -> -- Prompt
  39.                      String               -> -- Default string value
  40.                      (String -> Interact) -> -- How to use entered string
  41.                      Interact
  42.  
  43. clearScreen        = writeStr cls
  44. writeAt (x,y) s    = writeStr (goto x y ++ s)
  45. moveTo  (x,y)      = writeStr (goto x y)
  46.  
  47.  
  48. readAt pt l use    = writeAt pt (replicate l '_') (moveTo pt (loop 0 ""))
  49.  where loop n s    = readChar (return s) (\c ->
  50.                      case c of '\BS'         -> delete n s
  51.                                '\DEL'        -> delete n s
  52.                                '\n'          -> return s
  53.                                c | n < l     -> writeChar c (loop (n+1) (c:s))
  54.                                  | otherwise -> ringBell (loop n s))
  55.        delete n s  = if n>0 then writeStr "\BS_\BS" (loop (n-1) (tail s))
  56.                             else ringBell (loop 0 "")
  57.        return s    = use (reverse s)
  58.  
  59.  
  60. defReadAt (x,y) l def use
  61.                    = writeAt (x,y) (take l (def++repeat '_')) (
  62.                      readChar (use def) (\c ->
  63.                      if c=='\n' then use def
  64.                                 else unreadChar c (readAt (x,y) l use)))
  65.  
  66. promptReadAt (x,y) l prompt use
  67.                    = writeAt (x,y) prompt (readAt (x+length prompt,y) l use)
  68.  
  69. defPromptReadAt (x,y) l prompt def use
  70.                    = writeAt (x,y) prompt (
  71.                      defReadAt (x+length prompt,y) l def use)
  72.  
  73. -----------------------------------------------------------------------------
  74.