Skip to content

Commit 8b53591

Browse files
committed
Updates for 0.7
1 parent b3111cd commit 8b53591

File tree

9 files changed

+182
-141
lines changed

9 files changed

+182
-141
lines changed

Gruntfile.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

README.md

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
1-
# Module Documentation
1+
# purescript-node-readline
22

3-
## Module Node.ReadLine
3+
A low-level PureScript interface to the Node `readline` API.
44

5-
### Types
5+
## Installation
66

7-
type Completer eff = String -> Eff eff { matched :: String, completions :: [String] }
7+
```
8+
bower install purescript-readline
9+
```
810

9-
data Console :: !
11+
## Module documentation
1012

11-
data InputStream :: *
12-
13-
data Interface :: *
14-
15-
type LineHandler eff a = String -> Eff (console :: Console | eff) a
16-
17-
data OutputStream :: *
18-
19-
20-
### Values
21-
22-
createInterface :: forall eff. InputStream -> OutputStream -> Completer eff -> Eff (console :: Console | eff) Interface
23-
24-
noCompletion :: forall eff. Completer eff
25-
26-
process :: { stdin :: InputStream, stdout :: OutputStream, stderr :: OutputStream }
27-
28-
prompt :: forall eff. Interface -> Eff (console :: Console | eff) Interface
29-
30-
setLineHandler :: forall eff a. LineHandler eff a -> Interface -> Eff (console :: Console | eff) Interface
31-
32-
setPrompt :: forall eff. String -> Number -> Interface -> Eff (console :: Console | eff) Interface
13+
- [Node.ReadLine](docs/Node/ReadLine.md)

bower.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"keywords": [
55
"purescript"
66
],
7+
"repository": {
8+
"type": "git",
9+
"url": "git@github.com:purescript-node/purescript-node-readline.git"
10+
},
711
"license": "MIT",
812
"ignore": [
913
"**/.*",
@@ -15,5 +19,8 @@
1519
"bower.json",
1620
"Gruntfile.js",
1721
"package.json"
18-
]
22+
],
23+
"dependencies": {
24+
"purescript-console": "^0.1.0"
25+
}
1926
}

docs/Node/ReadLine.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Module Node.ReadLine
2+
3+
This module provides a binding to the Node `readline` API.
4+
5+
#### `Interface`
6+
7+
``` purescript
8+
data Interface :: *
9+
```
10+
11+
A handle to a console interface.
12+
13+
A handle can be created with the `createInterface` function.
14+
15+
#### `Completer`
16+
17+
``` purescript
18+
type Completer eff = String -> Eff (console :: CONSOLE | eff) { completions :: Array String, matched :: String }
19+
```
20+
21+
A function which performs tab completion.
22+
23+
This function takes the partial command as input, and returns a collection of
24+
completions, as well as the matched portion of the input string.
25+
26+
#### `LineHandler`
27+
28+
``` purescript
29+
type LineHandler eff a = String -> Eff (console :: CONSOLE | eff) a
30+
```
31+
32+
A function which handles input from the user.
33+
34+
#### `setLineHandler`
35+
36+
``` purescript
37+
setLineHandler :: forall eff a. LineHandler eff a -> Interface -> Eff (console :: CONSOLE | eff) Interface
38+
```
39+
40+
Set the current line handler function.
41+
42+
#### `prompt`
43+
44+
``` purescript
45+
prompt :: forall eff. Interface -> Eff (console :: CONSOLE | eff) Interface
46+
```
47+
48+
Prompt the user for input on the specified `Interface`.
49+
50+
#### `setPrompt`
51+
52+
``` purescript
53+
setPrompt :: forall eff. String -> Int -> Interface -> Eff (console :: CONSOLE | eff) Interface
54+
```
55+
56+
Set the prompt.
57+
58+
#### `createInterface`
59+
60+
``` purescript
61+
createInterface :: forall eff. Completer eff -> Eff (console :: CONSOLE | eff) Interface
62+
```
63+
64+
Create an interface with the specified completion function.
65+
66+
#### `noCompletion`
67+
68+
``` purescript
69+
noCompletion :: forall eff. Completer eff
70+
```
71+
72+
A completion function which offers no completions.
73+
74+

examples/Examples.purs.hs

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Node/ReadLine.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* global exports */
2+
"use strict";
3+
4+
// module Node.ReadLine
5+
6+
exports.setLineHandler = function(callback) {
7+
return function(readline) {
8+
return function() {
9+
readline.removeAllListeners('line');
10+
readline.on('line', function(line) {
11+
callback(line)();
12+
});
13+
return readline;
14+
};
15+
};
16+
};
17+
18+
exports.prompt = function(readline) {
19+
return function() {
20+
readline.prompt();
21+
return readline;
22+
};
23+
};
24+
25+
exports.setPrompt = function(prompt) {
26+
return function(length) {
27+
return function(readline) {
28+
return function() {
29+
readline.setPrompt(prompt, length);
30+
return readline;
31+
};
32+
};
33+
};
34+
};
35+
36+
exports.createInterface = function(completer) {
37+
return function() {
38+
var readline = require('readline');
39+
return readline.createInterface({
40+
input: process.stdin,
41+
output: process.stdout,
42+
completer: function(line) {
43+
var res = completer(line)();
44+
return [res.completions, res.suffix];
45+
}
46+
});
47+
};
48+
};

src/Node/ReadLine.purs

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,39 @@
1+
-- | This module provides a binding to the Node `readline` API.
2+
13
module Node.ReadLine where
24

3-
import Control.Monad.Eff
5+
import Prelude (return)
46

5-
foreign import data Console :: !
7+
import Control.Monad.Eff
8+
import Control.Monad.Eff.Console
69

10+
-- | A handle to a console interface.
11+
-- |
12+
-- | A handle can be created with the `createInterface` function.
713
foreign import data Interface :: *
814

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 }
1620

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
1823

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
3126

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
3929

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
5132

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
7035

36+
-- | A completion function which offers no completions.
7137
noCompletion :: forall eff. Completer eff
7238
noCompletion s = return { completions: [], matched: s }
7339

test/Main.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Test.Main where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff
6+
import Control.Monad.Eff.Console
7+
8+
import Node.ReadLine
9+
10+
main = do
11+
interface <- createInterface noCompletion
12+
13+
let
14+
lineHandler s = do
15+
log $ "You typed: " ++ s
16+
prompt interface
17+
18+
setPrompt "> " 2 interface
19+
prompt interface
20+
setLineHandler lineHandler interface

0 commit comments

Comments
 (0)