tiny-cursor
Version:
A tiny library for hiding and showing the cursor in the terminal.
32 lines (31 loc) • 813 B
JavaScript
/* IMPORT */
import process from 'node:process';
import whenExit from 'when-exit';
/* MAIN */
class Cursor {
/* CONSTRUCTOR */
constructor(stream = process.stdout) {
/* API */
this.has = () => {
return this.visible;
};
this.hide = () => {
return this.toggle(false);
};
this.show = () => {
return this.toggle(true);
};
this.toggle = (force = !this.visible) => {
if (!this.stream.isTTY)
return;
this.visible = force;
const command = force ? '\u001B[?25h' : '\u001B[?25l';
this.stream.write(command);
};
this.stream = stream;
this.visible = true;
whenExit(this.show);
}
}
/* EXPORT */
export default Cursor;