Copyright | (c) dr. Jonas Birch 2024 |
---|---|
License | MIT |
Maintainer | dr. Jonas Birch <mnt@doctorbirch.com> |
Stability | stable |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
Data.UI
Contents
Description
The UI library provides a more modern getLine style function for making console based CLIs (command line interfaces). The library provides editing capabilities through the use of arrow keys, as well as browsing through the command history.
It is very minimalistic, does only export two functions and doesn't require its own monad (only IO).
Data types
Term is the internal state of the library.
Constructors
Term | |
TermRes is the result of the input. Returned whenever the user presses a key.
Line is the datatype of the returned text string when the user presses enter. This type can easily be changed from the default (String) to Text or anything else. Change it in the type alias and it will be used everywhere.
Functions
init :: Line -> IO Term Source #
init initial_prompt
Initializes the terminal. Returns a handle which you pass on to the input function.
input :: Term -> IO (TermRes, Term) Source #
input handle
Reads one character from the keyboard. You give it the handle from the last function call. It returns the string when pressed enter and returns the key code otherwise.
Example:
> > import qualified Data.UI (init, input) > import Data.UI (TermRes(..)) > > example :: IO () > example = do > t <- Data.UI.init "prompt> " > go t > where > go t' = do > (res,newstate) <- Data.UI.input t' > case res of > TLine x -> do > _ <- putStrLn $ "\nResult: '" <> x <> "'" > go newstate > _ -> go newstate >