IO package:haskeline
This module provides a stateful, IO-based interface to Haskeline,
which may be easier to integrate into some existing programs or
libraries.
It is strongly recommended to use the safer, monadic API of
System.Console.Haskeline, if possible, rather than the explicit
state management functions of this module.
The equivalent REPL example is:
import System.Console.Haskeline
import System.Console.Haskeline.IO
import Control.Concurrent
main = bracketOnError (initializeInput defaultSettings)
cancelInput -- This will only be called if an exception such
-- as a SigINT is received.
(\hd -> loop hd >> closeInput hd)
where
loop :: InputState -> IO ()
loop hd = do
minput <- queryInput hd (getInputLine "% ")
case minput of
Nothing -> return ()
Just "quit" -> return ()
Just input -> do queryInput hd $ outputStrLn
$ "Input was: " ++ input
loop hd
Haskeline has two ways of interacting with the user:
- "Terminal-style" interaction provides an rich user interface by
connecting to the user's terminal (which may be different than
stdin or stdout).
- "File-style" interaction treats the input as a simple stream of
characters, for example when reading from a file or pipe. Input
functions (e.g., getInputLine) print the prompt to
stdout.
A
Behavior is a method for deciding at run-time which type of
interaction to use.
For most applications (e.g., a REPL),
defaultBehavior should
have the correct effect.
Read input from
stdin. Use terminal-style interaction if
stdin is connected to a terminal and has echoing enabled.
Otherwise (e.g., if
stdin is a pipe), use file-style
interaction.
This behavior should suffice for most applications.
Run a line-reading application according to the given behavior.
If it uses terminal-style interaction,
Prefs will be read from
the user's
~/.haskeline file (if present). If it uses
file-style interaction,
Prefs are not relevant and will not be
read.
Run a line-reading application.
Performs completions from the given line state.
The first
String argument is the contents of the line to the
left of the cursor, reversed. The second
String argument is the
contents of the line to the right of the cursor.
The output
String is the unused portion of the left half of the
line, reversed.
If the first completer produces no suggestions, fallback to the second
completer's output.
Disable completion altogether.
Create a finished completion out of the given word.