{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module System.Console.Repline (
HaskelineT,
runHaskelineT,
evalRepl,
ReplOpts(..),
evalReplOpts,
Cmd,
Options,
WordCompleter,
LineCompleter,
CompleterStyle(..),
Command,
CompletionFunc,
wordCompleter,
listCompleter,
fileCompleter,
listWordCompleter,
runMatcher,
trimComplete,
abort,
tryAction,
dontCrash,
) where
import System.Console.Haskeline.Completion
import System.Console.Haskeline.MonadException
import qualified System.Console.Haskeline as H
import Data.List (isPrefixOf)
import Control.Applicative
import Control.Monad.Fail as Fail
import Control.Monad.State.Strict
import Control.Monad.Reader
newtype HaskelineT (m :: * -> *) a = HaskelineT { unHaskeline :: H.InputT m a }
deriving (Monad, Functor, Applicative, MonadIO, MonadException, MonadTrans, MonadHaskeline)
runHaskelineT :: MonadException m => H.Settings m -> HaskelineT m a -> m a
runHaskelineT s m = H.runInputT s (H.withInterrupt (unHaskeline m))
class MonadException m => MonadHaskeline m where
getInputLine :: String -> m (Maybe String)
getInputChar :: String -> m (Maybe Char)
outputStr :: String -> m ()
outputStrLn :: String -> m ()
instance MonadException m => MonadHaskeline (H.InputT m) where
getInputLine = H.getInputLine
getInputChar = H.getInputChar
outputStr = H.outputStr
outputStrLn = H.outputStrLn
instance Fail.MonadFail m => Fail.MonadFail (HaskelineT m) where
fail = lift . Fail.fail
instance MonadState s m => MonadState s (HaskelineT m) where
get = lift get
put = lift . put
instance MonadReader r m => MonadReader r (HaskelineT m) where
ask = lift ask
local f (HaskelineT m) = HaskelineT $ H.mapInputT (local f) m
instance (MonadHaskeline m) => MonadHaskeline (StateT s m) where
getInputLine = lift . getInputLine
getInputChar = lift . getInputChar
outputStr = lift . outputStr
outputStrLn = lift . outputStrLn
type Cmd m = [String] -> m ()
type Options m = [(String, Cmd m)]
type Command m = String -> m ()
type WordCompleter m = (String -> m [String])
type LineCompleter m = (String -> String -> m [Completion])
tryAction :: MonadException m => HaskelineT m a -> HaskelineT m a
tryAction (HaskelineT f) = HaskelineT (H.withInterrupt loop)
where loop = handle (\H.Interrupt -> loop) f
dontCrash :: (MonadIO m, H.MonadException m) => m () -> m ()
dontCrash m = H.catch m ( \ e@SomeException{} -> liftIO ( putStrLn ( show e ) ) )
abort :: MonadIO m => HaskelineT m a
abort = throwIO H.Interrupt
replLoop :: (Functor m, MonadException m)
=> HaskelineT m String
-> Command (HaskelineT m)
-> Options (HaskelineT m)
-> Maybe Char
-> HaskelineT m ()
replLoop banner cmdM opts optsPrefix = loop
where
loop = do
prefix <- banner
minput <- H.handleInterrupt (return (Just "")) $ getInputLine prefix
case minput of
Nothing -> outputStrLn "Goodbye."
Just "" -> loop
Just (prefix: cmds)
| null cmds -> handleInput [prefix] >> loop
| Just prefix == optsPrefix ->
case words cmds of
[] -> loop
(cmd:args) -> do
let optAction = optMatcher cmd opts args
result <- H.handleInterrupt (return Nothing) $ Just <$> optAction
maybe exit (const loop) result
Just input -> do
handleInput input
loop
handleInput input = H.handleInterrupt exit $ cmdM input
exit = return ()
optMatcher :: MonadHaskeline m => String -> Options m -> [String] -> m ()
optMatcher s [] _ = outputStrLn $ "No such command :" ++ s
optMatcher s ((x, m):xs) args
| s `isPrefixOf` x = m args
| otherwise = optMatcher s xs args
data ReplOpts m = ReplOpts {
banner :: HaskelineT m String
, command :: Command (HaskelineT m)
, options :: Options (HaskelineT m)
, prefix :: Maybe Char
, tabComplete :: CompleterStyle m
, initialiser :: HaskelineT m ()
}
evalReplOpts :: (Functor m, MonadException m) => ReplOpts m -> m ()
evalReplOpts (ReplOpts {..}) = evalRepl
banner
command
options
prefix
tabComplete
initialiser
evalRepl :: (Functor m, MonadException m)
=> HaskelineT m String
-> Command (HaskelineT m)
-> Options (HaskelineT m)
-> Maybe Char
-> CompleterStyle m
-> HaskelineT m a
-> m ()
evalRepl banner cmd opts optsPrefix comp initz = runHaskelineT _readline (initz >> monad)
where
monad = replLoop banner cmd opts optsPrefix
_readline = H.Settings
{ H.complete = mkCompleter comp
, H.historyFile = Just ".history"
, H.autoAddHistory = True
}
data CompleterStyle m
= Word (WordCompleter m)
| Word0 (WordCompleter m)
| Cursor (LineCompleter m)
| File
| Prefix
(CompletionFunc m)
[(String, CompletionFunc m)]
mkCompleter :: MonadIO m => CompleterStyle m -> CompletionFunc m
mkCompleter (Word f) = completeWord (Just '\\') " \t()[]" (_simpleComplete f)
mkCompleter (Word0 f) = completeWord (Just '\\') " \t()[]" (_simpleCompleteNoSpace f)
mkCompleter (Cursor f) = completeWordWithPrev (Just '\\') " \t()[]" (unRev0 f)
mkCompleter File = completeFilename
mkCompleter (Prefix def opts) = runMatcher opts def
unRev0 :: LineCompleter m -> LineCompleter m
unRev0 f x = f (reverse x)
trimComplete :: String -> Completion -> Completion
trimComplete prefix (Completion a b c) = Completion (drop (length prefix) a) b c
_simpleComplete :: (Monad m) => (String -> m [String]) -> String -> m [Completion]
_simpleComplete f word = f word >>= return . map simpleCompletion
_simpleCompleteNoSpace :: (Monad m) => (String -> m [String]) -> String -> m [Completion]
_simpleCompleteNoSpace f word = f word >>= return . map completionNoSpace
completionNoSpace :: String -> Completion
completionNoSpace str = Completion str str False
wordCompleter :: Monad m => WordCompleter m -> CompletionFunc m
wordCompleter f (start, n) = completeWord (Just '\\') " \t()[]" (_simpleComplete f) (start, n)
listCompleter :: Monad m => [String] -> CompletionFunc m
listCompleter names (start, n) = completeWord (Just '\\') " \t()[]" (_simpleComplete (complete_aux names)) (start, n)
listWordCompleter :: Monad m => [String] -> WordCompleter m
listWordCompleter = complete_aux
fileCompleter :: MonadIO m => CompletionFunc m
fileCompleter = completeFilename
complete_aux :: Monad m => [String] -> WordCompleter m
complete_aux names n = return $ filter (isPrefixOf n) names
completeMatcher :: (Monad m) => CompletionFunc m -> String
-> [(String, CompletionFunc m)]
-> CompletionFunc m
completeMatcher def _ [] args = def args
completeMatcher def [] _ args = def args
completeMatcher def s ((x, f):xs) args
| x `isPrefixOf` s = f args
| otherwise = completeMatcher def s xs args
runMatcher
:: Monad m => [(String, CompletionFunc m)]
-> CompletionFunc m
-> CompletionFunc m
runMatcher opts def (start, n) =
completeMatcher def (n ++ reverse start) opts (start, n)