Skip to content

Commit 54b8f8c

Browse files
committed
Initial add
1 parent 6d1eb68 commit 54b8f8c

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
js/

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: lib test
2+
3+
lib:
4+
mkdir -p js/System/
5+
psc src/System/ReadLine.purs.hs \
6+
-o js/System/ReadLine.js \
7+
-e js/System/ReadLine.e.purs.hs \
8+
--module System.ReadLine --tco --magic-do
9+
10+
test:
11+
psc src/System/ReadLine.purs.hs examples/Examples.purs.hs \
12+
-o js/Examples.js \
13+
--main --module Main --tco --magic-do

examples/Examples.purs.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Main where
2+
3+
import Prelude
4+
import Control.Monad
5+
import Control.Monad.Eff
6+
import System.ReadLine
7+
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
14+
15+
main = do
16+
interface <- createInterface process.stdin process.stdout completion
17+
setPrompt "? " 2 interface
18+
prompt interface
19+
setLineHandler lineHandler interface

src/System/ReadLine.purs.hs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module System.ReadLine where
2+
3+
import Data.Tuple
4+
import Control.Monad.Eff
5+
6+
foreign import data Console :: !
7+
8+
foreign import data Interface :: *
9+
10+
foreign import data InputStream :: *
11+
12+
foreign import data OutputStream :: *
13+
14+
foreign import process :: { stderr :: OutputStream, stdout :: OutputStream, stdin :: System.ReadLine.InputStream }
15+
16+
type Completer eff = String -> Eff eff (Tuple [String] String)
17+
18+
type LineHandler eff = String -> Eff eff {}
19+
20+
foreign import setLineHandler
21+
"function setLineHandler(callback) {\
22+
\ return function(readline) {\
23+
\ return function() {\
24+
\ readline.on('line', function (line) {\
25+
\ callback(line)();\
26+
\ });\
27+
\ return readline;\
28+
\ };\
29+
\ };\
30+
\};" :: forall eff. LineHandler eff -> Interface -> Control.Monad.Eff.Eff (console :: System.ReadLine.Console | eff) System.ReadLine.Interface
31+
32+
foreign import prompt
33+
"function prompt(readline) {\
34+
\ return function() {\
35+
\ readline.prompt();\
36+
\ return readline;\
37+
\ };\
38+
\};" :: forall eff. Interface -> Control.Monad.Eff.Eff (console :: Console | eff) System.ReadLine.Interface
39+
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 -> Control.Monad.Eff.Eff (console :: Console | eff) System.ReadLine.Interface
51+
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 completions = completer(line)();\
63+
\ return completions.values;\
64+
\ }\
65+
\ });\
66+
\ };\
67+
\ };\
68+
\ };\
69+
\}" :: forall eff. InputStream -> OutputStream -> System.ReadLine.Completer eff -> Control.Monad.Eff.Eff (console :: System.ReadLine.Console | eff) System.ReadLine.Interface
70+
71+

0 commit comments

Comments
 (0)