[clean-list] Thanks, Command Prompts, and Curses (not swearing but the console cursor library :))

Jay Kint jean-luc-picard@ussenterprise.com
Wed, 3 Oct 2001 01:29:32 -0600


Just wanted to thank everyone who helped me out.  Here is the code that I
finally came to.  It might be useful for someone else, as Clean seems geared
towards writing GUIs, but I want something very easy to just throw up a
command prompt and test basic commands.

-- Start Code: CommandPrompt.icl --
implementation module CommandPrompt

import StdArray
import StdEnum
import StdEnv

// command loop functions
getCommand :: !*File -> (String,*File)
getCommand console = stripcr (freadline (fwritec '>' console))
where
 copyString :: !*String !*String Int -> !*String
 copyString s d n = { d & [i] = s.[i] \\ i <- [0..n] }
 stripcr :: (!*String,*File) -> (!*String,*File)
 stripcr (s,f)
  # (n,s) = usize s
    d = createArray (n-1) '\0'
  = (copyString s d (n-1),f)

commandLoop :: !*File (*File->(String,*File)) (String !*File ->
(String,!*File)) -> *File
commandLoop console get handler
 # (command, console) = get console
 | command == "quit"  = console
 # (result, console) = handler command console
 | result == "error"  = console
 | otherwise    = commandLoop console get handler

commandPrompt :: *World (String !*File -> (String,!*File)) -> *File
commandPrompt world handler
 # (console,world) = stdio world
 = commandLoop console getCommand handler
-- End Code: CommandPrompt.icl --
-- Start Code: CommandPrompt.dcl --
definition module CommandPrompt

import StdString

commandPrompt :: *World (String !*File -> (String,!*File))-> *File
-- End Code: CommandPrompt.dcl --
-- Start Code: Test.icl --
handler :: String !*File -> (String,!*File)
handler s f
 = (s,fwrites s f)

Start :: *World -> *File
Start world = commandPrompt world handler
-- End Code: Test.icl --

This will simply loop printing a prompt '>' and accepting a line of text as
a command.  The string and the output stream are handed to a function,
handler in the Test I tried.

To quit, if not obvious from the code, is to type "quit" at the prompt.  The
handler function will cause the loop to quit when it returns "error" as the
string it returns.

Probably a banal piece of code for most of you, but I like to start small :)
I would also like some pointers on working with the console, besides basic
reads and writes.  Is there a curses like library for Clean?

Jay