Skip to content

Commit 06b1bce

Browse files
committed
Simplify some type signatures. Provide default completion. Better handling of events.
1 parent 460a4c8 commit 06b1bce

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Types
66

7-
type Completer eff = String -> Eff eff (Tuple [String] String)
7+
type Completer eff = String -> Eff eff { matched :: String, completions :: [String] }
88

99
data Console :: !
1010

@@ -21,6 +21,8 @@
2121

2222
createInterface :: forall eff. InputStream -> OutputStream -> Completer eff -> Eff (console :: Console | eff) Interface
2323

24+
noCompletion :: forall eff. Completer eff
25+
2426
process :: { stdin :: InputStream, stdout :: OutputStream, stderr :: OutputStream }
2527

2628
prompt :: forall eff. Interface -> Eff (console :: Console | eff) Interface

bower.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@
1515
"bower.json",
1616
"Gruntfile.js",
1717
"package.json"
18-
],
19-
"dependencies": {
20-
"purescript-tuples": "*"
21-
}
18+
]
2219
}

examples/Examples.purs.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
module Main where
22

3-
import Prelude
4-
import Control.Monad
53
import Control.Monad.Eff
64
import Node.ReadLine
75
import Debug.Trace
8-
import Data.Tuple
9-
10-
completion :: forall eff. Completer eff
11-
completion s = return $ Tuple [] s
12-
13-
lineHandler s = trace $ "You typed: " ++ s
146

157
main = do
16-
interface <- createInterface process.stdin process.stdout completion
8+
interface <- createInterface process.stdin process.stdout noCompletion
9+
10+
let
11+
lineHandler s = do
12+
trace $ "You typed: " ++ s
13+
prompt interface
14+
1715
setPrompt "? " 2 interface
1816
prompt interface
1917
setLineHandler lineHandler interface

src/Node/ReadLine.purs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Node.ReadLine where
22

3-
import Data.Tuple
43
import Control.Monad.Eff
54

65
foreign import data Console :: !
@@ -13,21 +12,22 @@ foreign import data OutputStream :: *
1312

1413
foreign import process :: { stderr :: OutputStream, stdout :: OutputStream, stdin :: InputStream }
1514

16-
type Completer eff = String -> Eff eff (Tuple [String] String)
15+
type Completer eff = String -> Eff eff { completions :: [String], matched :: String }
1716

18-
type LineHandler eff = String -> Eff eff Unit
17+
type LineHandler eff a = String -> Eff eff a
1918

2019
foreign import setLineHandler
2120
"function setLineHandler(callback) {\
2221
\ return function(readline) {\
2322
\ return function() {\
23+
\ readline.removeAllListeners('line');\
2424
\ readline.on('line', function (line) {\
2525
\ callback(line)();\
2626
\ });\
2727
\ return readline;\
2828
\ };\
2929
\ };\
30-
\};" :: forall eff. LineHandler eff -> Interface -> Eff (console :: Console | eff) Interface
30+
\};" :: forall eff a. LineHandler eff a -> Interface -> Eff (console :: Console | eff) Interface
3131

3232
foreign import prompt
3333
"function prompt(readline) {\
@@ -59,13 +59,15 @@ foreign import createInterface
5959
\ input: input,\
6060
\ output: output,\
6161
\ completer: function(line) {\
62-
\ var completions = completer(line)();\
63-
\ return completions.values;\
62+
\ var res = completer(line)();\
63+
\ return [res.completions, res.suffix];\
6464
\ }\
6565
\ });\
6666
\ };\
6767
\ };\
6868
\ };\
6969
\}" :: forall eff. InputStream -> OutputStream -> Completer eff -> Eff (console :: Console | eff) Interface
7070

71+
noCompletion :: forall eff. Completer eff
72+
noCompletion s = return { completions: [], matched: s }
7173

0 commit comments

Comments
 (0)