ghci command output capture & full :browse! (draft patches)
Simon Marlow
simonmarhaskell at gmail.com
Fri Sep 7 10:17:18 EDT 2007
Claus Reinke wrote:
>>> And the fact that this only applies to certain commands, and not to
>>> output generated by ordinary expressions/statements, means it feels
>>> too ad-hoc to me.
> ..
>> but note that i'm looking for output capture, within GHCi, rather than
>> redirection of a whole session. one could try,
>> alternatively, to redirect stdout to a tmp file, then read that
>> into a variable (:redir >tmpfile | commands | :redir END, in
>> vim). perhaps that approach would give better coverage,
>> i'm open to suggestions/opinions.
>
> (actually, :redir =>variable | commands | :redir END)
This is fine, but if we were to do this properly I would still go for a
syntax like
output <- :browse M
which avoids the problems with your earlier solution.
> that route does indeed look easier. i guess i was confused
> because there seems to be no haskell function for redirect,
> but the neccessary tools are available in GHC.Handle (why
> not in the standard libs? i have often search for this..).
>
> in fact, i can almost get by without even extending GHCi,
> as far as command output capture is concerned (see below),
> so i can focus on the :browse! part of the patches i sent.
>
> great!-) the only problem being that the intermediate bindings
> (for h,sto, and var) are still being echoed, and apparently
> not to the redirected stdout. is there a way to avoid that
> output?
There are two stdouts in GHCi - the user program's stdout and GHCi's
stdout. They are probably both attached to FD 1 in most cases, but it
looks like you've redirected one and not the other.
This won't always be the case, when we are using shared libraries both GHCi
and the user program will be sharing a stdout. I'm not sure exactly how
this will work yet, we might have to save/restore stdout across execution
of user code.
> btw, is there a wiki page collecting useful ghci :defs?
Not as far as I'm aware, care to start one?
Cheers,
Simon
> claus
>
> ----------------------------------------------
> import GHC.Handle
> import IO
> import Data.Char
>
> -- :def redir redir
> --
> -- :redir var :browse Data.Maybe
> -- :redir out :?
> --
> -- problem: h/sto/var are still printed
> redir varcmd =
> let file = "ghci.tmp"
> (var,_:cmd)=span (not . Data.Char.isSpace) varcmd
> in return $ unlines
> ["h <- openFile "++show file++" WriteMode"
> ,"sto <- hDuplicate stdout"
> ,"hDuplicateTo h stdout"
> ,"hClose h"
> ,cmd
> ,"hDuplicateTo sto stdout"
> ,var++" <- readFile "++show file
> ]
> ----------------------------------------------
>
> $ ghcii.sh redir.hs
>
> *Main> :def redir redir
> Loading package haskell98 ... linking ... done.
>
> *Main> :redir a :t id
> {handle: ghci.tmp}
> {handle: <stdout>}
> "id :: a -> a\n"
>
> *Main> a
> "id :: a -> a\n"
>
> *Main> :redir out :b Data.Maybe
> {handle: ghci.tmp}
> {handle: <stdout>}
> "maybe :: b -> (a -> b) -> Maybe a -> b\ndata Maybe a = Nothing | Just
> a\nisJust :: Maybe a -> Bool\
> nisNothing :: Maybe a -> Bool\nfromJust :: Maybe a -> a\nfromMaybe :: a
> -> Maybe a -> a\nmaybeToList
> :: Maybe a -> [a]\nlistToMaybe :: [a] -> Maybe a\ncatMaybes :: [Maybe a]
> -> [a]\nmapMaybe :: (a ->
> Maybe b) -> [a] -> [b]\n"
>
> *Main> putStrLn out
> maybe :: b -> (a -> b) -> Maybe a -> b
> data Maybe a = Nothing | Just a
> isJust :: Maybe a -> Bool
> isNothing :: Maybe a -> Bool
> fromJust :: Maybe a -> a
> fromMaybe :: a -> Maybe a -> a
> maybeToList :: Maybe a -> [a]
> listToMaybe :: [a] -> Maybe a
> catMaybes :: [Maybe a] -> [a]
> mapMaybe :: (a -> Maybe b) -> [a] -> [b]
>
>
More information about the Cvs-ghc
mailing list