Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit aecdc8b

Browse files
authored
Merge pull request #74 from exoego/repl
Overhaul repl and readline module
2 parents 6816d87 + 4101abf commit aecdc8b

File tree

7 files changed

+73
-43
lines changed

7 files changed

+73
-43
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ The following core Node.js modules (v8.7.0+) have been implemented:
3838
| [path](https://nodejs.org/api/path.html) | :heavy_check_mark: |
3939
| [process](https://nodejs.org/api/process.html) | :heavy_check_mark: |
4040
| [querystring](https://nodejs.org/api/querystring.html) | :heavy_check_mark: |
41-
| [readline](https://nodejs.org/api/readline.html) | |
42-
| [repl](https://nodejs.org/api/repl.html) | |
41+
| [readline](https://nodejs.org/api/readline.html) | :heavy_check_mark: |
42+
| [repl](https://nodejs.org/api/repl.html) | :heavy_check_mark: |
4343
| [stream](https://nodejs.org/api/stream.html) | |
4444
| [string-decoder](https://nodejs.org/api/string_decoder.html) | :heavy_check_mark: |
4545
| [timers](https://nodejs.org/api/timers.html) | :heavy_check_mark: |

app/current/src/main/scala/io/scalajs/nodejs/readline/Interface.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ trait Interface extends IEventEmitter {
5151
* If output is set to null or undefined when calling createInterface, nothing is displayed.
5252
* @example rl.question(query, callback)
5353
*/
54-
def question(query: String, callback: js.Function): Unit = js.native
54+
def question(query: String, callback: js.Function1[String, Any]): Unit = js.native
5555

5656
/**
5757
* Resumes the readline input stream.
@@ -71,7 +71,7 @@ trait Interface extends IEventEmitter {
7171
* This will also resume the input stream if it has been paused.
7272
* @example rl.write(data[, key])
7373
*/
74-
def write(data: String, key: js.Any): Unit = js.native
74+
def write(data: String, key: Key): Unit = js.native
7575

7676
/**
7777
* Writes data to output stream, unless output is set to null or undefined when calling createInterface.
@@ -81,8 +81,16 @@ trait Interface extends IEventEmitter {
8181
*/
8282
def write(data: String): Unit = js.native
8383

84+
// TODO: [Symbol.asyncIterator]()
8485
}
8586

87+
class Key(
88+
var ctrl: js.UndefOr[Boolean] = js.undefined,
89+
var meta: js.UndefOr[Boolean] = js.undefined,
90+
var shift: js.UndefOr[Boolean] = js.undefined,
91+
var name: js.UndefOr[String] = js.undefined
92+
) extends js.Object
93+
8694
/**
8795
* Readline Interface Companion
8896
*/

app/current/src/main/scala/io/scalajs/nodejs/readline/Readline.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.scalajs.nodejs.readline
22

33
import io.scalajs.nodejs.events.IEventEmitter
4+
import io.scalajs.nodejs.stream.{Readable, Writable}
45

56
import scala.scalajs.js
67
import scala.scalajs.js.annotation.JSImport
78

8-
/**
9-
* Readline allows reading of a stream (such as process.stdin) on a line-by-line basis.
9+
/**reading
10+
* Readline allows of a stream (such as process.stdin) on a line-by-line basis.
1011
* To use this module, do require('readline').
1112
* Note that once you've invoked this module, your Node.js program will not terminate until you've closed the interface.
1213
* @see https://nodejs.org/api/readline.html
@@ -23,13 +24,13 @@ trait Readline extends IEventEmitter {
2324
* </ul>
2425
* @example readline.clearLine(stream, dir)
2526
*/
26-
def clearLine(stream: js.Any, dir: Int): Unit = js.native
27+
def clearLine(stream: Writable, dir: Int, callback: js.Function = js.native): Boolean = js.native
2728

2829
/**
2930
* Clears the screen from the current position of the cursor down.
3031
* @example readline.clearScreenDown(stream)
3132
*/
32-
def clearScreenDown(stream: js.Any): Unit = js.native
33+
def clearScreenDown(stream: Writable, callback: js.Function = js.native): Boolean = js.native
3334

3435
/**
3536
* Creates a readline Interface instance.
@@ -41,13 +42,17 @@ trait Readline extends IEventEmitter {
4142
* Move cursor to the specified position in a given TTY stream.
4243
* @example readline.cursorTo(stream, x, y)
4344
*/
44-
def cursorTo(stream: js.Any, x: Int, y: Int): Unit = js.native
45+
def cursorTo(stream: Writable, x: Int, y: Int, callback: js.Function = js.native): Unit = js.native
46+
def cursorTo(stream: Writable, x: Int, callback: js.Function): Unit = js.native
47+
def cursorTo(stream: Writable, x: Int): Unit = js.native
48+
49+
def emitKeypressEvents(stream: Readable, interface: Interface = js.native): Unit = js.native
4550

4651
/**
4752
* Move cursor relative to it's current position in a given TTY stream.
4853
* @example readline.moveCursor(stream, dx, dy)
4954
*/
50-
def moveCursor(stream: js.Any, dx: Int, dy: Int): Unit = js.native
55+
def moveCursor(stream: Writable, dx: Int, dy: Int, callback: js.Function = js.native): Unit = js.native
5156

5257
}
5358

app/current/src/main/scala/io/scalajs/nodejs/readline/ReadlineOptions.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import scala.scalajs.js
77
/**
88
* Readline Options
99
*/
10-
class ReadlineOptions(var input: js.UndefOr[Readable] = js.undefined,
11-
var output: js.UndefOr[Writable] = js.undefined,
12-
var completer: js.UndefOr[js.Function] = js.undefined,
13-
var terminal: js.UndefOr[Boolean] = js.undefined,
14-
var historySize: js.UndefOr[Int] = js.undefined)
15-
extends js.Object
10+
class ReadlineOptions(
11+
var input: js.UndefOr[Readable] = js.undefined,
12+
var output: js.UndefOr[Writable] = js.undefined,
13+
var completer: js.UndefOr[js.Function] = js.undefined,
14+
var terminal: js.UndefOr[Boolean] = js.undefined,
15+
var historySize: js.UndefOr[Int] = js.undefined,
16+
var prompt: js.UndefOr[String] = js.undefined,
17+
var crlfDelay: js.UndefOr[Double] = js.undefined,
18+
var removeHistoryDuplicates: js.UndefOr[Boolean] = js.undefined,
19+
var escapeCodeTimeout: js.UndefOr[Double] = js.undefined
20+
) extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/repl/REPL.scala

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.scalajs.nodejs.repl
22

33
import io.scalajs.nodejs.events.IEventEmitter
4-
import io.scalajs.nodejs.net.Socket
4+
import io.scalajs.nodejs.stream
55

66
import scala.scalajs.js
77
import scala.scalajs.js.annotation.JSImport
@@ -13,32 +13,31 @@ import scala.scalajs.js.annotation.JSImport
1313
*/
1414
@js.native
1515
trait REPL extends IEventEmitter {
16-
var REPL_MODE_SLOPPY: String = js.native
17-
var REPL_MODE_STRICT: String = js.native
18-
var REPL_MODE_MAGIC: String = js.native
16+
var REPL_MODE_SLOPPY: js.Symbol = js.native
17+
var REPL_MODE_STRICT: js.Symbol = js.native
1918

20-
/**
21-
* @example repl.start([options])
22-
*/
23-
def start(options: REPLOptions): REPLServer = js.native
24-
25-
/**
26-
* @example repl.start([options])
27-
*/
28-
def start(prompt: String, socket: Socket): REPLServer = js.native
29-
30-
/**
31-
* @example repl.start([options])
32-
*/
33-
def start(prompt: String): REPLServer = js.native
34-
35-
/**
36-
* @example repl.start([options])
37-
*/
38-
def start(): REPLServer = js.native
19+
@deprecated("Use REPL_MODE_SLOPPY instead", "Node.js v6.0.0")
20+
var REPL_MODE_MAGIC: js.UndefOr[js.Symbol] = js.native
3921

22+
def start(options: StartOptions = js.native): REPLServer = js.native
23+
def start(prompt: String): REPLServer = js.native
4024
}
4125

26+
class StartOptions(
27+
var prompt: js.UndefOr[String] = js.undefined,
28+
var input: js.UndefOr[stream.Readable] = js.undefined,
29+
var output: js.UndefOr[stream.Writable] = js.undefined,
30+
var terminal: js.UndefOr[Boolean] = js.undefined,
31+
var eval: js.UndefOr[js.Function4[String, js.Object, String, js.Function, Any]] = js.undefined,
32+
var useColors: js.UndefOr[Boolean] = js.undefined,
33+
var useGlobal: js.UndefOr[Boolean] = js.undefined,
34+
var ignoreUndefined: js.UndefOr[Boolean] = js.undefined,
35+
var writer: js.UndefOr[js.Function1[js.Any, Any]] = js.undefined,
36+
var completer: js.UndefOr[js.Function] = js.undefined,
37+
var replMode: js.UndefOr[js.Symbol] = js.undefined,
38+
var breakEvalOnSigint: js.UndefOr[Boolean] = js.undefined
39+
) extends js.Object
40+
4241
/**
4342
* REPL Singleton
4443
*/

app/current/src/main/scala/io/scalajs/nodejs/repl/REPLServer.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.scalajs.nodejs.repl
22

3+
import com.thoughtworks.enableIf
34
import io.scalajs.nodejs.events.IEventEmitter
45
import io.scalajs.nodejs.readline.Interface
56

67
import scala.scalajs.js
8+
import scala.scalajs.js.|
79

810
/**
911
* REPL Server
@@ -16,6 +18,9 @@ trait REPLServer extends IEventEmitter with Interface {
1618
*/
1719
val context: REPLContext = js.native
1820

21+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
22+
def clearBufferedCommand(): Unit = js.native
23+
1924
/**
2025
* The replServer.defineCommand() method is used to add new .-prefixed commands to the REPL instance.
2126
* Such commands are invoked by typing a period (.) followed by the keyword. The cmd is either a Function
@@ -27,7 +32,7 @@ trait REPLServer extends IEventEmitter with Interface {
2732
* @param keyword The command keyword (without a leading . character).
2833
* @param cmd The function to invoke when the command is processed.
2934
*/
30-
def defineCommand(keyword: String, cmd: js.Function0[Any]): Unit = js.native
35+
def defineCommand(keyword: String, cmd: DefinedCommand | js.Function1[String, Any]): Unit = js.native
3136

3237
/**
3338
* The replServer.displayPrompt() method readies the REPL instance for input from the user, printing the
@@ -52,8 +57,16 @@ trait REPLServer extends IEventEmitter with Interface {
5257
*/
5358
def displayPrompt(): Unit = js.native
5459

60+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
61+
def setupHistory(historyPath: String, callback: js.Function2[io.scalajs.nodejs.Error, REPLServer, Any]): Unit =
62+
js.native
5563
}
5664

65+
class DefinedCommand(
66+
var action: js.Function1[String, Any],
67+
var help: js.UndefOr[String] = js.undefined
68+
) extends js.Object
69+
5770
/**
5871
* REPL Server Companion
5972
*/

app/current/src/main/scala/io/scalajs/nodejs/repl/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package object repl {
2525
* @param listener The listener callback
2626
*/
2727
@inline
28-
def onExit(listener: () => Any): server.type = server.on("exit", listener)
28+
def onExit(listener: () => Any): REPLServer = server.on("exit", listener)
2929

3030
/**
3131
* The 'reset' event is emitted when the REPL's context is reset. This occurs whenever the .clear command
@@ -35,7 +35,7 @@ package object repl {
3535
* @param listener The listener callback
3636
*/
3737
@inline
38-
def onReset(listener: REPLContext => Any): server.type = server.on("reset", listener)
38+
def onReset(listener: REPLContext => Any): REPLServer = server.on("reset", listener)
3939

4040
}
4141

@@ -47,7 +47,7 @@ package object repl {
4747
* <li>NODE_REPL_MODE</li>
4848
* </ul>
4949
*/
50-
final implicit class EnvironmentVariableOptions(val env: process.Environment) extends AnyVal {
50+
final implicit class EnvironmentVariableOptions(private val env: process.Environment) extends AnyVal {
5151

5252
/**
5353
* When a valid path is given, persistent REPL history will be saved to the specified file rather

0 commit comments

Comments
 (0)