inquirer-plugins 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/_internal/plugins/table/table.d.ts +11 -0
- package/dist/_internal/plugins/table/table.js +59 -0
- package/dist/_internal/plugins/table/types.d.ts +45 -0
- package/dist/_internal/plugins/table/types.js +23 -0
- package/dist/_internal/plugins/table/utils.d.ts +3 -0
- package/dist/_internal/plugins/table/utils.js +22 -0
- package/dist/_internal/plugins/table.d.ts +49 -0
- package/dist/_internal/plugins/table.js +257 -0
- package/dist/_internal/plugins/test.d.ts +1 -0
- package/dist/_internal/plugins/test.js +47 -0
- package/dist/_internal/types/inquirer.d.ts +1 -0
- package/dist/_internal/types/inquirer.js +1 -0
- package/dist/_internal/types/parse-types.d.ts +6 -0
- package/dist/_internal/types/parse-types.js +5 -0
- package/dist/_internal/utils/chalk.d.ts +9 -0
- package/dist/_internal/utils/chalk.js +10 -0
- package/dist/_internal/utils/keypress.d.ts +3 -0
- package/dist/_internal/utils/keypress.js +14 -0
- package/dist/_internal/utils/parse-types.d.ts +3 -0
- package/dist/_internal/utils/parse-types.js +22 -0
- package/dist/modules/table.d.ts +2 -0
- package/dist/modules/table.js +2 -0
- package/dist/modules/types/inquirer.d.ts +1 -0
- package/dist/modules/types/inquirer.js +1 -0
- package/dist/modules/types/parse-types.d.ts +1 -0
- package/dist/modules/types/parse-types.js +1 -0
- package/package.json +86 -0
- package/src/_internal/plugins/table/table.ts +68 -0
- package/src/_internal/plugins/table/types.ts +71 -0
- package/src/_internal/plugins/table/utils.ts +25 -0
- package/src/_internal/plugins/table.ts +299 -0
- package/src/_internal/plugins/test.ts +58 -0
- package/src/_internal/types/inquirer.ts +1 -0
- package/src/_internal/types/parse-types.ts +7 -0
- package/src/_internal/utils/chalk.ts +13 -0
- package/src/_internal/utils/keypress.ts +16 -0
- package/src/_internal/utils/parse-types.ts +25 -0
- package/src/modules/table.ts +2 -0
- package/src/modules/types/inquirer.ts +1 -0
- package/src/modules/types/parse-types.ts +1 -0
- package/table/package.json +6 -0
- package/tsconfig.json +27 -0
- package/types/inquirer/package.json +6 -0
- package/types/parse-types/package.json +6 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# inquirer-plugins
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Column, Cell } from './types.js';
|
|
2
|
+
export declare class TableClass {
|
|
3
|
+
columns: Column[];
|
|
4
|
+
rows: string[][];
|
|
5
|
+
constructor(columns: Column[], rows: Cell[][]);
|
|
6
|
+
get header(): string[];
|
|
7
|
+
getColumnType(columnIndex: number): import("../../types/parse-types.js").ParseType;
|
|
8
|
+
updateRow(rowIndex: number, columnIndex: number, value: string): void;
|
|
9
|
+
getValue(rowIndex: number, columnIndex: number): string;
|
|
10
|
+
getResult(): Record<string, Cell>[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { PARSE_TYPES } from '../../types/parse-types.js';
|
|
2
|
+
import { trim } from '../../utils/chalk.js';
|
|
3
|
+
import { parse } from '../../utils/parse-types.js';
|
|
4
|
+
export class TableClass {
|
|
5
|
+
columns = [];
|
|
6
|
+
rows = [];
|
|
7
|
+
constructor(columns, rows) {
|
|
8
|
+
this.columns = columns;
|
|
9
|
+
this.rows = rows.map(columns => columns.map(value => `${value}`));
|
|
10
|
+
}
|
|
11
|
+
get header() {
|
|
12
|
+
return this.columns.map(({ name }) => `${name}`);
|
|
13
|
+
}
|
|
14
|
+
getColumnType(columnIndex) {
|
|
15
|
+
if (!(columnIndex in this.columns)) {
|
|
16
|
+
return PARSE_TYPES.input;
|
|
17
|
+
}
|
|
18
|
+
return this.columns[columnIndex].type;
|
|
19
|
+
}
|
|
20
|
+
updateRow(rowIndex, columnIndex, value) {
|
|
21
|
+
if (!(rowIndex in this.rows)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const columns = this.rows[rowIndex];
|
|
25
|
+
if (!(columnIndex in columns)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const validate = this.columns[columnIndex].validate || (() => true);
|
|
29
|
+
if (!validate(value)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const parser = this.columns[columnIndex].parser || ((txt) => txt);
|
|
33
|
+
const parsed = parser(value);
|
|
34
|
+
columns[columnIndex] = `${parsed}`;
|
|
35
|
+
}
|
|
36
|
+
getValue(rowIndex, columnIndex) {
|
|
37
|
+
if (!(rowIndex in this.rows)) {
|
|
38
|
+
return '';
|
|
39
|
+
}
|
|
40
|
+
const columns = this.rows[rowIndex];
|
|
41
|
+
if (!(columnIndex in columns)) {
|
|
42
|
+
return '';
|
|
43
|
+
}
|
|
44
|
+
return columns[columnIndex];
|
|
45
|
+
}
|
|
46
|
+
getResult() {
|
|
47
|
+
const result = [];
|
|
48
|
+
for (let i = 0; i < this.rows.length; i++) {
|
|
49
|
+
result.push({});
|
|
50
|
+
for (let ii = 0; ii < (this.rows[i] || []).length; ii++) {
|
|
51
|
+
const { name: n, type, parser } = this.columns[ii];
|
|
52
|
+
const name = trim(n.toString());
|
|
53
|
+
const value = trim(this.rows[i][ii]);
|
|
54
|
+
result[i][name] = parser ? parser(value) : parse(value, type);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ChalkInstance } from 'chalk';
|
|
2
|
+
import { ParseType } from '../../types/parse-types.js';
|
|
3
|
+
export declare const DEFAULT_PAGE_SIZE = 5;
|
|
4
|
+
export declare const INDEX_ERRORS: Readonly<{
|
|
5
|
+
readonly excess: "excess";
|
|
6
|
+
readonly disabled: "disabled";
|
|
7
|
+
}>;
|
|
8
|
+
export interface DefaultOptions extends Required<Omit<TableQuestion, 'type' | 'name' | 'colors'>> {
|
|
9
|
+
colors: Colors;
|
|
10
|
+
}
|
|
11
|
+
export declare const DEFAULT_OPTIONS: DefaultOptions;
|
|
12
|
+
export interface RequiredOptions extends Required<Omit<TableQuestion, 'colors'>> {
|
|
13
|
+
colors: Colors;
|
|
14
|
+
}
|
|
15
|
+
export interface Colors {
|
|
16
|
+
selected: ChalkInstance;
|
|
17
|
+
editable: ChalkInstance;
|
|
18
|
+
editing: ChalkInstance;
|
|
19
|
+
}
|
|
20
|
+
export interface ValidateResult {
|
|
21
|
+
isValid: boolean;
|
|
22
|
+
message: string;
|
|
23
|
+
}
|
|
24
|
+
export interface TableQuestion {
|
|
25
|
+
type: string;
|
|
26
|
+
name: string;
|
|
27
|
+
message?: string | ChalkInstance;
|
|
28
|
+
description?: string | ChalkInstance;
|
|
29
|
+
escapeMessage?: string | ChalkInstance;
|
|
30
|
+
confirmMessage?: string | ChalkInstance;
|
|
31
|
+
disableColumnIndexes?: number[];
|
|
32
|
+
colors?: Partial<Colors>;
|
|
33
|
+
columns: Column[];
|
|
34
|
+
rows: Cell[][];
|
|
35
|
+
validate?: (...args: any[]) => ValidateResult;
|
|
36
|
+
pageSize?: number;
|
|
37
|
+
}
|
|
38
|
+
export interface Column {
|
|
39
|
+
name: string | ChalkInstance;
|
|
40
|
+
value: string;
|
|
41
|
+
type: ParseType;
|
|
42
|
+
validate?: (input: string) => boolean;
|
|
43
|
+
parser?: (input: any) => any;
|
|
44
|
+
}
|
|
45
|
+
export type Cell = number | string | boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { hop, success, warn } from '../../utils/chalk.js';
|
|
3
|
+
export const DEFAULT_PAGE_SIZE = 5;
|
|
4
|
+
export const INDEX_ERRORS = Object.freeze({
|
|
5
|
+
excess: 'excess',
|
|
6
|
+
disabled: 'disabled',
|
|
7
|
+
});
|
|
8
|
+
export const DEFAULT_OPTIONS = {
|
|
9
|
+
message: '',
|
|
10
|
+
description: '',
|
|
11
|
+
escapeMessage: chalk.red('Press ESC again to exit!'),
|
|
12
|
+
confirmMessage: chalk.red('Press ENTER again to confirm!'),
|
|
13
|
+
disableColumnIndexes: [],
|
|
14
|
+
colors: {
|
|
15
|
+
selected: warn,
|
|
16
|
+
editable: success,
|
|
17
|
+
editing: hop,
|
|
18
|
+
},
|
|
19
|
+
columns: [],
|
|
20
|
+
rows: [],
|
|
21
|
+
pageSize: DEFAULT_PAGE_SIZE,
|
|
22
|
+
validate: () => ({ isValid: true, message: '' }),
|
|
23
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const isNoChoiceOfIndex: (maxIndex: number, disableIndexes: number[]) => boolean;
|
|
2
|
+
export declare const gateValidateColumnIndexError: (index: number, maxIndex: number, disableIndexes: number[]) => false | "disabled" | "excess";
|
|
3
|
+
export declare const getNextIndex: (index: number, maxIndex: number, num: number) => number;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { INDEX_ERRORS } from '../../plugins/table/types.js';
|
|
2
|
+
export const isNoChoiceOfIndex = (maxIndex, disableIndexes) => {
|
|
3
|
+
return disableIndexes.length > maxIndex && [...Array(maxIndex).keys()].every(index => disableIndexes.includes(index));
|
|
4
|
+
};
|
|
5
|
+
export const gateValidateColumnIndexError = (index, maxIndex, disableIndexes) => {
|
|
6
|
+
if (index > maxIndex || index < 0) {
|
|
7
|
+
return INDEX_ERRORS.excess;
|
|
8
|
+
}
|
|
9
|
+
if (disableIndexes.includes(index)) {
|
|
10
|
+
return INDEX_ERRORS.disabled;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
};
|
|
14
|
+
export const getNextIndex = (index, maxIndex, num) => {
|
|
15
|
+
if (index > maxIndex) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
if (index < 0) {
|
|
19
|
+
return maxIndex;
|
|
20
|
+
}
|
|
21
|
+
return index > maxIndex ? 0 : index + num;
|
|
22
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Interface as ReadLineInterface, Key } from 'readline';
|
|
3
|
+
import { Question, Answers } from 'inquirer';
|
|
4
|
+
import Base from 'inquirer/lib/prompts/base.js';
|
|
5
|
+
import { Done } from '../types/inquirer.js';
|
|
6
|
+
import { TableClass } from './table/table.js';
|
|
7
|
+
import { RequiredOptions, TableQuestion } from './table/types.js';
|
|
8
|
+
export declare class TablePlugin extends Base {
|
|
9
|
+
table: TableClass;
|
|
10
|
+
options: RequiredOptions;
|
|
11
|
+
done: Done;
|
|
12
|
+
pointerIndexes: {
|
|
13
|
+
row: number;
|
|
14
|
+
column: number;
|
|
15
|
+
};
|
|
16
|
+
isNextCloses: {
|
|
17
|
+
escape: boolean;
|
|
18
|
+
};
|
|
19
|
+
editing: boolean;
|
|
20
|
+
temporaryBackup: string | null;
|
|
21
|
+
answers: Answers;
|
|
22
|
+
constructor(question: Question<TableQuestion>, readLine: ReadLineInterface, answers: Answers);
|
|
23
|
+
getCurrentValues(): Record<string, import("./table/types.js").Cell>[];
|
|
24
|
+
isEditable(index: number): boolean;
|
|
25
|
+
_run(callback: Done): this;
|
|
26
|
+
get maxPageIndex(): number;
|
|
27
|
+
get maxRowIndex(): number;
|
|
28
|
+
get maxColumnIndex(): number;
|
|
29
|
+
onDownKey(): void;
|
|
30
|
+
onUpKey(): void;
|
|
31
|
+
updateColumnIndex(num: number): void;
|
|
32
|
+
onLeftKey(): void;
|
|
33
|
+
onRightKey(): void;
|
|
34
|
+
clear(): void;
|
|
35
|
+
onEnd(state: any): void;
|
|
36
|
+
validate(): import("./table/types.js").ValidateResult;
|
|
37
|
+
onError(_state: any): void;
|
|
38
|
+
validateValue(): void;
|
|
39
|
+
updateChoice(value: string): void;
|
|
40
|
+
get currentValue(): string;
|
|
41
|
+
onEditPress(key: Key): void;
|
|
42
|
+
getShowRowIndex(): {
|
|
43
|
+
rowIndexFrom: number;
|
|
44
|
+
rowIndexTo: number;
|
|
45
|
+
};
|
|
46
|
+
isSelected(rowIndex: number, columnIndex: number): boolean;
|
|
47
|
+
getColor(_value: string, isSelected: boolean, isEditable: boolean): string;
|
|
48
|
+
render(isDisconnect?: boolean): void;
|
|
49
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import cliCursor from 'cli-cursor';
|
|
2
|
+
import Table from 'cli-table3';
|
|
3
|
+
import Base from 'inquirer/lib/prompts/base.js';
|
|
4
|
+
import observe from 'inquirer/lib/utils/events.js';
|
|
5
|
+
import { map } from 'rxjs/operators';
|
|
6
|
+
import { dim, error } from '../utils/chalk.js';
|
|
7
|
+
import { getKeypressValue } from '../utils/keypress.js';
|
|
8
|
+
import { TableClass } from './table/table.js';
|
|
9
|
+
import { DEFAULT_OPTIONS } from './table/types.js';
|
|
10
|
+
import { isNoChoiceOfIndex, gateValidateColumnIndexError, getNextIndex } from './table/utils.js';
|
|
11
|
+
export class TablePlugin extends Base {
|
|
12
|
+
table = new TableClass([], []);
|
|
13
|
+
options;
|
|
14
|
+
done;
|
|
15
|
+
pointerIndexes = {
|
|
16
|
+
row: 0,
|
|
17
|
+
column: 0,
|
|
18
|
+
};
|
|
19
|
+
isNextCloses = {
|
|
20
|
+
escape: false,
|
|
21
|
+
};
|
|
22
|
+
editing = false;
|
|
23
|
+
temporaryBackup = null;
|
|
24
|
+
answers = [];
|
|
25
|
+
constructor(question, readLine, answers) {
|
|
26
|
+
super({ ...question, validate: () => false }, readLine, answers);
|
|
27
|
+
this.options = {
|
|
28
|
+
...DEFAULT_OPTIONS,
|
|
29
|
+
...this.opt,
|
|
30
|
+
validate: question.validate || DEFAULT_OPTIONS.validate,
|
|
31
|
+
};
|
|
32
|
+
this.answers = answers;
|
|
33
|
+
this.table = new TableClass(this.options.columns, this.options.rows);
|
|
34
|
+
if (isNoChoiceOfIndex(this.maxColumnIndex, this.options.disableColumnIndexes)) {
|
|
35
|
+
throw new Error('disableColumnIndexes contains all of index.');
|
|
36
|
+
}
|
|
37
|
+
this.pointerIndexes.row = 0;
|
|
38
|
+
this.updateColumnIndex(1);
|
|
39
|
+
}
|
|
40
|
+
getCurrentValues() {
|
|
41
|
+
return this.table.getResult();
|
|
42
|
+
}
|
|
43
|
+
isEditable(index) {
|
|
44
|
+
return !this.options.disableColumnIndexes.includes(index);
|
|
45
|
+
}
|
|
46
|
+
_run(callback) {
|
|
47
|
+
this.done = callback;
|
|
48
|
+
const events = observe(this.rl);
|
|
49
|
+
const validation = this.handleSubmitEvents(events.line.pipe(map(this.getCurrentValues.bind(this))));
|
|
50
|
+
validation.success.forEach(this.onEnd.bind(this));
|
|
51
|
+
validation.error.forEach(this.onError.bind(this));
|
|
52
|
+
events.keypress.forEach(({ key }) => {
|
|
53
|
+
if (key.name !== 'escape') {
|
|
54
|
+
this.isNextCloses.escape = false;
|
|
55
|
+
}
|
|
56
|
+
if (!this.editing) {
|
|
57
|
+
switch (key.name) {
|
|
58
|
+
case 'down':
|
|
59
|
+
return this.onDownKey();
|
|
60
|
+
case 'up':
|
|
61
|
+
return this.onUpKey();
|
|
62
|
+
case 'left':
|
|
63
|
+
return this.onLeftKey();
|
|
64
|
+
case 'right':
|
|
65
|
+
return this.onRightKey();
|
|
66
|
+
case 'escape':
|
|
67
|
+
if (!this.isNextCloses.escape) {
|
|
68
|
+
this.isNextCloses.escape = true;
|
|
69
|
+
return this.render();
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.render();
|
|
73
|
+
return this.onEnd(false);
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
if (this.isEditable(this.pointerIndexes.column)) {
|
|
77
|
+
this.editing = true;
|
|
78
|
+
this.onEditPress(key);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return this.onEditPress(key);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
cliCursor.hide();
|
|
87
|
+
this.render();
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
get maxPageIndex() {
|
|
91
|
+
return this.options.pageSize - 1;
|
|
92
|
+
}
|
|
93
|
+
get maxRowIndex() {
|
|
94
|
+
return this.table.rows.length - 1;
|
|
95
|
+
}
|
|
96
|
+
get maxColumnIndex() {
|
|
97
|
+
return this.table.columns.length - 1;
|
|
98
|
+
}
|
|
99
|
+
onDownKey() {
|
|
100
|
+
if (this.pointerIndexes.row < this.maxRowIndex) {
|
|
101
|
+
++this.pointerIndexes.row;
|
|
102
|
+
}
|
|
103
|
+
this.render();
|
|
104
|
+
}
|
|
105
|
+
onUpKey() {
|
|
106
|
+
if (this.pointerIndexes.row > 0) {
|
|
107
|
+
--this.pointerIndexes.row;
|
|
108
|
+
}
|
|
109
|
+
this.render();
|
|
110
|
+
}
|
|
111
|
+
updateColumnIndex(num) {
|
|
112
|
+
let index = this.pointerIndexes.column;
|
|
113
|
+
for (let c = 0; c <= this.maxColumnIndex; c++) {
|
|
114
|
+
index = getNextIndex(index, this.maxColumnIndex, num);
|
|
115
|
+
const error = gateValidateColumnIndexError(index, this.maxColumnIndex, this.options.disableColumnIndexes);
|
|
116
|
+
if (!error) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
this.pointerIndexes.column = index;
|
|
121
|
+
}
|
|
122
|
+
onLeftKey() {
|
|
123
|
+
this.updateColumnIndex(-1);
|
|
124
|
+
this.render();
|
|
125
|
+
}
|
|
126
|
+
onRightKey() {
|
|
127
|
+
this.updateColumnIndex(1);
|
|
128
|
+
this.render();
|
|
129
|
+
}
|
|
130
|
+
clear() {
|
|
131
|
+
this.temporaryBackup = null;
|
|
132
|
+
}
|
|
133
|
+
onEnd(state) {
|
|
134
|
+
this.render(true);
|
|
135
|
+
this.clear();
|
|
136
|
+
this.status = 'answered';
|
|
137
|
+
this.screen.done();
|
|
138
|
+
cliCursor.show();
|
|
139
|
+
if (state) {
|
|
140
|
+
return this.done({
|
|
141
|
+
state,
|
|
142
|
+
result: this.table.getResult(),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return this.done({
|
|
147
|
+
state,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
validate() {
|
|
152
|
+
return this.options.validate(this.getCurrentValues(), this.answers);
|
|
153
|
+
}
|
|
154
|
+
onError(_state) {
|
|
155
|
+
this.clear();
|
|
156
|
+
if (this.editing) {
|
|
157
|
+
this.editing = false;
|
|
158
|
+
this.render();
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
if (this.validate().isValid) {
|
|
162
|
+
return this.onEnd(true);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return this.render();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
validateValue() { }
|
|
170
|
+
updateChoice(value) {
|
|
171
|
+
this.table.updateRow(this.pointerIndexes.row, this.pointerIndexes.column, value);
|
|
172
|
+
}
|
|
173
|
+
get currentValue() {
|
|
174
|
+
return this.table.getValue(this.pointerIndexes.row, this.pointerIndexes.column);
|
|
175
|
+
}
|
|
176
|
+
onEditPress(key) {
|
|
177
|
+
let value = this.currentValue;
|
|
178
|
+
if (this.temporaryBackup === null) {
|
|
179
|
+
this.temporaryBackup = value;
|
|
180
|
+
value = '';
|
|
181
|
+
}
|
|
182
|
+
switch (key.name) {
|
|
183
|
+
case 'escape':
|
|
184
|
+
this.updateChoice(this.temporaryBackup);
|
|
185
|
+
this.editing = false;
|
|
186
|
+
break;
|
|
187
|
+
case 'delete':
|
|
188
|
+
this.updateChoice('');
|
|
189
|
+
break;
|
|
190
|
+
case 'backspace': {
|
|
191
|
+
let currentValue = value;
|
|
192
|
+
currentValue = currentValue.slice(0, -1);
|
|
193
|
+
this.updateChoice(currentValue);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
default:
|
|
197
|
+
this.updateChoice(`${value}${getKeypressValue(key)}`);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
this.render();
|
|
201
|
+
}
|
|
202
|
+
getShowRowIndex() {
|
|
203
|
+
const middleOfPage = Math.floor(this.options.pageSize / 2);
|
|
204
|
+
let rowIndexFrom = Math.max(0, this.pointerIndexes.row - middleOfPage);
|
|
205
|
+
const rowIndexTo = Math.min(this.maxRowIndex, rowIndexFrom + this.maxPageIndex);
|
|
206
|
+
rowIndexFrom = Math.min(rowIndexFrom, rowIndexTo - (this.options.pageSize - 1));
|
|
207
|
+
return { rowIndexFrom, rowIndexTo };
|
|
208
|
+
}
|
|
209
|
+
isSelected(rowIndex, columnIndex) {
|
|
210
|
+
return this.pointerIndexes.row === rowIndex && this.pointerIndexes.column === columnIndex;
|
|
211
|
+
}
|
|
212
|
+
getColor(_value, isSelected, isEditable) {
|
|
213
|
+
const value = isEditable ? _value : dim(_value);
|
|
214
|
+
if (!isSelected) {
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
if (this.editing) {
|
|
218
|
+
return isEditable ? this.options.colors.editing(value) : this.options.colors.selected(value);
|
|
219
|
+
}
|
|
220
|
+
return isEditable ? this.options.colors.editable(value) : this.options.colors.selected(value);
|
|
221
|
+
}
|
|
222
|
+
render(isDisconnect = false) {
|
|
223
|
+
let content = this.getQuestion();
|
|
224
|
+
let bottomContent = '';
|
|
225
|
+
content += this.options.description ? `\n${this.options.description}` : '';
|
|
226
|
+
const { rowIndexFrom, rowIndexTo } = this.getShowRowIndex();
|
|
227
|
+
const table = new Table({
|
|
228
|
+
head: this.table.header,
|
|
229
|
+
});
|
|
230
|
+
for (let i = 0; i < this.table.rows.length; i++) {
|
|
231
|
+
if (i < rowIndexFrom || i > rowIndexTo) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const columnValues = [];
|
|
235
|
+
for (let ii = 0; ii < (this.table.rows[i] || []).length; ii++) {
|
|
236
|
+
const isEditable = this.isEditable(ii);
|
|
237
|
+
const value = this.table.rows[i][ii];
|
|
238
|
+
columnValues.push(this.getColor(value, this.isSelected(i, ii), isEditable));
|
|
239
|
+
}
|
|
240
|
+
table.push(columnValues);
|
|
241
|
+
}
|
|
242
|
+
content += '\n\n' + table.toString();
|
|
243
|
+
if (!isDisconnect) {
|
|
244
|
+
const validateResult = this.validate();
|
|
245
|
+
if (!this.editing && validateResult.isValid) {
|
|
246
|
+
bottomContent = this.options.confirmMessage;
|
|
247
|
+
}
|
|
248
|
+
if (!validateResult.isValid) {
|
|
249
|
+
bottomContent = validateResult.message || error('Something went wrong.');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (this.isNextCloses.escape) {
|
|
253
|
+
bottomContent = this.options.escapeMessage;
|
|
254
|
+
}
|
|
255
|
+
this.screen.render(content, bottomContent);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import inquirer from 'inquirer';
|
|
3
|
+
import { success, warn, hop, cyan } from '../utils/chalk.js';
|
|
4
|
+
import { TablePlugin } from './table.js';
|
|
5
|
+
const run = async () => {
|
|
6
|
+
inquirer.registerPrompt('table-input', TablePlugin);
|
|
7
|
+
const rows = [...Array(33).keys()].map(i => [cyan(`--path ${i}--`), '--id--', true, i + 100, '--lock--']);
|
|
8
|
+
const question = {
|
|
9
|
+
type: 'table-input',
|
|
10
|
+
name: 'links',
|
|
11
|
+
message: chalk.red('GENERATE LINKS PATH SETTINGS'),
|
|
12
|
+
description: 'Edit setting.',
|
|
13
|
+
disableColumnIndexes: [0],
|
|
14
|
+
colors: {
|
|
15
|
+
selected: warn,
|
|
16
|
+
editable: success,
|
|
17
|
+
editing: hop,
|
|
18
|
+
},
|
|
19
|
+
columns: [
|
|
20
|
+
{ name: cyan.bold('filepath'), value: 'path', type: 'input' },
|
|
21
|
+
{ name: cyan.bold('id'), value: 'id', type: 'input', parser: () => 'aaa' },
|
|
22
|
+
{ name: cyan.bold('title'), value: 'title', type: 'confirm' },
|
|
23
|
+
{
|
|
24
|
+
name: cyan.bold('num'),
|
|
25
|
+
value: 'output',
|
|
26
|
+
type: 'number',
|
|
27
|
+
validate: (value) => /^\d*$/.test(value) || value === '0' || !value,
|
|
28
|
+
},
|
|
29
|
+
{ name: cyan.bold('lock'), value: 'lock', type: 'input' },
|
|
30
|
+
],
|
|
31
|
+
rows: rows,
|
|
32
|
+
validate: (inputs, _answers) => {
|
|
33
|
+
const isValid = inputs.every((i) => Object.values(i).every(v => `${v}`.length));
|
|
34
|
+
return {
|
|
35
|
+
isValid,
|
|
36
|
+
message: isValid ? '' : 'not allow empty input',
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const answers = await inquirer.prompt([
|
|
41
|
+
question,
|
|
42
|
+
]);
|
|
43
|
+
console.log('answers', JSON.stringify(answers, null, 2));
|
|
44
|
+
};
|
|
45
|
+
run().then(() => {
|
|
46
|
+
console.log('--- end ---');
|
|
47
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Done = (value: any) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const trim: (txt: string) => string;
|
|
2
|
+
export declare const success: import("chalk").ChalkInstance;
|
|
3
|
+
export declare const warn: import("chalk").ChalkInstance;
|
|
4
|
+
export declare const info: import("chalk").ChalkInstance;
|
|
5
|
+
export declare const error: import("chalk").ChalkInstance;
|
|
6
|
+
export declare const hop: import("chalk").ChalkInstance;
|
|
7
|
+
export declare const cyan: import("chalk").ChalkInstance;
|
|
8
|
+
export declare const bold: import("chalk").ChalkInstance;
|
|
9
|
+
export declare const dim: import("chalk").ChalkInstance;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
export const trim = (txt) => txt.replace(/\u001b\[[0-9;]*m/g, '');
|
|
3
|
+
export const success = chalk.green;
|
|
4
|
+
export const warn = chalk.hex('#FFA500');
|
|
5
|
+
export const info = chalk.hex('#77CFF5');
|
|
6
|
+
export const error = chalk.red;
|
|
7
|
+
export const hop = chalk.hex('#F577F5');
|
|
8
|
+
export const cyan = chalk.cyan;
|
|
9
|
+
export const bold = chalk.bold;
|
|
10
|
+
export const dim = chalk.dim;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PARSE_TYPES } from '../types/parse-types.js';
|
|
2
|
+
const BOOL_VALUES = Object.freeze(['Y', 'y', 'yes', 'YES', 'x', 'v', 'o', 'true']);
|
|
3
|
+
export const parse = (value, type) => {
|
|
4
|
+
switch (type) {
|
|
5
|
+
case PARSE_TYPES.number:
|
|
6
|
+
return value ? Number(value) : 0;
|
|
7
|
+
case PARSE_TYPES.confirm:
|
|
8
|
+
return BOOL_VALUES.includes(value);
|
|
9
|
+
default:
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export const parseView = (value, type) => {
|
|
14
|
+
switch (type) {
|
|
15
|
+
case PARSE_TYPES.number:
|
|
16
|
+
return value ? Number(value) : 0;
|
|
17
|
+
case PARSE_TYPES.confirm:
|
|
18
|
+
return value === 'true';
|
|
19
|
+
default:
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../_internal/types/inquirer.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../_internal/types/inquirer.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../_internal/types/parse-types.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../_internal/types/parse-types.js';
|