module Network.Console ( CompIO , Comp , mkComp , runConsole , readConsole , compFilename , addHist , compCatch , compHFlush , compPrint , compPutStr , compHPutStrLn , lastWord ) where import System.Console.Editline.Readline --import System.Console.Readline import Prelude hiding (catch) import Control.Exception import System.IO import Data.Char (isSpace) readConsole :: String -> IO (Maybe String) readConsole = readline compFilename :: String -> IO [ String ] compFilename "" = filenameCompletionFunction "" compFilename strGen = filenameCompletionFunction str where str = if isSpace $ last strGen then "" else lastWord strGen data Comp = Comp (String -> IO [ String ]) getCompFunc :: Comp -> String -> IO [ String ] getCompFunc (Comp cmp) _ = do bf <- getLineBuffer -- putStr "\nDEBUG: "; print bf cmp bf mkComp :: (String -> IO [ String ]) -> Comp mkComp = Comp type CompIO = IO runConsole :: Comp -> CompIO a -> IO a runConsole cmp act = do setCompletionEntryFunction $ Just $ getCompFunc cmp act compCatch :: Exception e => CompIO a -> (e -> CompIO a) -> CompIO a compCatch = catch compPutStr :: String -> CompIO () compPutStr = putStr compHFlush :: Handle -> CompIO () compHFlush = hFlush addHist :: String -> CompIO () addHist = addHistory compPrint :: Show a => a -> CompIO () compPrint = print compHPutStrLn :: Handle -> String -> CompIO () compHPutStrLn = hPutStrLn lastWord :: String -> String lastWord ln = if isSpace ln then "" else last $ words ln