Skip to content

Commit

Permalink
feat(presentation): Implements exit method and supports keypress for …
Browse files Browse the repository at this point in the history
…switching the slides
  • Loading branch information
ghaiklor committed Nov 23, 2015
1 parent a880a24 commit 4e8d731
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/Presentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ import { Cursor } from './cursor/Cursor';
* @version 1.0.0
*/
export class Presentation {
_cursor = Cursor.create([process.stdout], [process.stdin]);
_cursor = Cursor.create([process.stdout], [process.stdin]).reset().hide();
_currentSlideIndex = 0;
_slides = [];

constructor() {
/**
* Creates presentation instance with slides
* @param {Array<Array<Object>>} slides
*/
constructor(slides) {
process.stdin.setRawMode(true);
process.stdin.setEncoding('utf8');

keypress(process.stdin);

process.stdin.on('keypress', this._onKeyPress.bind(this));

this._slides = slides.map(slide => Slide.create(slide));
this.renderSlide();
}

/**
* Renders specified slide
* @param {Number} index Slide index in presentation
* @param {Number} [index] Slide index in presentation
* @returns {Presentation}
*/
renderSlide(index) {
Expand All @@ -49,4 +58,28 @@ export class Presentation {
this.renderSlide(--this._currentSlideIndex);
return this;
}

exit() {
this._cursor.show().reset();
process.exit(0);
}

/**
* Triggers when user is pressing the key.
*
* @param ch
* @param key
* @private
*/
_onKeyPress(ch, key) {
if (key.name == 'left') this.prevSlide();
if (key.name == 'right') this.nextSlide();
if (key.ctrl && key.name == 'c') this.exit();

return this;
}

static create(...args) {
return new this(...args);
}
}

0 comments on commit 4e8d731

Please sign in to comment.