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

Add heading/separator support to select and multiselect #364

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
33 changes: 25 additions & 8 deletions lib/elements/multiselect.js
Original file line number Diff line number Diff line change
@@ -22,7 +22,9 @@ class MultiselectPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.cursor = opts.cursor || 0;
// set the cursor to the first non-heading
this.cursorStart = opts.choices.findIndex(choice => !choice.heading);
this.cursor = opts.cursor || this.cursorStart;
this.scrollIndex = opts.cursor || 0;
this.hint = opts.hint || '';
this.warn = opts.warn || '- This option is disabled -';
@@ -39,7 +41,8 @@ class MultiselectPrompt extends Prompt {
description: ch && ch.description,
value: ch && (ch.value === undefined ? idx : ch.value),
selected: ch && ch.selected,
disabled: ch && ch.disabled
disabled: ch && ch.disabled,
heading: ch && ch.heading
};
});
this.clear = clear('', this.out.columns);
@@ -50,7 +53,7 @@ class MultiselectPrompt extends Prompt {

reset() {
this.value.map(v => !v.selected);
this.cursor = 0;
this.cursor = this.cursorStart;
this.fire();
this.render();
}
@@ -88,7 +91,7 @@ class MultiselectPrompt extends Prompt {
}

first() {
this.cursor = 0;
this.cursor = this.cursorStart
this.render();
}

@@ -98,23 +101,32 @@ class MultiselectPrompt extends Prompt {
}
next() {
this.cursor = (this.cursor + 1) % this.value.length;
if (this.value[this.cursor].heading) {
this.next();
}
this.render();
}

up() {
if (this.cursor === 0) {
if (this.cursor === this.cursorStart) {
this.cursor = this.value.length - 1;
} else {
this.cursor--;
if (this.value[this.cursor].heading) {
this.up();
}
}
this.render();
}

down() {
if (this.cursor === this.value.length - 1) {
this.cursor = 0;
this.cursor = this.cursorStart;
} else {
this.cursor++;
if (this.value[this.cursor].heading) {
this.down();
}
}
this.render();
}
@@ -150,7 +162,7 @@ class MultiselectPrompt extends Prompt {
}

const newSelected = !this.value[this.cursor].selected;
this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
this.value.filter(v => !v.disabled && !v.heading).forEach(v => v.selected = newSelected);
this.render();
}

@@ -184,7 +196,12 @@ class MultiselectPrompt extends Prompt {

if (v.disabled) {
title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
} else {
}
else if(v.heading) {
title = v.title
return title + color.gray(desc || '');
}
else {
title = cursor === i ? color.cyan().underline(v.title) : v.title;
if (cursor === i && v.description) {
desc = ` - ${v.description}`;