packages feed

console-program 0.4.0.3 → 0.4.1.0

raw patch · 3 files changed

+28/−8 lines, 3 files

Files

CHANGELOG.md view
@@ -1,5 +1,10 @@ # Changelog +### 0.4.1.0++* Add the possibility to customize the prompt in interactive mode.+* Eliminate dependency on unix package on Win32.+ ## 0.4.0.0  * API change: the `Command` constructor now takes an extra argument
console-program.cabal view
@@ -1,5 +1,5 @@ name:            console-program-version:         0.4.0.3+version:         0.4.1.0 cabal-Version:   >= 1.6 license:         BSD3 author:          Arie Peterson@@ -52,8 +52,10 @@     utility-ht == 0.0.*,     split == 0.2.*,     parsec == 3.1.*,-    parsec-extra == 0.1.*,-    unix >= 2.7 && < 2.8+    parsec-extra == 0.1.*+  if !os(windows)+    build-depends:+      unix >= 2.7 && < 2.8   exposed-modules:     System.Console.Argument,     System.Console.Command,
src/System/Console/Program.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} -- | This module contains functions to build a console program, that parses -- the command line (and a configuration file), divides it into commands,@@ -10,6 +11,7 @@   -- * Using a command tree to construct a program     single   , interactive+  , interactiveWithPrompt   , showUsage      -- * Configuration file@@ -49,7 +51,9 @@ import           System.Environment        (getArgs) import           System.Exit               (exitSuccess) import           System.IO                 (readFile)+#ifndef mingw32_HOST_OS import qualified System.Posix.Signals         as Sig+#endif import qualified Text.Parsec                  as P import qualified Text.PrettyPrint.ANSI.Leijen as PP @@ -89,9 +93,10 @@       optionIndex = Map.fromList .         concatMap (\ ((i,names),_) -> map (flip (,) i) names) .         options $ action command-      lookupSetting = maybe-        (error "System.Console.Program.parse: option not found")-        id . flip Map.lookup optionIndex+      lookupSetting i = maybe+        (error $ "Unknown option: " ++ show i)+        id+        $ Map.lookup i optionIndex       identifiedFileSettings = Map.fromList $ map (first lookupSetting) fileSettings       settings = commandLineSettings `Map.union` identifiedFileSettings   run (action command) nonOpts settings@@ -126,9 +131,17 @@ -- instead, the user may repeatedly enter a command, possibly with options, -- which will be executed. interactive :: (MonadIO m,Haskeline.MonadException m,Applicative m) => Commands m -> m ()-interactive commands = do+interactive commands = interactiveWithPrompt (return $ name $ Tree.rootLabel commands) commands++-- | Same as 'interactive', but with a custom monadic function specifying the+-- text of the prompt.+interactiveWithPrompt :: (MonadIO m,Haskeline.MonadException m,Applicative m)+  => m String -> Commands m -> m ()+interactiveWithPrompt prompt commands = do   tid <- liftIO myThreadId+#ifndef mingw32_HOST_OS   liftIO $ Sig.installHandler Sig.keyboardSignal (Sig.Catch $ throwTo tid UserInterrupt) Nothing+#endif   Haskeline.runInputT Haskeline.defaultSettings $ sequence_ . repeat $ one  where   one = getLine' >>= \ line -> case words' line of@@ -139,7 +152,7 @@         then liftIO $ print e         else Haskeline.throwIO e)   getLine' = do-    putStrBold $ name (Tree.rootLabel commands)+    putStrBold =<< lift prompt     maybe (liftIO exitSuccess) return =<< Haskeline.getInputLine ": "  words' :: String -> Either String [String]