Skip to content

Commit

Permalink
feat(cursor): Implements on, off for cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiklor committed Nov 16, 2015
1 parent ed38b52 commit c38d93a
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Cursor {
if (stdout) this._cursor.pipe(stdout);
if (stdin) stdin.pipe(this._cursor);

process.on('exit', () => this.setPosition(Cursor.getTerminalWidth(), Cursor.getTerminalHeight()));
this.off('^C').on('^C', this._onClose);
}

/**
Expand Down Expand Up @@ -209,6 +209,43 @@ export class Cursor {
return this;
}

/**
* Sets listener to the specified event
* @param {String} event
* @param {Function} handler
* @returns {Cursor}
*/
on(event, handler) {
this._cursor.on(event, handler);
return this;
}

/**
* Removes all listeners or specified from the event
* @param {String} event
* @param {Function} [handler]
* @returns {Cursor}
*/
off(event, handler) {
if (handler) {
this._cursor.off(event, handler);
} else {
this._cursor.removeAllListeners(event);
}

return this;
}

/**
* Triggers when user types ^C combination for exit
* @private
* @returns {Cursor}
*/
_onClose() {
this.setPosition(Cursor.getTerminalWidth(), Cursor.getTerminalHeight());
process.exit(0);
}

/**
* Get width of terminal
* @returns {Number}
Expand Down

0 comments on commit c38d93a

Please sign in to comment.