Skip to content

Commit

Permalink
feat(plugin-inquirer): done search-list inquirer plugin
Browse files Browse the repository at this point in the history
link #28
  • Loading branch information
Zhengqbbb committed May 20, 2022
1 parent a620290 commit ad12f75
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"docs:build": "pnpm --filter=docs docs:build",
"docs:dev": "pnpm --filter=docs docs:dev",
"docs:serve": "pnpm --filter=docs docs:serve",
"example:checkbox": "node ./packages/@cz-git/plugin-inquirer/examples/checkbox.js",
"example:list": "node ./packages/@cz-git/plugin-inquirer/examples/list.js",
"lint": "eslint --ext .ts packages docs/.vuepress",
"prepare": "husky install",
"release:changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && pnpm run -r --stream release:changelog",
Expand Down
1 change: 1 addition & 0 deletions packages/@cz-git/plugin-inquirer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
pnpm install
pnpm run dev
node ./packages/@cz-git/plugin-inquirer/examples/checkbox.js
node ./packages/@cz-git/plugin-inquirer/examples/list.js
```
4 changes: 3 additions & 1 deletion packages/@cz-git/plugin-inquirer/examples/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ inquirer
{
type: "search-checkbox",
name: "testOne",
themeColorCode: "",
message: "Select checkbox test:",
source: function (answers, input) {
return fuzzyFilter(input, testArr1);
Expand All @@ -51,6 +52,7 @@ inquirer
{
type: "search-checkbox",
name: "testTwo",
themeColorCode: "38;5;043",
message: "Select checkbox test:",
source: function (answers, input) {
return fuzzyFilter(input, testArr2);
Expand All @@ -59,7 +61,7 @@ inquirer
{
type: "search-checkbox",
name: "cz",
themeColorCode: "38;5;036",
themeColorCode: "38;5;042",
message: "Select scope:",
source: function (answers, input) {
return fuzzyFilter(input, testArr3);
Expand Down
53 changes: 51 additions & 2 deletions packages/@cz-git/plugin-inquirer/examples/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,37 @@ var inquirer = require("inquirer");
var { SearchList, fuzzyFilter } = require("@cz-git/inquirer");
var testArr1 = [
{ name: "test1", value: "test1" },
{ name: "test2", value: "test2" }
{ name: "test2", value: "test2" },
{ name: "test3", value: "test3" },
{ name: "test4", value: "test4" },
{ name: "test5", value: "test5" },
{ name: "test6", value: "test6" },
{ name: "test7", value: "test7" },
{ name: "test8", value: "test8" },
{ name: "test9", value: "test9" }
];

var testArr2 = [
{ name: "test1", value: "test1" },
new inquirer.Separator(),
{ name: "test2", value: "test2" },
{ name: "test3", value: "test3" },
{ name: "test4", value: "test4" },
{ name: "test5", value: "test5" },
{ name: "test6", value: "test6" },
{ name: "test7", value: "test7" },
{ name: "test8", value: "test8" },
{ name: "test9", value: "test9" }
];

var testArr3 = [
{ value: false, name: "empty" },
{ value: "___CUSTOM___", name: "custom" },
new inquirer.Separator(),
{ value: "cz-git", name: "cz-git" },
{ value: "docs", name: "docs: cz-git document" },
{ value: "plugin-inquirer", name: "plugin-inquirer: provide cz-git inquirer" },
{ value: "plugin-loader", name: "plugin-loader: options loader" }
];

inquirer.registerPrompt("search-list", SearchList);
Expand All @@ -12,10 +42,29 @@ inquirer
{
type: "search-list",
name: "testOne",
message: "Select list test",
themeColorCode: "",
message: "Select checkbox test:",
source: function (answers, input) {
return fuzzyFilter(input, testArr1);
}
},
{
type: "search-list",
name: "testTwo",
themeColorCode: "38;5;043",
message: "Select checkbox test:",
source: function (answers, input) {
return fuzzyFilter(input, testArr2);
}
},
{
type: "search-list",
name: "cz",
themeColorCode: "38;5;042",
message: "Select scope:",
source: function (answers, input) {
return fuzzyFilter(input, testArr3);
}
}
])
.then(function (answers) {
Expand Down
9 changes: 5 additions & 4 deletions packages/@cz-git/plugin-inquirer/src/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type { Answers, Question } from "inquirer";
import type Separator from "inquirer/lib/objects/separator";
import type { CZPromptQuestionOptions, ChoicesType, ChoiceType } from "../shared";
import { style, figures } from "../shared";

export type { CZPromptQuestionOptions } from "../shared";
export class SearchCheckbox extends Base {
private renderChoices: ChoicesType;
private originChoices: ChoiceType<string>[] = [];
Expand Down Expand Up @@ -54,8 +56,7 @@ export class SearchCheckbox extends Base {
const events = observe(this.rl);
const dontHaveAnswer = () => this.answer === undefined;

// const validation = this.handleSubmitEvents(events.line.pipe(map(this.onSubmit.bind(this))));
events.keypress.pipe(takeWhile(dontHaveAnswer)).forEach(this.onKeyPress.bind(this));
events.keypress.pipe(takeWhile(dontHaveAnswer)).forEach(this.onKeypress.bind(this));
events.spaceKey.pipe(takeWhile(dontHaveAnswer)).forEach(this.onChoice.bind(this));
events.line.pipe(takeWhile(dontHaveAnswer)).forEach(this.onSubmit.bind(this));

Expand Down Expand Up @@ -111,7 +112,7 @@ export class SearchCheckbox extends Base {
this.firstRender = false;

if (error) {
bottomContent = style.red(">> ") + error;
bottomContent += "\n" + style.red(">> ") + error;
}

this.screen.render(content, bottomContent);
Expand Down Expand Up @@ -238,7 +239,7 @@ export class SearchCheckbox extends Base {
/**
* @description: Search <any key>
*/
onKeyPress(e: { key: { name?: string; ctrl?: boolean }; value: string }) {
onKeypress(e: { key: { name?: string; ctrl?: boolean }; value: string }) {
let len;
const keyName = e.key?.name || "";
if (keyName === "space") {
Expand Down
Loading

0 comments on commit ad12f75

Please sign in to comment.