1||| Various helper functions for creating simple interactive systems.
3||| These are mostly intended for helping with teaching, in that they will allow
4||| the easy creation of interactive programs without needing to teach IO
5||| or Effects first, but they also capture some common patterns of interactive
7module System.Interactive
10When the interfaces and names are stable, these are intended for the Prelude.
15||| Process input from an open file handle, while maintaining a state.
16||| @ state the input state
17||| @ onRead the function to run on reading a line, returning a String to
18||| output and a new state
19||| @ onEOF the function to run on reaching end of file, returning a String
22processHandle : File ->
24 (onRead : a -> String -> (String, a)) ->
25 (onEOF : a -> String) ->
27processHandle h acc onRead onEOF
29 then putStr (onEOF acc)
31 let (out, acc') = onRead acc x
33 processHandle h acc' onRead onEOF
35||| Process input from the standard input stream, while maintaining a state.
36||| @ state the input state
37||| @ onRead the function to run on reading a line, returning a String to
38||| output and a new state
39||| @ onEOI the function to run on reaching end of input, returning a String
42processStdin : (state : a) ->
43 (onRead : a -> String -> (String, a)) ->
44 (onEOI : a -> String) -> IO ()
45processStdin = processHandle stdin
47||| A basic read-eval-print loop, maintaining a state
48||| @ state the input state
49||| @ prompt the prompt to show
50||| @ onInput the function to run on reading input, returning a String to
51||| output and a new state. Returns Nothing if the repl should exit
53replWith : (state : a) -> (prompt : String) ->
54 (onInput : a -> String -> Maybe (String, a)) -> IO a
59 Just (out, acc') => do putStr out
60 replWith acc' prompt fn
63||| A basic read-eval-print loop
64||| @ prompt the prompt to show
65||| @ onInput the function to run on reading input, returning a String to
68repl : (prompt : String) ->
69 (onInput : String -> String) -> IO ()
71 = do replWith () prompt (\x, s => Just (fn s, ()))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list