Home Reference Source Repository
import {Cursor} from '@kittikjs/cursor/src/Cursor.js'
public class | version 1.0.0 | since 1.0.0 | source

Cursor

Cursor implements low-level API to terminal control codes.

See:

Static Method Summary

Static Public Methods
public static

create(args: ...*): Cursor

Wrapper around new Cursor().

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 stdout.

Method Summary

Public Methods
public

Set the background color.

public

Destroy the cursor.

public

Move the cursor down.

public

Erase a defined region.

public

fill(options: Object): Cursor

Fill the specified region with symbol.

public

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

off(event: String, handler: Function): Cursor

Removes all listeners or specified listener from the event.

public

on(event: String, handler: Function): Cursor

Sets a listener on cursor stream.

public

Resets the entire screen.

public

Move the cursor right.

public

Set the cursor position by absolute coordinates.

public

Set the cursor visible.

public

Move the cursor up.

public

write(message: String): Cursor

Write to the stream.

Static Public Methods

public static create(args: ...*): Cursor source

Wrapper around new Cursor().

Params:

NameTypeAttributeDescription
args ...*

Return:

Cursor

public static getTTYHeight(): Number source

Get height of TTY.

Return:

Number

public static getTTYSize(): {width: Number, height: Number} source

Get TTY sizes.

Return:

{width: Number, height: Number}

public static getTTYWidth(): Number source

Get width of TTY.

Return:

Number

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.

Params:

NameTypeAttributeDescription
stdout Array<Stream.Transform>
  • optional
  • default: [process.stdout]

Array of Transform streams that will be used as target source for cursor

stdin Array<Stream.Transform>
  • optional
  • default: []

Array of Transform streams that will be used as data source for cursor

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.

Return:

Cursor

Example:

import { Cursor, COLORS } from './src/Cursor';

let cursor = new Cursor();
cursor.background(COLORS.YELLOW);

public destroy(): Cursor source

Destroy the cursor.

Return:

Cursor

public down(y: Number): Cursor source

Move the cursor down.

Params:

NameTypeAttributeDescription
y Number
  • optional
  • default: 1

Rows count

Return:

Cursor

public erase(): Cursor source

Erase a defined region.

Return:

Cursor

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.

Params:

NameTypeAttributeDescription
options Object
options.x1 Number

Start coordinate X

options.y1 Number

Start coordinate Y

options.x2 Number

End coordinate X

options.y2 Number

End coordinate Y

options.symbol String
  • optional

Symbol that will be used for filling the region

Return:

Cursor

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.

Return:

Cursor

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.

Return:

Promise

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 hide(): Cursor source

Set the cursor invisible.

Return:

Cursor

public left(x: Number): Cursor source

Move the cursor left.

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 1

Columns count

Return:

Cursor

public move(x: Number, y: Number): Cursor source

Move the cursor by the relative coordinates starting from the current position of cursor.

Params:

NameTypeAttributeDescription
x Number

Coordinate X

y Number

Coordinate Y

Return:

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.

Params:

NameTypeAttributeDescription
event String

Event name

handler Function
  • optional

Handler that you want to delete from the event

Return:

Cursor

public on(event: String, handler: Function): Cursor source

Sets a listener on cursor stream.

Params:

NameTypeAttributeDescription
event String

Event name

handler Function

Handler for the specified event

Return:

Cursor

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.

Return:

Cursor

public right(x: Number): Cursor source

Move the cursor right.

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 1

Columns count

Return:

Cursor

public setPosition(x: Number, y: Number): Cursor source

Set the cursor position by absolute coordinates.

Params:

NameTypeAttributeDescription
x Number

Coordinate X

y Number

Coordinate Y

Return:

Cursor

public show(): Cursor source

Set the cursor visible.

Return:

Cursor

public up(y: Number): Cursor source

Move the cursor up.

Params:

NameTypeAttributeDescription
y Number
  • optional
  • default: 1

Rows count

Return:

Cursor

public write(message: String): Cursor source

Write to the stream. It can be whatever info you want, but usually it's just a text that you want to print out.

Params:

NameTypeAttributeDescription
message String

Message to write to the stream

Return:

Cursor