Skip to content

Commit 8c2effe

Browse files
committed
feat(plugin-inquirer): checkbox support theme color configure
1 parent 4ab6873 commit 8c2effe

5 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎packages/@cz-git/plugin-inquirer/__tests__/style.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe("style()", () => {
4949
expect(style.bold("hello")).toBe("\u001b[1mhello\u001b[0m");
5050
expect(style.cyan("hello")).toBe("\u001b[36mhello\u001b[0m");
5151
expect(style.gray("hello")).toBe("\u001b[90mhello\u001b[0m");
52+
expect(style.rgb("38;5;036")("hello")).toBe("\u001b[38;5;036mhello\u001b[0m");
5253
expect(style.bold(" Add plugin " + style.yellow("hello"))).toBe(
5354
"\u001b[1m Add plugin \u001b[33mhello\u001b[0m\u001b[0m"
5455
);

‎packages/@cz-git/plugin-inquirer/examples/checkbox.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ inquirer
5959
{
6060
type: "search-checkbox",
6161
name: "cz",
62+
themeColorCode: "38;5;036",
6263
message: "Select scope:",
6364
source: function (answers, input) {
6465
return fuzzyFilter(input, testArr3);

‎packages/@cz-git/plugin-inquirer/src/checkbox/index.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description: inquirer plugin - ComplateCheckbox
2+
* @description: inquirer plugin - Search Checkbox
33
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
44
* @license MIT
55
*/
@@ -24,6 +24,7 @@ export class SearchCheckbox extends Base {
2424
private firstRender = true;
2525
private searching = false;
2626
private haveSearched = false;
27+
private themeColorCode?: string;
2728
private initialValue: any = -1;
2829
private lastSearchInput?: string;
2930
private paginator: Paginator = new Paginator(this.screen, { isInfinite: true });
@@ -33,10 +34,12 @@ export class SearchCheckbox extends Base {
3334

3435
constructor(questions: Question, readline: ReadlineInterface, answers: Answers) {
3536
super(questions, readline, answers);
36-
const { source, separator, isInitDefault } = this.opt as unknown as CZPromptQuestionOptions;
37+
const { source, separator, isInitDefault, themeColorCode } = this
38+
.opt as unknown as CZPromptQuestionOptions;
3739
if (!source) this.throwParamError("source");
3840
if (typeof separator === "string") this.separator = separator;
3941
if (isInitDefault) this.initialValue = this.opt.default;
42+
if (themeColorCode) this.themeColorCode = themeColorCode;
4043
this.renderChoices = new Choices([], {});
4144
}
4245

@@ -72,12 +75,18 @@ export class SearchCheckbox extends Base {
7275

7376
// Render choices or answer depending on the state
7477
if (this.status === "answered") {
75-
content += style.cyan(this.selection.join(this.separator));
78+
this.themeColorCode
79+
? (content += style.rgb(this.themeColorCode)(this.selection.join(this.separator)))
80+
: (content += style.cyan(this.selection.join(this.separator)));
7681
} else if (this.searching) {
7782
content += this.rl.line;
7883
bottomContent += " " + style.dim("Searching...");
7984
} else if (this.choicesLen) {
80-
const choicesStr = choicesRender(this.renderChoices.choices, this.pointer);
85+
const choicesStr = choicesRender(
86+
this.renderChoices.choices,
87+
this.pointer,
88+
this.themeColorCode
89+
);
8190
content += this.rl.line;
8291
const indexPosition = this.pointer;
8392
let realIndexPosition = 0;
@@ -99,11 +108,12 @@ export class SearchCheckbox extends Base {
99108
if (this.firstRender) {
100109
content += style.dim("Press <space>|<right> to select, <enter> to submit");
101110
}
111+
this.firstRender = false;
112+
102113
if (error) {
103114
bottomContent = style.red(">> ") + error;
104115
}
105116

106-
this.firstRender = false;
107117
this.screen.render(content, bottomContent);
108118
}
109119

@@ -265,7 +275,11 @@ export class SearchCheckbox extends Base {
265275
* @param {Number} pointer Position of the pointer
266276
* @return {String} Rendered content
267277
*/
268-
const choicesRender = (choices: ChoicesType["choices"], pointer: number): string => {
278+
const choicesRender = (
279+
choices: ChoicesType["choices"],
280+
pointer: number,
281+
themeColorCode?: string
282+
): string => {
269283
let output = "";
270284
let separatorOffset = 0;
271285

@@ -286,7 +300,11 @@ const choicesRender = (choices: ChoicesType["choices"], pointer: number): string
286300
? figures.squareSmallFilled + " " + choice.name
287301
: getCheckbox(choice.checked || false) + " " + choice.name;
288302
if (i - separatorOffset === pointer) {
289-
output += style.cyan(" " + figures.pointer + style.cyan(line));
303+
themeColorCode
304+
? (output += style.rgb(themeColorCode)(
305+
" " + figures.pointer + style.rgb(themeColorCode)(line)
306+
))
307+
: (output += style.cyan(" " + figures.pointer + line));
290308
} else {
291309
output += " " + line;
292310
}

‎packages/@cz-git/plugin-inquirer/src/shared/types/inquirer.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,25 @@ export interface CZPromptQuestionOptions<T extends Answers = Answers> extends Qu
3535
separator: string;
3636

3737
/**
38+
* @description: support rgb color code. e.g: `38;5;036`
39+
* @default: cyan
40+
*/
41+
themeColorCode?: string;
42+
43+
/**
44+
* @description:
3845
* Function to determine what options to display to user.
3946
* Called with previous answers object and the current user input each time the user types, it must return a promise.
4047
*/
4148
source: (answersSoFar: T, input: string | undefined) => Promise<any[]>;
4249

4350
/**
44-
* The number of elements to show on each page.
51+
* @description: The number of elements to show on each page.
4552
*/
4653
pageSize?: number | undefined;
4754

4855
/**
56+
* @description:
4957
* default false. Setting it to true turns the input into a normal text input.
5058
*/
5159
isInitDefault?: boolean | undefined;

‎packages/@cz-git/plugin-inquirer/src/shared/utils/style.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const styleFn = (enabled = isColorizenSupport()) => ({
5959
cyan: enabled ? formatter("\u001b[36m", "\u001b[0m") : String,
6060
white: enabled ? formatter("\u001b[37m", "\u001b[0m") : String,
6161
gray: enabled ? formatter("\u001b[90m", "\u001b[0m") : String,
62+
rgb: (rgbColor = "38;5;036") =>
63+
enabled ? formatter(`\u001b[${rgbColor}m`, "\u001b[0m") : String,
6264
bgBlack: enabled ? formatter("\u001b[40m", "\u001b[0m") : String,
6365
bgRed: enabled ? formatter("\u001b[41m", "\u001b[0m") : String,
6466
bgGreen: enabled ? formatter("\u001b[42m", "\u001b[0m") : String,

0 commit comments

Comments
 (0)