Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separator #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Initializes an interactive navigable menu based prompt, where the user can navig
```js
const qoa = require('qoa');

const {log} = console;
const {log, error} = console;

const interactive = {
type: 'interactive',
Expand All @@ -239,11 +239,11 @@ const interactive = {
};

// using the `prompt` async method
qoa.prompt([interactive]).then(log);
qoa.prompt([interactive]).then(log).catch(error);
//=> { treat: 'Cupcakes' }

// using the `interactive` async method
qoa.interactive(interactive).then(log);
qoa.interactive(interactive).then(log).catch(error);
//=> { treat: 'Cupcakes' }
```

Expand Down
10 changes: 8 additions & 2 deletions src/interactive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const readline = require('readline');
const Nav = require('./nav');
const Separator = require('./separator');

class Interactive extends Nav {
constructor(opts = {}) {
Expand Down Expand Up @@ -40,7 +41,7 @@ class Interactive extends Nav {
}
};

return new Promise(resolve => {
return new Promise((resolve, reject) => {
this._cursor.hide();
this._displayQuestion();

Expand All @@ -55,7 +56,12 @@ class Interactive extends Nav {
this._input.pause();
this._input.setRawMode(false);
this._input.removeListener('keypress', onkeypress);
resolve(answer);

if (answer.action instanceof Separator) {
reject(new Error('Separator can\'t selected'));
} else {
resolve(answer);
}
});
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/nav.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const Menu = require('./menu');
const Separator = require('./separator');

const {log} = console;

Expand All @@ -19,11 +20,12 @@ class Nav extends Menu {

get _menuItems() {
return this._menu.map((x, i) => {
if (i === this._idx) {
return this._formatItem.selected(x);
const _x = x instanceof Separator ? x._seperator : x;
if ((i === this._idx) && !(x instanceof Separator)) {
return this._formatItem.selected(_x);
}

return this._formatItem.menu(x);
return this._formatItem.menu(_x);
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/qoa.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Interactive = require('./interactive');
const Keypress = require('./keypress');
const Quiz = require('./quiz');
const Secure = require('./secure');
const Separator = require('./separator');

class Qoa {
constructor(opts = {}) {
Expand Down Expand Up @@ -62,6 +63,10 @@ class Qoa {
return new Secure(this._buildConfig(x)).request();
}

separator(x) {
return new Separator(x);
}

clearScreen() {
process.stdout.cursorTo(0, 0);
process.stdout.clearScreenDown();
Expand Down
9 changes: 9 additions & 0 deletions src/separator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

class Seperator {
constructor(opts = {}) {
this._seperator = opts.seperator || '***';
}
}

module.exports = Seperator;