ui-1.0.0: Minimalistic console UI (getLine), arrow key support (edit, browse cmd history).
Copyright(c) dr. Jonas Birch 2024
LicenseMIT
Maintainerdr. Jonas Birch <mnt@doctorbirch.com>
Stabilitystable
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Data.UI

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).

Synopsis

Data types

data Term Source #

Term is the internal state of the library.

Constructors

Term 

Fields

Instances

Instances details
Show Term Source # 
Instance details

Defined in Data.UI

Methods

showsPrec :: Int -> Term -> ShowS #

show :: Term -> String #

showList :: [Term] -> ShowS #

data TermRes Source #

TermRes is the result of the input. Returned whenever the user presses a key.

Constructors

TKey Key

Returns the numeric value on each key press (except enter).

TLine Line

Returns the line of data when user presses enter.

Instances

Instances details
Show TermRes Source # 
Instance details

Defined in Data.UI

type Line = String Source #

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
>