|
1 | 1 | -- | This module provides a binding to the Node `readline` API.
|
2 | 2 |
|
3 |
| -module Node.ReadLine where |
| 3 | +module Node.ReadLine |
| 4 | + ( Interface |
| 5 | + , READLINE |
| 6 | + , InterfaceOptions |
| 7 | + , Completer |
| 8 | + , LineHandler |
| 9 | + , createInterface |
| 10 | + , createConsoleInterface |
| 11 | + , output |
| 12 | + , completer |
| 13 | + , terminal |
| 14 | + , historySize |
| 15 | + , noCompletion |
| 16 | + , prompt |
| 17 | + , setPrompt |
| 18 | + , setLineHandler |
| 19 | + , close |
| 20 | + ) where |
4 | 21 |
|
5 |
| -import Prelude (return) |
6 |
| - |
7 |
| -import Control.Monad.Eff |
8 |
| -import Control.Monad.Eff.Console |
| 22 | +import Prelude (Unit, return, (<>), ($)) |
| 23 | +import Control.Monad.Eff (Eff) |
| 24 | +import Control.Monad.Eff.Console (CONSOLE) |
| 25 | +import Control.Monad.Eff.Exception (EXCEPTION) |
| 26 | +import Data.Foreign (Foreign) |
| 27 | +import Data.Options (Options, Option, (:=), options, opt) |
| 28 | +import Node.Process (stdin, stdout) |
| 29 | +import Node.Stream (Readable, Writable) |
9 | 30 |
|
10 | 31 | -- | A handle to a console interface.
|
11 | 32 | -- |
|
12 | 33 | -- | A handle can be created with the `createInterface` function.
|
13 | 34 | foreign import data Interface :: *
|
14 | 35 |
|
| 36 | +-- | The effect of interacting with a stream via an `Interface` |
| 37 | +foreign import data READLINE :: ! |
| 38 | + |
| 39 | +foreign import createInterfaceImpl :: forall eff. |
| 40 | + Foreign |
| 41 | + -> Eff ( readline :: READLINE |
| 42 | + | eff |
| 43 | + ) Interface |
| 44 | + |
| 45 | +-- | Options passed to `readline`'s `createInterface` |
| 46 | +data InterfaceOptions |
| 47 | + |
| 48 | +output :: forall w eff. Option InterfaceOptions (Writable w eff) |
| 49 | +output = opt "output" |
| 50 | + |
| 51 | +completer :: forall eff. Option InterfaceOptions (Completer eff) |
| 52 | +completer = opt "completer" |
| 53 | + |
| 54 | +terminal :: Option InterfaceOptions Boolean |
| 55 | +terminal = opt "terminal" |
| 56 | + |
| 57 | +historySize :: Option InterfaceOptions Int |
| 58 | +historySize = opt "historySize" |
| 59 | + |
15 | 60 | -- | A function which performs tab completion.
|
16 | 61 | -- |
|
17 |
| --- | This function takes the partial command as input, and returns a collection of |
| 62 | +-- | This function takes the partial command as input, and returns a collection of |
18 | 63 | -- | completions, as well as the matched portion of the input string.
|
19 |
| -type Completer eff = String -> Eff (console :: CONSOLE | eff) { completions :: Array String, matched :: String } |
| 64 | +type Completer eff = String -> Eff eff { completions :: Array String |
| 65 | + , matched :: String } |
20 | 66 |
|
21 |
| --- | A function which handles input from the user. |
22 |
| -type LineHandler eff a = String -> Eff (console :: CONSOLE | eff) a |
| 67 | +-- | Builds an interface with the specified options. |
| 68 | +createInterface :: forall r eff. |
| 69 | + Readable r ( readline :: READLINE |
| 70 | + | eff |
| 71 | + ) |
| 72 | + -> Options InterfaceOptions |
| 73 | + -> Eff ( readline :: READLINE |
| 74 | + | eff |
| 75 | + ) Interface |
| 76 | +createInterface input opts = createInterfaceImpl |
| 77 | + $ options $ opts |
| 78 | + <> opt "input" := input |
23 | 79 |
|
24 |
| --- | Set the current line handler function. |
25 |
| -foreign import setLineHandler :: forall eff a. Interface -> LineHandler eff a -> Eff (console :: CONSOLE | eff) Interface |
| 80 | +-- | Create an interface with the specified completion function. |
| 81 | +createConsoleInterface :: forall eff. |
| 82 | + Completer ( readline :: READLINE |
| 83 | + , console :: CONSOLE |
| 84 | + , err :: EXCEPTION |
| 85 | + | eff |
| 86 | + ) |
| 87 | + -> Eff ( readline :: READLINE |
| 88 | + , console :: CONSOLE |
| 89 | + , err :: EXCEPTION |
| 90 | + | eff |
| 91 | + ) Interface |
| 92 | +createConsoleInterface compl = createInterface stdin $ output := stdout |
| 93 | + <> completer := compl |
| 94 | + |
| 95 | +-- | A completion function which offers no completions. |
| 96 | +noCompletion :: forall eff. Completer eff |
| 97 | +noCompletion s = return { completions: [], matched: s } |
26 | 98 |
|
27 | 99 | -- | Prompt the user for input on the specified `Interface`.
|
28 |
| -foreign import prompt :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface |
| 100 | +foreign import prompt :: forall eff. |
| 101 | + Interface |
| 102 | + -> Eff ( readline :: READLINE |
| 103 | + | eff |
| 104 | + ) Unit |
29 | 105 |
|
30 | 106 | -- | Set the prompt.
|
31 |
| -foreign import setPrompt :: forall eff. String -> Int -> Interface -> Eff (console :: CONSOLE | eff) Interface |
32 |
| - |
33 |
| --- | Create an interface with the specified completion function. |
34 |
| -foreign import createInterface :: forall eff. Completer eff -> Eff (console :: CONSOLE | eff) Interface |
| 107 | +foreign import setPrompt :: forall eff. |
| 108 | + String |
| 109 | + -> Int |
| 110 | + -> Interface |
| 111 | + -> Eff ( readline :: READLINE |
| 112 | + | eff |
| 113 | + ) Unit |
35 | 114 |
|
36 | 115 | -- | Close the specified `Interface`.
|
37 |
| -foreign import close :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface |
| 116 | +foreign import close :: forall eff. |
| 117 | + Interface |
| 118 | + -> Eff ( readline :: READLINE |
| 119 | + | eff |
| 120 | + ) Unit |
38 | 121 |
|
39 |
| --- | A completion function which offers no completions. |
40 |
| -noCompletion :: forall eff. Completer eff |
41 |
| -noCompletion s = return { completions: [], matched: s } |
| 122 | +-- | A function which handles each line of input. |
| 123 | +type LineHandler eff a = String -> Eff eff a |
42 | 124 |
|
| 125 | +-- | Set the current line handler function. |
| 126 | +foreign import setLineHandler :: forall eff a. |
| 127 | + Interface |
| 128 | + -> LineHandler ( readline :: READLINE |
| 129 | + | eff |
| 130 | + ) a |
| 131 | + -> Eff ( readline :: READLINE |
| 132 | + | eff |
| 133 | + ) Unit |
0 commit comments