repline 0.1.3.0 → 0.1.4.0
raw patch · 5 files changed
+195/−4 lines, 5 filesdep +processdep +replinedep ~basedep ~containersdep ~mtlnew-component:exe:Examplenew-component:exe:Simple
Dependencies added: process, repline
Dependency ranges changed: base, containers, mtl
Files
- Example.hs +129/−0
- README.md +2/−1
- Simple.hs +43/−0
- repline.cabal +13/−3
- src/System/Console/Repline.hs +8/−0
+ Example.hs view
@@ -0,0 +1,129 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE TypeSynonymInstances #-}++module Main where++import System.Console.Repline++import qualified Data.Set as Set+import Control.Monad.State.Strict++import Data.List (isPrefixOf)++-------------------------------------------------------------------------------+-- Stateful Completion+-------------------------------------------------------------------------------++type IState = Set.Set String+type Repl1 a = HaskelineT (StateT IState IO) a++-- Evaluation+cmd1 :: String -> Repl1 ()+cmd1 input = modify $ Set.insert input++-- Completion+completer1 :: (Monad m, MonadState IState m) => WordCompleter m+completer1 n = do+ ns <- get+ return $ filter (isPrefixOf n) (Set.toList ns)++-- Commands+help1 :: [String] -> Repl1 ()+help1 args = liftIO $ print $ "Help!" ++ show args++puts1 :: [String] -> Repl1 ()+puts1 args = modify $ Set.union (Set.fromList args)++opts1 :: [(String, [String] -> Repl1 ())]+opts1 = [+ ("help", help1) -- :help+ , ("puts", puts1) -- :puts+ ]++init1 :: Repl1 ()+init1 = return ()++-- Tab completion inside of StateT+repl1 :: IO ()+repl1 = flip evalStateT Set.empty+ $ evalRepl "_proto> " cmd1 opts1 (Word completer1) init1++-------------------------------------------------------------------------------+-- Command options+-------------------------------------------------------------------------------++type Repl2 a = HaskelineT IO a++-- Evaluation+cmd2 :: String -> Repl2 ()+cmd2 input = liftIO $ print input++-- Completion+comp2 :: Monad m => WordCompleter m+comp2 = listWordCompleter ["kirk", "spock", "mccoy"]++-- Commands+help2 :: [String] -> Repl2 ()+help2 args = liftIO $ print $ "Help!" ++ show args++opts2 :: [(String, [String] -> Repl2 ())]+opts2 = [+ ("help", help2)+ ]++init2 :: Repl2 ()+init2 = liftIO $ putStrLn "Welcome!"++repl2 :: IO ()+repl2 = evalRepl "example2> " cmd2 opts2 (Word comp2) init2++-------------------------------------------------------------------------------+-- Mixed Completion+-------------------------------------------------------------------------------++type Repl3 a = HaskelineT IO a++-- Evaluation+cmd3 :: String -> Repl3 ()+cmd3 input = liftIO $ print input++defaultMatcher :: MonadIO m => [(String, CompletionFunc m)]+defaultMatcher = [+ (":file" , fileCompleter)+ , (":holiday" , listCompleter ["christmas", "thanksgiving", "festivus"])+ ]++byWord :: Monad m => WordCompleter m+byWord n = do+ let names = ["picard", "riker", "data", ":file", ":holiday"]+ return $ filter (isPrefixOf n) names++files :: [String] -> Repl3 ()+files args = liftIO $ do+ contents <- readFile (unwords args)+ putStrLn contents++holidays :: [String] -> Repl3 ()+holidays [] = liftIO $ putStrLn "Enter a holiday."+holidays xs = liftIO $ do+ putStrLn $ "Happy " ++ unwords xs ++ "!"++opts3 :: [(String, [String] -> Repl3 ())]+opts3 = [+ ("file", files)+ , ("holiday", holidays)+ ]++init3 :: Repl3 ()+init3 = return ()++repl3 :: IO ()+repl3 = evalRepl "example3> " cmd3 opts3 (Prefix (wordCompleter byWord) defaultMatcher) init3++-------------------------------------------------------------------------------+--+-------------------------------------------------------------------------------++main :: IO ()+main = repl1 >> repl2 >> repl3
README.md view
@@ -49,7 +49,8 @@ Trying it out: ```haskell-$ runhaskell Main.hs+$ runhaskell Simple.hs+# Or if in a sandbox: cabal exec runhaskell Simple.hs Welcome! >>> <TAB> kirk spock mccoy
+ Simple.hs view
@@ -0,0 +1,43 @@+module Main where++import Control.Monad.Trans+import System.Console.Repline++import System.Process (callCommand)+import Data.List (isPrefixOf)++type Repl a = HaskelineT IO a++-- Evaluation : handle each line user inputs+cmd :: String -> Repl ()+cmd input = liftIO $ print input++-- Tab Completion: return a completion for partial words entered+completer :: Monad m => WordCompleter m+completer n = do+ let names = ["kirk", "spock", "mccoy"]+ return $ filter (isPrefixOf n) names++-- Commands+help :: [String] -> Repl ()+help args = liftIO $ print $ "Help: " ++ show args++say :: [String] -> Repl ()+say args = do+ _ <- liftIO $ callCommand $ "cowsay" ++ " " ++ (unwords args)+ return ()++options :: [(String, [String] -> Repl ())]+options = [+ ("help", help) -- :help+ , ("say", say) -- :say+ ]++ini :: Repl ()+ini = liftIO $ putStrLn "Welcome!"++repl :: IO ()+repl = evalRepl ">>> " cmd options (Word0 completer) ini++main :: IO ()+main = repl
repline.cabal view
@@ -1,5 +1,5 @@ name: repline-version: 0.1.3.0+version: 0.1.4.0 synopsis: Haskeline wrapper for GHCi-like REPL interfaces. license: MIT license-file: LICENSE@@ -10,7 +10,7 @@ build-type: Simple extra-source-files: README.md cabal-version: >=1.10-tested-with: GHC == 7.6.1, GHC == 7.6.3, GHC == 7.8.3+tested-with: GHC == 7.6.1, GHC == 7.6.3, GHC == 7.8.3, GHC == 7.10.1 Bug-Reports: https://github.com/sdiehl/repline/issues Description:@@ -24,7 +24,7 @@ exposed-modules: System.Console.Repline -- other-modules: build-depends:- base >= 4.6 && <4.8,+ base >= 4.6 && <4.9, containers >= 0.5 && <0.6, mtl >= 2.2 && <2.3, haskeline >= 0.7 && <0.8@@ -36,3 +36,13 @@ MultiParamTypeClasses, GeneralizedNewtypeDeriving default-language: Haskell2010++executable Simple+ default-language: Haskell2010+ main-is: Simple.hs+ build-depends: base, repline, process, mtl++executable Example+ default-language: Haskell2010+ main-is: Example.hs+ build-depends: base, repline, mtl, containers
src/System/Console/Repline.hs view
@@ -243,6 +243,7 @@ data CompleterStyle m = Word (WordCompleter m) -- ^ Completion function takes single word.+ | Word0 (WordCompleter m) -- ^ Completion function takes single word ( no space ). | Cursor (LineCompleter m) -- ^ Completion function takes tuple of full line. | File -- ^ Completion function completes files in CWD. | Prefix@@ -251,6 +252,7 @@ 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@@ -264,6 +266,12 @@ _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)