looking for System.Console.Readline example
I am looking for an example program that uses System.Console.Readline http://www.haskell.org/ghc/docs/latest/html/readline/System.Console.Readline... If my main program looks like this: main = do input <- getContents ; handle input can I just "drop in" readline somewhere? -- -- Johannes Waldmann, Tel/Fax: (0341) 3076 6479 / 6480 -- ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
tis 2003-12-02 klockan 12.55 skrev Johannes Waldmann:
I am looking for an example program that uses System.Console.Readline
http://www.haskell.org/ghc/docs/latest/html/readline/System.Console.Readline...
If my main program looks like this: main = do input <- getContents ; handle input
can I just "drop in" readline somewhere?
Sure. main = do input <- readline "prompt> " ; handle input Regards, Martin
Martin Norbäck wrote:
main = do input <- readline "prompt> " ; handle input
but this only reads one line (and it gives a Maybe String) I wanted a (lazy) list of all input lines, where the user should have the usual line editing functions available. -- -- Johannes Waldmann, Tel/Fax: (0341) 3076 6479 / 6480 -- ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
tis 2003-12-02 klockan 14.23 skrev Johannes Waldmann:
Martin Norbäck wrote:
main = do input <- readline "prompt> " ; handle input
but this only reads one line (and it gives a Maybe String) I wanted a (lazy) list of all input lines, where the user should have the usual line editing functions available.
What do you mean? Readline is for editing one line. If you want something like getting multiple lines until user presses ctrl-d or something you can do this: readlines :: IO [String] readlines = do input <- readline "" case input of Nothing -> return [] Just str -> do strs <- readlines return (str:strs) It won't be lazy though. Regards, Martin
Martin Norbäck wrote:
What do you mean? Readline is for editing one line.
well, yes and no. sure its built-in history functions precisely do help editing a sequence of lines? as used in bash, ghci, hugs? I now have something that works (bottom of this file): http://theo1.informatik.uni-leipzig.de/cgi-bin/cvsweb/autotool/Exp/Loop.hs?r... Still I think it would be nice to have Readline.getContents :: IO String that just returns the lazy input list (line by line, as getContents would do). It's just another kind of line buffering, and this should be transparent to the application. (Of course, it couldn't change the prompt symbol then, but I could live with that). -- -- Johannes Waldmann, Tel/Fax: (0341) 3076 6479 / 6480 -- ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
A minor modification to Martin's code gives you laziness:
import System.IO.Unsafe import System.Console.Readline
readlines :: String -> IO [String] readlines prompt = do input <- readline prompt case input of Nothing -> return [] Just str -> do strs <- unsafeInterleaveIO (readlines prompt) return (str:strs)
*R> readlines ">" >>= mapM (print . length)
foo 3 barbaz 6
From the libraries documentation: unsafeInterleaveIO :: IO a -> IO a unsafeInterleaveIO allows IO computation to be deferred lazily. When passed a value of type IO a, the IO will only be performed when the value of the a is demanded. This is used to implement lazy file reading, see hGetContents. -- Sebastien On Tue, Dec 02, 2003 at 03:34:58PM +0100, Johannes Waldmann wrote:
Martin Norbäck wrote:
What do you mean? Readline is for editing one line.
well, yes and no. sure its built-in history functions precisely do help editing a sequence of lines? as used in bash, ghci, hugs?
I now have something that works (bottom of this file):
http://theo1.informatik.uni-leipzig.de/cgi-bin/cvsweb/autotool/Exp/Loop.hs?r...
Still I think it would be nice to have
Readline.getContents :: IO String
that just returns the lazy input list (line by line, as getContents would do).
It's just another kind of line buffering, and this should be transparent to the application. (Of course, it couldn't change the prompt symbol then, but I could live with that).
-- -- Johannes Waldmann, Tel/Fax: (0341) 3076 6479 / 6480 -- ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (3)
-
Johannes Waldmann -
Martin Norbäck -
sebc@macs.hw.ac.uk