|
| 1 | +-- | This module provides a binding to the Node `readline` API. |
| 2 | + |
1 | 3 | module Node.ReadLine where |
2 | 4 |
|
3 | | -import Control.Monad.Eff |
| 5 | +import Prelude (return) |
4 | 6 |
|
5 | | -foreign import data Console :: ! |
| 7 | +import Control.Monad.Eff |
| 8 | +import Control.Monad.Eff.Console |
6 | 9 |
|
| 10 | +-- | A handle to a console interface. |
| 11 | +-- | |
| 12 | +-- | A handle can be created with the `createInterface` function. |
7 | 13 | foreign import data Interface :: * |
8 | 14 |
|
9 | | -foreign import data InputStream :: * |
10 | | - |
11 | | -foreign import data OutputStream :: * |
12 | | - |
13 | | -foreign import process :: { stderr :: OutputStream, stdout :: OutputStream, stdin :: InputStream } |
14 | | - |
15 | | -type Completer eff = String -> Eff eff { completions :: [String], matched :: String } |
| 15 | +-- | A function which performs tab completion. |
| 16 | +-- | |
| 17 | +-- | This function takes the partial command as input, and returns a collection of |
| 18 | +-- | 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 } |
16 | 20 |
|
17 | | -type LineHandler eff a = String -> Eff (console :: Console | eff) a |
| 21 | +-- | A function which handles input from the user. |
| 22 | +type LineHandler eff a = String -> Eff (console :: CONSOLE | eff) a |
18 | 23 |
|
19 | | -foreign import setLineHandler |
20 | | - "function setLineHandler(callback) {\ |
21 | | - \ return function(readline) {\ |
22 | | - \ return function() {\ |
23 | | - \ readline.removeAllListeners('line');\ |
24 | | - \ readline.on('line', function (line) {\ |
25 | | - \ callback(line)();\ |
26 | | - \ });\ |
27 | | - \ return readline;\ |
28 | | - \ };\ |
29 | | - \ };\ |
30 | | - \};" :: forall eff a. LineHandler eff a -> Interface -> Eff (console :: Console | eff) Interface |
| 24 | +-- | Set the current line handler function. |
| 25 | +foreign import setLineHandler :: forall eff a. LineHandler eff a -> Interface -> Eff (console :: CONSOLE | eff) Interface |
31 | 26 |
|
32 | | -foreign import prompt |
33 | | - "function prompt(readline) {\ |
34 | | - \ return function() {\ |
35 | | - \ readline.prompt();\ |
36 | | - \ return readline;\ |
37 | | - \ };\ |
38 | | - \};" :: forall eff. Interface -> Eff (console :: Console | eff) Interface |
| 27 | +-- | Prompt the user for input on the specified `Interface`. |
| 28 | +foreign import prompt :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface |
39 | 29 |
|
40 | | -foreign import setPrompt |
41 | | - "function setPrompt(prompt) {\ |
42 | | - \ return function(length) {\ |
43 | | - \ return function(readline) {\ |
44 | | - \ return function() {\ |
45 | | - \ readline.setPrompt(prompt, length);\ |
46 | | - \ return readline;\ |
47 | | - \ };\ |
48 | | - \ };\ |
49 | | - \ };\ |
50 | | - \}" :: forall eff. Prim.String -> Prim.Number -> Interface -> Eff (console :: Console | eff) Interface |
| 30 | +-- | Set the prompt. |
| 31 | +foreign import setPrompt :: forall eff. String -> Int -> Interface -> Eff (console :: CONSOLE | eff) Interface |
51 | 32 |
|
52 | | -foreign import createInterface |
53 | | - "function createInterface(input) {\ |
54 | | - \ return function(output) {\ |
55 | | - \ return function(completer) {\ |
56 | | - \ return function() {\ |
57 | | - \ var readline = require('readline');\ |
58 | | - \ return readline.createInterface({\ |
59 | | - \ input: input,\ |
60 | | - \ output: output,\ |
61 | | - \ completer: function(line) {\ |
62 | | - \ var res = completer(line)();\ |
63 | | - \ return [res.completions, res.suffix];\ |
64 | | - \ }\ |
65 | | - \ });\ |
66 | | - \ };\ |
67 | | - \ };\ |
68 | | - \ };\ |
69 | | - \}" :: forall eff. InputStream -> OutputStream -> Completer eff -> Eff (console :: Console | eff) Interface |
| 33 | +-- | Create an interface with the specified completion function. |
| 34 | +foreign import createInterface :: forall eff. Completer eff -> Eff (console :: CONSOLE | eff) Interface |
70 | 35 |
|
| 36 | +-- | A completion function which offers no completions. |
71 | 37 | noCompletion :: forall eff. Completer eff |
72 | 38 | noCompletion s = return { completions: [], matched: s } |
73 | 39 |
|
0 commit comments