import {Cursor} from '@kittikjs/cursor/src/Cursor.js'
Cursor
Cursor implements low-level API to terminal control codes.
Static Method Summary
Static Public Methods | ||
public static |
Wrapper around |
|
public static |
Get height of TTY. |
|
public static |
getTTYSize(): {width: Number, height: Number} Get TTY sizes. |
|
public static |
Get width of TTY. |
Constructor Summary
Public Constructor | ||
public |
constructor(stdout: Array<Stream.Transform>, stdin: Array<Stream.Transform>) By default, creates simple cursor that writes direct to |
Method Summary
Public Methods | ||
public |
background(): Cursor Set the background color. |
|
public |
Destroy the cursor. |
|
public |
Move the cursor down. |
|
public |
Erase a defined region. |
|
public |
Fill the specified region with symbol. |
|
public |
foreground(): Cursor Set the foreground color. |
|
public |
Get the cursor position in absolute coordinates. |
|
public |
Set the cursor invisible. |
|
public |
Move the cursor left. |
|
public |
Move the cursor by the relative coordinates starting from the current position of cursor. |
|
public |
Removes all listeners or specified listener from the event. |
|
public |
Sets a listener on cursor stream. |
|
public |
Resets the entire screen. |
|
public |
Move the cursor right. |
|
public |
setPosition(x: Number, y: Number): Cursor Set the cursor position by absolute coordinates. |
|
public |
Set the cursor visible. |
|
public |
Move the cursor up. |
|
public |
Write to the stream. |
Static Public Methods
public static create(args: ...*): Cursor source
Wrapper around new Cursor()
.
Params:
Name | Type | Attribute | Description |
args | ...* |
Public Constructors
public constructor(stdout: Array<Stream.Transform>, stdin: Array<Stream.Transform>) source
By default, creates simple cursor that writes direct to stdout
.
If you want to work with other streams, you can pass custom stdout
and stdin
streams in.
It's useful when you want to chain few streams before pipe it into the cursor or want to modify what cursor pipes to stdout
.
Example:
import { Cursor } from './Cursor';
// Creates simple cursor that renders direct to `process.stdout`
let cursor = new Cursor();
// Creates cursor that takes data from `stdin` and pipes to cursor.
// Afterwards to some Transform stream and `stdout`.
let cursor = new Cursor([new MyTransformStream(), process.stdout], [process.stdin]);
Public Methods
public background(): Cursor source
Set the background color. This color is used for filling the whole cell in the TTY.
Example:
import { Cursor, COLORS } from './src/Cursor';
let cursor = new Cursor();
cursor.background(COLORS.YELLOW);
public down(y: Number): Cursor source
Move the cursor down.
Params:
Name | Type | Attribute | Description |
y | Number |
|
Rows count |
public erase(): Cursor source
Erase a defined region.
Example:
import { Cursor, ERASE_REGIONS } from './src/Cursor';
let cursor = new Cursor();
cursor.erase(ERASE_REGIONS.CURRENT_LINE); // Erases current line
public fill(options: Object): Cursor source
Fill the specified region with symbol. By default this symbol is whitespace but you can change it and fill with another symbol.
Example:
let cursor = new Cursor();
// Renders the rectangle at top of TTY
cursor.fill({x1: 0, y1: 0, x2: Cursor.getTTYWidth(), y2: 4, background: COLORS.YELLOW});
public foreground(): Cursor source
Set the foreground color. This color is used when text is rendering.
Example:
import { Cursor, COLORS } from './src/Cursor';
let cursor = new Cursor();
cursor.foreground(COLORS.BLACK);
public getPosition(): Promise source
Get the cursor position in absolute coordinates.
Example:
let cursor = new Cursor();
// Old-fashioned way
cursor.getPosition().then(position => {x: position.x, y: position.y});
// In async\await style
async function myMagicFunction() {
let {x, y} = await cursor.getPosition();
doSomethingWithPosition(x, y);
}
public left(x: Number): Cursor source
Move the cursor left.
Params:
Name | Type | Attribute | Description |
x | Number |
|
Columns count |
public move(x: Number, y: Number): Cursor source
Move the cursor by the relative coordinates starting from the current position of cursor.
public off(event: String, handler: Function): Cursor source
Removes all listeners or specified listener from the event.
If handler is not defined then removes all listeners from the specified event.
public reset(): Cursor source
Resets the entire screen. It's not the same as <span><a href="class/src/Cursor.js~Cursor.html#instance-method-erase">Cursor.erase</a></span>. reset() resets the TTY settings to default.
public right(x: Number): Cursor source
Move the cursor right.
Params:
Name | Type | Attribute | Description |
x | Number |
|
Columns count |
public setPosition(x: Number, y: Number): Cursor source
Set the cursor position by absolute coordinates.