haskeline-0.2: A command-line interface for user input, written in Haskell.

System.Console.Haskeline

Contents

Synopsis

Main functions

An example use of this library for a simple read-eval-print loop is the following.

 import System.Console.Haskeline
 import Control.Monad.Trans
 
 main :: IO ()
 main = runInputT defaultSettings loop
    where 
        loop :: InputT IO ()
        loop = do
            minput <- getInputLine "% "
            case minput of
                Nothing -> return ()
                Just "quit" -> return ()
                Just input -> do outputStrLn $ "Input was: " ++ input
                                 loop

data InputT m a Source

A monad transformer which carries all of the state and settings relevant to a line-reading application.

Instances

MonadTrans InputT 
Monad m => MonadState History (InputT m) 
Monad m => MonadReader Prefs (InputT m) 
Monad m => Monad (InputT m) 
MonadIO m => MonadIO (InputT m) 
MonadException m => MonadException (InputT m) 
Monad m => MonadReader (RunTerm (InputCmdT m)) (InputT m) 
Monad m => MonadReader (Settings m) (InputT m) 

runInputT :: MonadException m => Settings m -> InputT m a -> m aSource

Run a line-reading application, reading user Prefs from ~/.haskeline

getInputLineSource

Arguments

:: forall m . MonadException m 
=> String

The input prompt

-> InputT m (Maybe String) 

Read one line of input from the user, with a rich line-editing user interface. Returns Nothing if the user presses Ctrl-D when the input text is empty. Otherwise, it returns the input line with the final newline removed.

If stdin is not connected to a terminal (for example, piped from another process), then this function is equivalent to getLine, except that it returns Nothing if an EOF is encountered before any characters are read.

If signal handling is enabled in the Settings, then getInputLine will throw an Interrupt exception when the user presses Ctrl-C.

outputStr :: forall m. MonadIO m => String -> InputT m ()Source

Write a string to the console output. Allows cross-platform display of Unicode characters.

outputStrLn :: MonadIO m => String -> InputT m ()Source

Write a string to the console output, followed by a newline. Allows cross-platform display of Unicode characters.

Settings

data Settings m Source

Application-specific customizations to the user interface.

Constructors

Settings 

Fields

complete :: CompletionFunc m

Custom tab completion

historyFile :: Maybe FilePath
 
handleSigINT :: Bool

Throw an Interrupt exception if the user presses Ctrl-C

Instances

Monad m => MonadReader (Settings m) (InputT m) 

defaultSettings :: MonadIO m => Settings mSource

A useful default. In particular:

 defaultSettings = Settings {
           complete = completeFilename,
           historyFile = Nothing,
           handleSigINT = False
           }

setComplete :: CompletionFunc m -> Settings m -> Settings mSource

Because complete is the only field of Settings depending on m, the expression defaultSettings {completionFunc = f} leads to a type error from being too general. This function may become unnecessary if another field depending on m is added.

Ctrl-C handling

handleInterruptSource

Arguments

:: MonadException m 
=> m a

Handler to run if Ctrl-C is pressed

-> m a

Computation to run

-> m a 

Catch and handle an exception of type Interrupt.