Skip to content

Commit 6214ab6

Browse files
committed
Adjusted Readline to be actually usable.
Moved Readline methods that were incorrectly on the interface to top level. Added example code and codegen test for Readline.
1 parent a63354b commit 6214ab6

File tree

5 files changed

+136
-36
lines changed

5 files changed

+136
-36
lines changed

examples/ReadFileByLine.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
open NodeJs
2+
3+
// example from the NodeJS docs
4+
let rl = Readline.make(
5+
Readline.interfaceOptions(~input=Fs.createReadStream("sample.txt"), ~crlfDelay=infinity, ()),
6+
)
7+
8+
rl
9+
->Readline.Interface.on(Event.fromString("line"), line => {
10+
Js.log(`Received: ${line}`)
11+
})
12+
->ignore

lib/js/examples/ReadFileByLine.bs.js

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/js/test/codegen/Readline.codegen.bs.js

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Readline.res

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
module Interface = {
22
type t
3-
type interfaceOptions
4-
@obj
5-
external interfaceOptions: (
6-
~input: Stream.Readable.subtype<Buffer.t, 'rtype>,
7-
~output: Stream.Writable.subtype<Buffer.t, 'wtype>=?,
8-
~completer: (string, (string, (array<string>, string)) => unit) => unit=?,
9-
~terminal: bool=?,
10-
~historySize: int=?,
11-
~prompt: string=?,
12-
~crlfDelay: int=?,
13-
~removeHistoryDuplicates: bool=?,
14-
~escapeCodeTimeout: int=?,
15-
unit,
16-
) => interfaceOptions = ""
17-
@send external make: (t, interfaceOptions) => t = "createInterface"
183
@send external close: t => unit = "close"
194
@send external pause: t => unit = "pause"
205
@send external prompt: (t, Js.nullable<bool>) => unit = "prompt"
@@ -42,25 +27,59 @@ module Interface = {
4227
@get @return(nullable) external line: t => option<string> = "line"
4328
@get @return(nullable)
4429
external cursor: t => option<int> = "cursor"
45-
@send
46-
external clearLine: (t, Stream.Writable.subtype<Buffer.t, 'ty>, int) => bool = "clearLine"
47-
@send
48-
external clearScreenDown: (t, Stream.Writable.subtype<Buffer.t, 'ty>, unit => unit) => bool =
49-
"clearScreenDown"
50-
@send
51-
external cursorTo: (
52-
t,
53-
Stream.Writable.subtype<Buffer.t, 'ty>,
54-
int,
55-
Js.Undefined.t<int>,
56-
Js.Undefined.t<unit => unit>,
57-
) => bool = "cursorTo"
58-
@send
59-
external moveCursor: (
60-
t,
61-
Stream.Writable.subtype<Buffer.t, 'ty>,
62-
int,
63-
int,
64-
Js.Undefined.t<unit => unit>,
65-
) => bool = "moveCursor"
30+
31+
include EventEmitter.Impl({
32+
type t = t
33+
})
34+
}
35+
36+
module Events = {
37+
open Interface
38+
let close: (t, unit => unit) => t = (rl, f) => rl->on(Event.fromString("close"), f)
39+
let line: (t, string => unit) => t = (rl, f) => rl->on(Event.fromString("line"), f)
40+
let history: (t, array<string> => unit) => t = (rl, f) => rl->on(Event.fromString("history"), f)
6641
}
42+
43+
type interfaceOptions
44+
@obj
45+
external interfaceOptions: (
46+
~input: Stream.Readable.subtype<Buffer.t, 'rtype>,
47+
~output: Stream.Writable.subtype<Buffer.t, 'wtype>=?,
48+
~completer: (string, (string, (array<string>, string)) => unit) => unit=?,
49+
~terminal: bool=?,
50+
~historySize: int=?,
51+
~prompt: string=?,
52+
~crlfDelay: float=?,
53+
~removeHistoryDuplicates: bool=?,
54+
~escapeCodeTimeout: int=?,
55+
unit,
56+
) => interfaceOptions = ""
57+
@module("readline") external make: interfaceOptions => Interface.t = "createInterface"
58+
59+
@module("readline")
60+
external clearLine: (Stream.Writable.subtype<Buffer.t, 'ty>, int, ~callback: unit => unit) => bool =
61+
"clearLine"
62+
63+
@module("readline")
64+
external clearScreenDown: (
65+
Stream.Writable.subtype<Buffer.t, 'ty>,
66+
~callback: unit => unit,
67+
) => bool = "clearScreenDown"
68+
69+
@module("readline")
70+
external cursorTo: (
71+
Stream.Writable.subtype<Buffer.t, 'ty>,
72+
~x: int,
73+
~y: int=?,
74+
~callback: unit => unit=?,
75+
@ignore unit,
76+
) => bool = "cursorTo"
77+
78+
@module("readline")
79+
external moveCursor: (
80+
Stream.Writable.subtype<Buffer.t, 'ty>,
81+
~dx: int,
82+
~dy: int,
83+
~callback: unit => unit=?,
84+
@ignore unit,
85+
) => bool = "moveCursor"

test/codegen/Readline.codegen.res

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// codegen verification only, not intended to run
2+
Readline.clearLine(Process.stdout(Process.process), 1, ~callback=() =>
3+
Js.log("line cleared")
4+
)->ignore
5+
Readline.clearScreenDown(Process.stdout(Process.process), ~callback=() =>
6+
Js.log("screen cleared")
7+
)->ignore
8+
9+
Readline.cursorTo(
10+
Process.stdout(Process.process),
11+
~x=1,
12+
~y=2,
13+
~callback=() => Js.log("cursor to"),
14+
(),
15+
)->ignore
16+
17+
Readline.moveCursor(
18+
Process.stdout(Process.process),
19+
~dx=1,
20+
~dy=2,
21+
~callback=() => Js.log("cursor moved"),
22+
(),
23+
)->ignore

0 commit comments

Comments
 (0)