|
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 (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.Maybe (Maybe(Just)) |
| 28 | +import Data.Options (Options, Option, (:=), options, opt) |
| 29 | +import Node.Process (stdin, stdout) |
| 30 | +import Node.Stream (Readable, Writable) |
9 | 31 |
|
10 | 32 | -- | A handle to a console interface. |
11 | 33 | -- | |
12 | 34 | -- | A handle can be created with the `createInterface` function. |
13 | 35 | foreign import data Interface :: * |
14 | 36 |
|
| 37 | +-- | The effect of interacting with a stream via an `Interface` |
| 38 | +foreign import data READLINE :: ! |
| 39 | + |
| 40 | +foreign import createInterfaceImpl :: forall eff. |
| 41 | + Foreign |
| 42 | + -> Eff ( readline :: READLINE | 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 eff |
| 70 | + -> Options InterfaceOptions |
| 71 | + -> Eff ( readline :: READLINE |
| 72 | + | eff ) |
| 73 | + Interface |
| 74 | +createInterface input opts = createInterfaceImpl |
| 75 | + $ options $ opts |
| 76 | + <> opt "input" := input |
23 | 77 |
|
24 | | --- | Set the current line handler function. |
25 | | -foreign import setLineHandler :: forall eff a. Interface -> LineHandler eff a -> Eff (console :: CONSOLE | eff) Interface |
| 78 | +-- | Create an interface with the specified completion function. |
| 79 | +createConsoleInterface :: forall eff. |
| 80 | + Completer ( readline :: READLINE |
| 81 | + , console :: CONSOLE |
| 82 | + , err :: EXCEPTION |
| 83 | + | eff ) |
| 84 | + -> Eff ( readline :: READLINE |
| 85 | + , console :: CONSOLE |
| 86 | + , err :: EXCEPTION |
| 87 | + | eff ) |
| 88 | + Interface |
| 89 | +createConsoleInterface compl = createInterface stdin $ output := stdout |
| 90 | + <> completer := compl |
| 91 | + |
| 92 | +-- | A completion function which offers no completions. |
| 93 | +noCompletion :: forall eff. Completer eff |
| 94 | +noCompletion s = return { completions: [], matched: s } |
26 | 95 |
|
27 | 96 | -- | Prompt the user for input on the specified `Interface`. |
28 | | -foreign import prompt :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface |
| 97 | +foreign import prompt :: forall eff. |
| 98 | + Interface |
| 99 | + -> Eff ( readline :: READLINE | eff ) Interface |
29 | 100 |
|
30 | 101 | -- | 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 |
| 102 | +foreign import setPrompt :: forall eff. |
| 103 | + String |
| 104 | + -> Int |
| 105 | + -> Interface |
| 106 | + -> Eff ( readline :: READLINE | eff ) Interface |
35 | 107 |
|
36 | 108 | -- | Close the specified `Interface`. |
37 | | -foreign import close :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface |
| 109 | +foreign import close :: forall eff. |
| 110 | + Interface |
| 111 | + -> Eff ( readline :: READLINE | eff ) Interface |
38 | 112 |
|
39 | | --- | A completion function which offers no completions. |
40 | | -noCompletion :: forall eff. Completer eff |
41 | | -noCompletion s = return { completions: [], matched: s } |
| 113 | +-- | A function which handles each line of input. |
| 114 | +type LineHandler eff a = String -> Eff eff a |
42 | 115 |
|
| 116 | +-- | Set the current line handler function. |
| 117 | +foreign import setLineHandler :: forall eff a. |
| 118 | + Interface |
| 119 | + -> LineHandler ( readline :: READLINE | eff ) a |
| 120 | + -> Eff ( readline :: READLINE | eff ) Interface |
0 commit comments