Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 110 additions & 56 deletions src/js/node/Readline.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,108 +27,162 @@ import js.node.stream.Readable.IReadable;
import js.node.stream.Writable.IWritable;
import js.node.readline.*;

typedef ReadlineOptions = {
/**
The readline module provides an interface for reading data from a `Readable` stream (such as `process.stdin`) one
line at a time.

@see https://nodejs.org/api/readline.html#readline_readline
**/
@:jsRequire("readline")
extern class Readline {
/**
the readable stream to listen to
The `readline.clearLine()` method Clears current line of given `TTY` stream
in a specified direction identified by `dir`.

@see https://nodejs.org/api/readline.html#readline_readline_clearline_stream_dir_callback
**/
var input:IReadable;
static function clearLine(stream:IWritable, dir:ClearLineDirection, ?callback:Void->Void):Bool;

/**
the writable stream to write readline data to
The `readline.clearScreenDown()` method clears the given `TTY` stream
from the current position of the cursor down.

@see https://nodejs.org/api/readline.html#readline_readline_clearscreendown_stream_callback
**/
@:optional var output:IWritable;
static function clearScreenDown(stream:IWritable, ?callback:Void->Void):Bool;

/**
an optional function that is used for Tab autocompletion.
The `readline.createInterface()` method creates a new `readline.Interface` instance.

The `completer` function is given the current line entered by the user,
and is supposed to return an Array with 2 entries:
* An Array with matching entries for the completion.
* The substring that was used for the matching.
Which ends up looking something like: [[substr1, substr2, ...], originalsubstring].
@see https://nodejs.org/api/readline.html#readline_readline_createinterface_options
**/
@:optional var completer:ReadlineCompleterCallback;
@:overload(function(input:IReadable, ?output:IWritable, ?completer:ReadlineCompleterCallback, ?terminal:Bool):Interface {})
static function createInterface(options:ReadlineOptions):Interface;

/**
pass true if the input and output streams should be treated like a TTY,
and have ANSI/VT100 escape codes written to it.
The `readline.cursorTo()` method moves cursor to the specified position in a given `TTY` `stream`.

Defaults to checking isTTY on the output stream upon instantiation.
@see https://nodejs.org/api/readline.html#readline_readline_cursorto_stream_x_y_callback
**/
@:optional var terminal:Bool;
static function cursorTo(stream:IWritable, x:Int, ?y:Int, ?callback:Void->Void):Bool;

/**
maximum number of history lines retained.
Defaults to 30.
The `readline.emitKeypressEvents()` method causes the given `Readable` stream to begin emitting `'keypress'`
events corresponding to received input.

@see https://nodejs.org/api/readline.html#readline_readline_emitkeypressevents_stream_interface
**/
@:optional var historySize:Int;
static function emitKeypressEvents(stream:IReadable, ?iface:Interface):Void;

/**
the prompt string to use.
Default: '> '
The `readline.moveCursor()` method moves the cursor relative to its current position in a given `TTY` `stream`.

@see https://nodejs.org/api/readline.html#readline_readline_movecursor_stream_dx_dy_callback
**/
@:optional var prompt:String;
static function moveCursor(stream:IWritable, dx:Int, dy:Int, ?callback:Void->Void):Bool;
}

typedef ReadlineCompleterCallback = #if (haxe_ver >= 4) (line : String) -> Array<EitherType<Array<String>, String>>; #else String->
Array<EitherType<Array<String>, String>>; #end

/**
Enumeration of possible directions for `Readline.clearLine`
Options object used by `Readline.createInterface`.

@see https://nodejs.org/api/readline.html#readline_readline_createinterface_options
**/
@:enum abstract ClearLineDirection(Int) from Int to Int {
typedef ReadlineOptions = {
/**
to the left from cursor
The `Readable` stream to listen to.
**/
var Left = -1;
var input:IReadable;

/**
to the right from cursor
The `Writable` stream to write readline data to.
**/
var Right = 1;
@:optional var output:IWritable;

/**
the entire line
An optional function used for Tab autocompletion.

@see https://nodejs.org/api/readline.html#readline_use_of_the_completer_function
**/
var EntireLine = 0;
}
@:optional var completer:ReadlineCompleterCallback;

/**
Readline allows reading of a stream (such as process.stdin) on a line-by-line basis.
/**
`true` if the `input` and `output` streams should be treated like a TTY, and have ANSI/VT100 escape codes
written to it.

Default: checking `isTTY` on the `output` stream upon instantiation.
**/
@:optional var terminal:Bool;

Note that once you've invoked this module, your node program will not terminate until you've closed the interface.
**/
@:jsRequire("readline")
extern class Readline {
/**
Creates a readline Interface instance.
`createInterface` is commonly used with process.stdin and process.stdout in order to accept user input.
Once you have a readline instance, you most commonly listen for the "line" event.
Maximum number of history lines retained.
To disable the history set this value to `0`.
This option makes sense only if `terminal` is set to `true` by the user or by an internal `output` check,
otherwise the history caching mechanism is not initialized at all.

If terminal is `true` for this instance then the output stream will get the best compatibility if it defines
an `columns` property, and fires a "resize" event on the output if/when the columns ever change
(process.stdout does this automatically when it is a TTY).
Default: `30`.
**/
@:overload(function(input:IReadable, ?output:IWritable, ?completer:ReadlineCompleterCallback, ?terminal:Bool):Interface {})
static function createInterface(options:ReadlineOptions):Interface;
@:optional var historySize:Int;

/**
The prompt string to use.

Default: `'> '`.
**/
@:optional var prompt:String;

/**
If the delay between `\r` and `\n` exceeds `crlfDelay` milliseconds, both `\r` and `\n` will be treated as
separate end-of-line input.
`crlfDelay` will be coerced to a number no less than `100`.
It can be set to `Infinity`, in which case `\r` followed by `\n` will always be considered a single newline
(which may be reasonable for reading files with `\r\n` line delimiter).

Default: `100`.
**/
@:optional var crlfDelay:Int;

/**
If `true`, when a new input line added to the history list duplicates an older one, this removes the older line
from the list.

Default: `false`.
**/
@:optional var removeHistoryDuplicates:Bool;

/**
Move cursor to the specified position in a given TTY stream.
The duration `readline` will wait for a character (when reading an ambiguous key sequence in milliseconds one
that can both form a complete key sequence using the input read so far and can take additional input to complete
a longer key sequence).

Default: `500`.
**/
static function cursorTo(stream:IWritable, x:Int, y:Int):Void;
@:optional var escapeCodeTimeout:Int;
}

#if haxe4
typedef ReadlineCompleterCallback = (line:String) -> Array<EitherType<Array<String>, String>>;
#else
typedef ReadlineCompleterCallback = String->Array<EitherType<Array<String>, String>>;
#end

/**
Enumeration of possible directions for `Readline.clearLine`.

@see https://nodejs.org/api/readline.html#readline_readline_clearline_stream_dir_callback
**/
@:enum abstract ClearLineDirection(Int) from Int to Int {
/**
Move cursor relative to it's current position in a given TTY stream.
to the left from cursor.
**/
static function moveCursor(stream:IWritable, dx:Int, dy:Int):Void;
var Left = -1;

/**
Clears current line of given TTY stream in a specified direction.
to the right from cursor.
**/
static function clearLine(stream:IWritable, dir:ClearLineDirection):Void;
var Right = 1;

/**
Clears the screen from the current position of the cursor down.
the entire line.
**/
static function clearScreenDown(stream:IWritable):Void;
var EntireLine = 0;
}
Loading