inquirer-plugins 1.0.0-alpha.7 → 1.0.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 +5 -34
- package/dist/lib/_internal/plugins/table/table.d.ts +2 -0
- package/dist/lib/_internal/plugins/table/table.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/table.js +1 -0
- package/dist/lib/_internal/plugins/table/types/columns.d.ts +21 -0
- package/dist/lib/_internal/plugins/table/types/columns.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/types/columns.js +2 -0
- package/dist/lib/_internal/plugins/table/types/common.d.ts +25 -0
- package/dist/lib/_internal/plugins/table/types/common.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/types/common.js +5 -0
- package/dist/lib/_internal/plugins/table/types/rows.d.ts +2 -0
- package/dist/lib/_internal/plugins/table/types/rows.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/types/table.d.ts +27 -0
- package/dist/lib/_internal/plugins/table/types/table.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/types.d.ts +27 -0
- package/dist/lib/_internal/plugins/table/types.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/types.js +24 -0
- package/dist/lib/_internal/plugins/table/utils.d.ts +25 -0
- package/dist/lib/_internal/plugins/table/utils.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table/utils.js +179 -0
- package/dist/lib/_internal/plugins/table.d.ts +11 -0
- package/dist/lib/_internal/plugins/table.d.ts.map +1 -0
- package/dist/lib/_internal/plugins/table.js +173 -0
- package/dist/lib/_internal/types/chalk.d.ts +7 -0
- package/dist/lib/_internal/types/chalk.d.ts.map +1 -0
- package/dist/lib/_internal/types/chalk.js +1 -0
- package/dist/lib/_internal/types/inquirer.d.ts +6 -0
- package/dist/lib/_internal/types/inquirer.d.ts.map +1 -0
- package/dist/lib/_internal/types/inquirer.js +5 -0
- package/dist/lib/_internal/types/parse-types.d.ts +7 -0
- package/dist/lib/_internal/types/parse-types.d.ts.map +1 -0
- package/{src/_internal/types/parse-types.ts → dist/lib/_internal/types/parse-types.js} +1 -3
- package/dist/lib/_internal/types/table.d.ts +15 -0
- package/dist/lib/_internal/types/table.d.ts.map +1 -0
- package/dist/lib/_internal/types/table.js +1 -0
- package/dist/lib/_internal/utils/chalk.d.ts +12 -0
- package/dist/lib/_internal/utils/chalk.d.ts.map +1 -0
- package/dist/lib/_internal/utils/chalk.js +29 -0
- package/dist/lib/_internal/utils/columns.d.ts +7 -0
- package/dist/lib/_internal/utils/columns.d.ts.map +1 -0
- package/dist/lib/_internal/utils/columns.js +28 -0
- package/dist/lib/_internal/utils/common.d.ts +4 -0
- package/dist/lib/_internal/utils/common.d.ts.map +1 -0
- package/dist/lib/_internal/utils/common.js +3 -0
- package/dist/lib/_internal/utils/keypress.d.ts +12 -0
- package/dist/lib/_internal/utils/keypress.d.ts.map +1 -0
- package/dist/lib/_internal/utils/keypress.js +28 -0
- package/dist/lib/_internal/utils/parse-types.d.ts +4 -0
- package/dist/lib/_internal/utils/parse-types.d.ts.map +1 -0
- package/{src/_internal/utils/parse-types.ts → dist/lib/_internal/utils/parse-types.js} +3 -7
- package/dist/lib/_internal/utils/table/arrow-key.d.ts +14 -0
- package/dist/lib/_internal/utils/table/arrow-key.d.ts.map +1 -0
- package/dist/lib/_internal/utils/table/arrow-key.js +24 -0
- package/dist/lib/_internal/utils/table/cell.d.ts +3 -0
- package/dist/lib/_internal/utils/table/cell.d.ts.map +1 -0
- package/dist/lib/_internal/utils/table/cell.js +6 -0
- package/dist/lib/_internal/utils/table/columns.d.ts +3 -0
- package/dist/lib/_internal/utils/table/columns.d.ts.map +1 -0
- package/dist/lib/_internal/utils/table/columns.js +29 -0
- package/dist/lib/_internal/utils/table/pagenate.d.ts +4 -0
- package/dist/lib/_internal/utils/table/pagenate.d.ts.map +1 -0
- package/dist/lib/_internal/utils/table/pagenate.js +20 -0
- package/dist/lib/_internal/utils/table/rows.d.ts +3 -0
- package/dist/lib/_internal/utils/table/rows.d.ts.map +1 -0
- package/dist/lib/_internal/utils/table/rows.js +4 -0
- package/dist/lib/modules/table.d.ts +2 -0
- package/dist/lib/modules/table.d.ts.map +1 -0
- package/dist/lib/modules/table.js +1 -0
- package/dist/lib/modules/types/chalk.d.ts +2 -0
- package/dist/lib/modules/types/chalk.d.ts.map +1 -0
- package/dist/lib/modules/types/chalk.js +1 -0
- package/dist/{modules → lib/modules}/types/inquirer.d.ts +1 -0
- package/dist/lib/modules/types/inquirer.d.ts.map +1 -0
- package/dist/{modules → lib/modules}/types/parse-types.d.ts +1 -0
- package/dist/lib/modules/types/parse-types.d.ts.map +1 -0
- package/dist/lib/modules/types/table.d.ts +2 -0
- package/dist/lib/modules/types/table.d.ts.map +1 -0
- package/dist/lib/modules/types/table.js +1 -0
- package/package.json +27 -103
- package/LICENSE +0 -21
- package/dist/_internal/plugins/table/table.d.ts +0 -15
- package/dist/_internal/plugins/table/table.js +0 -77
- package/dist/_internal/plugins/table/types.d.ts +0 -47
- package/dist/_internal/plugins/table/types.js +0 -23
- package/dist/_internal/plugins/table/utils.d.ts +0 -3
- package/dist/_internal/plugins/table/utils.js +0 -22
- package/dist/_internal/plugins/table.d.ts +0 -49
- package/dist/_internal/plugins/table.js +0 -263
- package/dist/_internal/plugins/test.js +0 -50
- package/dist/_internal/types/inquirer.d.ts +0 -1
- package/dist/_internal/types/parse-types.d.ts +0 -6
- package/dist/_internal/types/parse-types.js +0 -5
- package/dist/_internal/utils/chalk.d.ts +0 -9
- package/dist/_internal/utils/chalk.js +0 -10
- package/dist/_internal/utils/keypress.d.ts +0 -4
- package/dist/_internal/utils/keypress.js +0 -20
- package/dist/_internal/utils/parse-types.d.ts +0 -3
- package/dist/_internal/utils/parse-types.js +0 -23
- package/dist/modules/table.d.ts +0 -2
- package/dist/modules/table.js +0 -2
- package/src/_internal/plugins/table/README.md +0 -117
- package/src/_internal/plugins/table/table.ts +0 -93
- package/src/_internal/plugins/table/types.ts +0 -74
- package/src/_internal/plugins/table/utils.ts +0 -25
- package/src/_internal/plugins/table.ts +0 -305
- package/src/_internal/plugins/test.ts +0 -76
- package/src/_internal/types/inquirer.ts +0 -1
- package/src/_internal/utils/chalk.ts +0 -13
- package/src/_internal/utils/keypress.ts +0 -23
- package/src/modules/table.ts +0 -2
- package/src/modules/types/inquirer.ts +0 -1
- package/src/modules/types/parse-types.ts +0 -1
- package/table/package.json +0 -6
- package/tsconfig.json +0 -27
- package/types/inquirer/package.json +0 -6
- package/types/parse-types/package.json +0 -6
- /package/dist/{_internal/plugins/test.d.ts → lib/_internal/plugins/table/types/rows.js} +0 -0
- /package/dist/{_internal/types/inquirer.js → lib/_internal/plugins/table/types/table.js} +0 -0
- /package/dist/{modules → lib/modules}/types/inquirer.js +0 -0
- /package/dist/{modules → lib/modules}/types/parse-types.js +0 -0
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
<!-- MD_HOPPER: ID: src-_internal-plugins-table-rmd -->
|
|
2
|
-
<!-- MD_HOPPER: TITLE: TABLE PLUGIN -->
|
|
3
|
-
<!-- MD_HOPPER: OUTPUT: README.md -->
|
|
4
|
-
|
|
5
|
-
`Table Plugin` is based on the original [inquirer-table-input](https://github.com/edelciomolina/inquirer-table-input).
|
|
6
|
-
This plugin has been customized to support TypeScript and symbol input.
|
|
7
|
-
|
|
8
|
-
```ts
|
|
9
|
-
import chalk from 'chalk'
|
|
10
|
-
import inquirer from 'inquirer'
|
|
11
|
-
import { TablePlugin, TableQuestion } from 'inquirer-plugins/table'
|
|
12
|
-
|
|
13
|
-
inquirer.registerPrompt('table', TablePlugin)
|
|
14
|
-
|
|
15
|
-
const question: TableQuestion = {
|
|
16
|
-
type: 'table',
|
|
17
|
-
name: 'settings',
|
|
18
|
-
message: 'SETTINGS',
|
|
19
|
-
description: `You can edit data.`,
|
|
20
|
-
disableColumnIndexes: [0],
|
|
21
|
-
colors: {
|
|
22
|
-
selected: chalk.hex('#FFA500'),
|
|
23
|
-
editable: chalk.green,
|
|
24
|
-
editing: chalk.hex('#F577F5'),
|
|
25
|
-
},
|
|
26
|
-
columns: [
|
|
27
|
-
{ name: cyan.bold('ID'), value: 'id', type: 'input' },
|
|
28
|
-
{
|
|
29
|
-
name: cyan.bold('filename'),
|
|
30
|
-
value: 'filename',
|
|
31
|
-
type: 'input',
|
|
32
|
-
parser: (input: string, _inputs, _answers) => (/\.md$/.test(input) ? input : `${input}.md`),
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
name: cyan.bold('count'),
|
|
36
|
-
value: 'cnt',
|
|
37
|
-
type: 'number',
|
|
38
|
-
validate: (input: string, _inputs, _answers) => {
|
|
39
|
-
const isValid = Number(input) > 0
|
|
40
|
-
return {
|
|
41
|
-
isValid,
|
|
42
|
-
message: isValid ? '' : error('should be more than 0'),
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
{ name: cyan.bold('IS LOCK'), value: 'lock', type: 'confirm' },
|
|
47
|
-
],
|
|
48
|
-
rows: [
|
|
49
|
-
[chalk.bold('ABCD'), 'hoge', 0, true],
|
|
50
|
-
[chalk.bold('EFG'), 'foo', 1, false],
|
|
51
|
-
],
|
|
52
|
-
validate: (inputs, _answers) => {
|
|
53
|
-
const isValid = inputs.every((i: any) => Object.values(i).every(v => `${v}`.length))
|
|
54
|
-
return {
|
|
55
|
-
isValid,
|
|
56
|
-
message: isValid ? '' : 'not allow empty input',
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
}
|
|
60
|
-
inquirer.prompt([question])
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
> In the [inquirer-table-input](https://github.com/edelciomolina/inquirer-table-input#usage), it was necessary to set validation to false, but this plugin makes it configurable by separating the validation and processing within Inquirer. Instead of using a boolean, changes have been made to pass isValid and message.
|
|
64
|
-
|
|
65
|
-
```json
|
|
66
|
-
{
|
|
67
|
-
"settings": {
|
|
68
|
-
"state": true,
|
|
69
|
-
"result": [
|
|
70
|
-
{
|
|
71
|
-
"id": "ABCD",
|
|
72
|
-
"filename": "hoge.md",
|
|
73
|
-
"cnt": 0,
|
|
74
|
-
"lock": true
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
"id": "EFG",
|
|
78
|
-
"filename": "foo.md",
|
|
79
|
-
"cnt": 1,
|
|
80
|
-
"lock": false
|
|
81
|
-
}
|
|
82
|
-
]
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Params
|
|
88
|
-
|
|
89
|
-
- **type** [*required*]
|
|
90
|
-
- **name** [*required*]
|
|
91
|
-
- **message** [*optional*] (String | ChalkInstance)
|
|
92
|
-
- **description** [*optional*] (String | ChalkInstance)
|
|
93
|
-
- **escapeMessage** [*optional*] (String | ChalkInstance)
|
|
94
|
-
- **confirmMessage** [*optional*] (String | ChalkInstance)
|
|
95
|
-
- **disableColumnIndexes** [*optional*] (number[])
|
|
96
|
-
- **colors** [*optional*]
|
|
97
|
-
- _selected_ [*optional*] (ChalkInstance)
|
|
98
|
-
- _editable_ [*optional*] (ChalkInstance)
|
|
99
|
-
- _editing_ [*optional*] (ChalkInstance)
|
|
100
|
-
- **columns** [*required*]
|
|
101
|
-
- **rows** [*required*]
|
|
102
|
-
- **validate** [*optional*] (() => { isValid: boolean, message: string })
|
|
103
|
-
- **pageSize** [*optional*] number
|
|
104
|
-
|
|
105
|
-
## Column Params
|
|
106
|
-
|
|
107
|
-
- **name**: [*required*] (String | ChalkInstance) To show table header colum name.
|
|
108
|
-
- **value**: [*required*] (String) result param property name.
|
|
109
|
-
- **type**: [*required*] (String) `input`, `number`, `confirm`
|
|
110
|
-
- **parser**: [*optional*] (() => any) table view & result parser
|
|
111
|
-
- **validate**: [*optional*] (() => { isValid: boolean, message: string }) validate cell
|
|
112
|
-
|
|
113
|
-
## Raws (number | string | boolean)
|
|
114
|
-
|
|
115
|
-
<!-- MD_HOPPER: BEGIN_DEFINE_LINKS: -->
|
|
116
|
-
|
|
117
|
-
<!-- MD_HOPPER: END_DEFINE_LINKS: -->
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { Answers } from 'inquirer'
|
|
2
|
-
|
|
3
|
-
import { PARSE_TYPES } from '~types/parse-types.js'
|
|
4
|
-
import { trim, error } from '~utils/chalk.js'
|
|
5
|
-
import { parse } from '~utils/parse-types.js'
|
|
6
|
-
|
|
7
|
-
import { Column, Cell, RawInputs } from './types.js'
|
|
8
|
-
|
|
9
|
-
export class TableClass {
|
|
10
|
-
columns: Column[] = []
|
|
11
|
-
rows: string[][] = []
|
|
12
|
-
|
|
13
|
-
constructor(columns: Column[], rows: Cell[][]) {
|
|
14
|
-
this.columns = columns
|
|
15
|
-
this.rows = rows.map(columns => columns.map(value => `${value}`))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get header() {
|
|
19
|
-
return this.columns.map(({ name }) => `${name}`)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
getColumnType(columnIndex: number) {
|
|
23
|
-
if (!(columnIndex in this.columns)) {
|
|
24
|
-
return PARSE_TYPES.input
|
|
25
|
-
}
|
|
26
|
-
return this.columns[columnIndex].type
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
getParser(columnIndex: number): Exclude<Column['parser'], undefined> {
|
|
30
|
-
return this.columns[columnIndex].parser || ((txt: string, _inputs: RawInputs, _answers: Answers) => txt)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getValidator(columnIndex: number): Exclude<Column['validate'], undefined> {
|
|
34
|
-
return (
|
|
35
|
-
this.columns[columnIndex].validate ||
|
|
36
|
-
((_txt: string, _inputs: RawInputs, _answers: Answers) => ({ isValid: true, message: '' }))
|
|
37
|
-
)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
updateRow(rowIndex: number, columnIndex: number, value: string, answers: Answers) {
|
|
41
|
-
if (!(rowIndex in this.rows)) {
|
|
42
|
-
return error(`row index (${rowIndex}) not found`)
|
|
43
|
-
}
|
|
44
|
-
const columns = this.rows[rowIndex]
|
|
45
|
-
if (!(columnIndex in columns)) {
|
|
46
|
-
return error(`column index (${columnIndex}) not found`)
|
|
47
|
-
}
|
|
48
|
-
const validateResult = this.getValidator(columnIndex)(value, this.getRaw(), answers)
|
|
49
|
-
if (!validateResult.isValid) {
|
|
50
|
-
return validateResult.message
|
|
51
|
-
}
|
|
52
|
-
const parser = this.getParser(columnIndex)
|
|
53
|
-
const parsed = parser(value, this.getRaw(), answers)
|
|
54
|
-
columns[columnIndex] = `${parsed}`
|
|
55
|
-
return ''
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
getValue(rowIndex: number, columnIndex: number) {
|
|
59
|
-
if (!(rowIndex in this.rows)) {
|
|
60
|
-
return ''
|
|
61
|
-
}
|
|
62
|
-
const columns = this.rows[rowIndex]
|
|
63
|
-
if (!(columnIndex in columns)) {
|
|
64
|
-
return ''
|
|
65
|
-
}
|
|
66
|
-
return columns[columnIndex]
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
getRaw(): RawInputs {
|
|
70
|
-
const result: RawInputs = []
|
|
71
|
-
for (let i = 0; i < this.rows.length; i++) {
|
|
72
|
-
result.push({})
|
|
73
|
-
for (let ii = 0; ii < (this.rows[i] || []).length; ii++) {
|
|
74
|
-
const { value: key } = this.columns[ii]
|
|
75
|
-
result[i][key] = trim(this.rows[i][ii])
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return result
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getResult(answers: Answers) {
|
|
82
|
-
const result: Record<string, Cell>[] = []
|
|
83
|
-
for (let i = 0; i < this.rows.length; i++) {
|
|
84
|
-
result.push({})
|
|
85
|
-
for (let ii = 0; ii < (this.rows[i] || []).length; ii++) {
|
|
86
|
-
const { value: key, type, parser } = this.columns[ii]
|
|
87
|
-
const value = trim(this.rows[i][ii])
|
|
88
|
-
result[i][key] = parser ? parser(value, this.getRaw(), answers) : parse(value, type)
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return result
|
|
92
|
-
}
|
|
93
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import chalk, { ChalkInstance } from 'chalk'
|
|
2
|
-
import { Answers } from 'inquirer'
|
|
3
|
-
|
|
4
|
-
import { ParseType } from '~types/parse-types.js'
|
|
5
|
-
import { hop, success, warn } from '~utils/chalk.js'
|
|
6
|
-
|
|
7
|
-
export const DEFAULT_PAGE_SIZE = 5
|
|
8
|
-
export const INDEX_ERRORS = Object.freeze({
|
|
9
|
-
excess: 'excess',
|
|
10
|
-
disabled: 'disabled',
|
|
11
|
-
} as const)
|
|
12
|
-
|
|
13
|
-
export interface DefaultOptions extends Required<Omit<TableQuestion, 'type' | 'name' | 'colors'>> {
|
|
14
|
-
colors: Colors
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const DEFAULT_OPTIONS: DefaultOptions = {
|
|
18
|
-
message: '',
|
|
19
|
-
description: '',
|
|
20
|
-
escapeMessage: chalk.red('Press ESC again to exit!'),
|
|
21
|
-
confirmMessage: chalk.red('Press ENTER again to confirm!'),
|
|
22
|
-
disableColumnIndexes: [],
|
|
23
|
-
colors: {
|
|
24
|
-
selected: warn,
|
|
25
|
-
editable: success,
|
|
26
|
-
editing: hop,
|
|
27
|
-
},
|
|
28
|
-
columns: [],
|
|
29
|
-
rows: [],
|
|
30
|
-
pageSize: DEFAULT_PAGE_SIZE,
|
|
31
|
-
validate: () => ({ isValid: true, message: '' }),
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface RequiredOptions extends Required<Omit<TableQuestion, 'colors'>> {
|
|
35
|
-
colors: Colors
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface Colors {
|
|
39
|
-
selected: ChalkInstance
|
|
40
|
-
editable: ChalkInstance
|
|
41
|
-
editing: ChalkInstance
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface ValidateResult {
|
|
45
|
-
isValid: boolean
|
|
46
|
-
message: string
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface TableQuestion {
|
|
50
|
-
type: string
|
|
51
|
-
name: string
|
|
52
|
-
message?: string | ChalkInstance
|
|
53
|
-
description?: string | ChalkInstance
|
|
54
|
-
escapeMessage?: string | ChalkInstance
|
|
55
|
-
confirmMessage?: string | ChalkInstance
|
|
56
|
-
disableColumnIndexes?: number[]
|
|
57
|
-
colors?: Partial<Colors>
|
|
58
|
-
columns: Column[]
|
|
59
|
-
rows: Cell[][]
|
|
60
|
-
validate?: (...args: any[]) => ValidateResult
|
|
61
|
-
pageSize?: number
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface Column {
|
|
65
|
-
name: string | ChalkInstance
|
|
66
|
-
value: string
|
|
67
|
-
type: ParseType
|
|
68
|
-
validate?: (input: string, raws: RawInputs, answers: Answers) => ValidateResult
|
|
69
|
-
parser?: (input: any, raws: RawInputs, answers: Answers) => any
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export type Cell = number | string | boolean
|
|
73
|
-
|
|
74
|
-
export type RawInputs = Record<string, string>[]
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { INDEX_ERRORS } from '~internal/plugins/table/types.js'
|
|
2
|
-
|
|
3
|
-
export const isNoChoiceOfIndex = (maxIndex: number, disableIndexes: number[]) => {
|
|
4
|
-
return disableIndexes.length > maxIndex && [...Array(maxIndex).keys()].every(index => disableIndexes.includes(index))
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const gateValidateColumnIndexError = (index: number, maxIndex: number, disableIndexes: number[]) => {
|
|
8
|
-
if (index > maxIndex || index < 0) {
|
|
9
|
-
return INDEX_ERRORS.excess
|
|
10
|
-
}
|
|
11
|
-
if (disableIndexes.includes(index)) {
|
|
12
|
-
return INDEX_ERRORS.disabled
|
|
13
|
-
}
|
|
14
|
-
return false
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const getNextIndex = (index: number, maxIndex: number, num: number) => {
|
|
18
|
-
if (index > maxIndex) {
|
|
19
|
-
return 0
|
|
20
|
-
}
|
|
21
|
-
if (index < 0) {
|
|
22
|
-
return maxIndex
|
|
23
|
-
}
|
|
24
|
-
return index > maxIndex ? 0 : index + num
|
|
25
|
-
}
|
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { Interface as ReadLineInterface, Key } from 'readline'
|
|
2
|
-
|
|
3
|
-
import { ChalkInstance } from 'chalk'
|
|
4
|
-
import cliCursor from 'cli-cursor'
|
|
5
|
-
import Table from 'cli-table3'
|
|
6
|
-
import { Question, Answers } from 'inquirer'
|
|
7
|
-
import Base from 'inquirer/lib/prompts/base.js'
|
|
8
|
-
import observe from 'inquirer/lib/utils/events.js'
|
|
9
|
-
import { map } from 'rxjs/operators'
|
|
10
|
-
|
|
11
|
-
import { Done } from '~types/inquirer.js'
|
|
12
|
-
import { dim, error } from '~utils/chalk.js'
|
|
13
|
-
import { getKeypressValue, getBackspaceText } from '~utils/keypress.js'
|
|
14
|
-
|
|
15
|
-
import { TableClass } from './table/table.js'
|
|
16
|
-
import { DEFAULT_OPTIONS, RequiredOptions, TableQuestion } from './table/types.js'
|
|
17
|
-
import { isNoChoiceOfIndex, gateValidateColumnIndexError, getNextIndex } from './table/utils.js'
|
|
18
|
-
|
|
19
|
-
export class TablePlugin extends Base {
|
|
20
|
-
table = new TableClass([], [])
|
|
21
|
-
options!: RequiredOptions
|
|
22
|
-
|
|
23
|
-
done!: Done
|
|
24
|
-
|
|
25
|
-
pointerIndexes = {
|
|
26
|
-
row: 0,
|
|
27
|
-
column: 0,
|
|
28
|
-
}
|
|
29
|
-
isNextCloses = {
|
|
30
|
-
escape: false,
|
|
31
|
-
}
|
|
32
|
-
editing = false
|
|
33
|
-
temporaryBackup: string | null = null
|
|
34
|
-
purTyping = ''
|
|
35
|
-
answers: Answers = []
|
|
36
|
-
|
|
37
|
-
constructor(question: Question<TableQuestion>, readLine: ReadLineInterface, answers: Answers) {
|
|
38
|
-
super({ ...question, validate: () => false } as Question, readLine, answers)
|
|
39
|
-
this.options = {
|
|
40
|
-
...DEFAULT_OPTIONS,
|
|
41
|
-
...(this.opt as any),
|
|
42
|
-
validate: question.validate || DEFAULT_OPTIONS.validate,
|
|
43
|
-
}
|
|
44
|
-
this.answers = answers
|
|
45
|
-
this.table = new TableClass(this.options.columns, this.options.rows)
|
|
46
|
-
|
|
47
|
-
if (isNoChoiceOfIndex(this.maxColumnIndex, this.options.disableColumnIndexes)) {
|
|
48
|
-
throw new Error('disableColumnIndexes contains all of index.')
|
|
49
|
-
}
|
|
50
|
-
this.pointerIndexes.row = 0
|
|
51
|
-
this.updateColumnIndex(1)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
getCurrentValues() {
|
|
55
|
-
return this.table.getResult(this.answers)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
isEditable(index: number) {
|
|
59
|
-
return !this.options.disableColumnIndexes.includes(index)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
_run(callback: Done) {
|
|
63
|
-
this.done = callback
|
|
64
|
-
|
|
65
|
-
const events = observe(this.rl)
|
|
66
|
-
const validation = this.handleSubmitEvents(events.line.pipe(map(this.getCurrentValues.bind(this))))
|
|
67
|
-
validation.success.forEach(this.onEnd.bind(this))
|
|
68
|
-
validation.error.forEach(this.onError.bind(this))
|
|
69
|
-
|
|
70
|
-
events.keypress.forEach(({ key }) => {
|
|
71
|
-
if (key.name !== 'escape') {
|
|
72
|
-
this.isNextCloses.escape = false
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!this.editing) {
|
|
76
|
-
switch (key.name) {
|
|
77
|
-
case 'down':
|
|
78
|
-
return this.onDownKey()
|
|
79
|
-
|
|
80
|
-
case 'up':
|
|
81
|
-
return this.onUpKey()
|
|
82
|
-
|
|
83
|
-
case 'left':
|
|
84
|
-
return this.onLeftKey()
|
|
85
|
-
|
|
86
|
-
case 'right':
|
|
87
|
-
return this.onRightKey()
|
|
88
|
-
|
|
89
|
-
case 'escape':
|
|
90
|
-
if (!this.isNextCloses.escape) {
|
|
91
|
-
this.isNextCloses.escape = true
|
|
92
|
-
return this.render()
|
|
93
|
-
} else {
|
|
94
|
-
this.render()
|
|
95
|
-
return this.onEnd(false)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
default:
|
|
99
|
-
if (this.isEditable(this.pointerIndexes.column)) {
|
|
100
|
-
this.editing = true
|
|
101
|
-
this.clear()
|
|
102
|
-
this.onEditPress(key)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
return this.onEditPress(key)
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
cliCursor.hide()
|
|
111
|
-
this.render()
|
|
112
|
-
|
|
113
|
-
return this
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
get maxPageIndex() {
|
|
117
|
-
return this.options.pageSize - 1
|
|
118
|
-
}
|
|
119
|
-
get maxRowIndex() {
|
|
120
|
-
return this.table.rows.length - 1
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
get maxColumnIndex() {
|
|
124
|
-
return this.table.columns.length - 1
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
onDownKey() {
|
|
128
|
-
if (this.pointerIndexes.row < this.maxRowIndex) {
|
|
129
|
-
++this.pointerIndexes.row
|
|
130
|
-
}
|
|
131
|
-
this.render()
|
|
132
|
-
}
|
|
133
|
-
onUpKey() {
|
|
134
|
-
if (this.pointerIndexes.row > 0) {
|
|
135
|
-
--this.pointerIndexes.row
|
|
136
|
-
}
|
|
137
|
-
this.render()
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
updateColumnIndex(num: number) {
|
|
141
|
-
let index = this.pointerIndexes.column
|
|
142
|
-
for (let c = 0; c <= this.maxColumnIndex; c++) {
|
|
143
|
-
index = getNextIndex(index, this.maxColumnIndex, num)
|
|
144
|
-
const error = gateValidateColumnIndexError(index, this.maxColumnIndex, this.options.disableColumnIndexes)
|
|
145
|
-
if (!error) {
|
|
146
|
-
break
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
this.pointerIndexes.column = index
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
onLeftKey() {
|
|
153
|
-
this.updateColumnIndex(-1)
|
|
154
|
-
this.render()
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
onRightKey() {
|
|
158
|
-
this.updateColumnIndex(1)
|
|
159
|
-
this.render()
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
clear() {
|
|
163
|
-
this.temporaryBackup = null
|
|
164
|
-
this.purTyping = ''
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
onEnd(state: any) {
|
|
168
|
-
this.render(true)
|
|
169
|
-
this.status = 'answered'
|
|
170
|
-
this.screen.done()
|
|
171
|
-
cliCursor.show()
|
|
172
|
-
if (state) {
|
|
173
|
-
return this.done({
|
|
174
|
-
state,
|
|
175
|
-
result: this.getCurrentValues(),
|
|
176
|
-
})
|
|
177
|
-
} else {
|
|
178
|
-
return this.done({
|
|
179
|
-
state,
|
|
180
|
-
})
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
validate() {
|
|
185
|
-
return this.options.validate(this.getCurrentValues(), this.answers)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
onError(_state: any) {
|
|
189
|
-
if (this.editing) {
|
|
190
|
-
this.editing = false
|
|
191
|
-
this.render()
|
|
192
|
-
} else {
|
|
193
|
-
if (this.validate().isValid) {
|
|
194
|
-
return this.onEnd(true)
|
|
195
|
-
} else {
|
|
196
|
-
return this.render()
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
updateChoice(value: string) {
|
|
202
|
-
return this.table.updateRow(this.pointerIndexes.row, this.pointerIndexes.column, value, this.answers)
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
get currentValue() {
|
|
206
|
-
return this.table.getValue(this.pointerIndexes.row, this.pointerIndexes.column)
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
onEditPress(key: Key) {
|
|
210
|
-
if (this.temporaryBackup === null) {
|
|
211
|
-
this.temporaryBackup = this.currentValue
|
|
212
|
-
}
|
|
213
|
-
let value = this.purTyping
|
|
214
|
-
switch (key.name) {
|
|
215
|
-
case 'escape':
|
|
216
|
-
this.purTyping = ''
|
|
217
|
-
value = this.temporaryBackup
|
|
218
|
-
this.editing = false
|
|
219
|
-
break
|
|
220
|
-
case 'delete':
|
|
221
|
-
this.purTyping = ''
|
|
222
|
-
value = this.purTyping
|
|
223
|
-
break
|
|
224
|
-
case 'backspace': {
|
|
225
|
-
this.purTyping = getBackspaceText(this.purTyping)
|
|
226
|
-
value = this.purTyping
|
|
227
|
-
break
|
|
228
|
-
}
|
|
229
|
-
default:
|
|
230
|
-
this.purTyping = `${this.purTyping}${getKeypressValue(key)}`
|
|
231
|
-
value = this.purTyping
|
|
232
|
-
break
|
|
233
|
-
}
|
|
234
|
-
const message = this.updateChoice(value)
|
|
235
|
-
this.render(false, message)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
getShowRowIndex() {
|
|
239
|
-
const middleOfPage = Math.floor(this.options.pageSize / 2)
|
|
240
|
-
let rowIndexFrom = Math.max(0, this.pointerIndexes.row - middleOfPage)
|
|
241
|
-
const rowIndexTo = Math.min(this.maxRowIndex, rowIndexFrom + this.maxPageIndex)
|
|
242
|
-
rowIndexFrom = Math.min(rowIndexFrom, rowIndexTo - (this.options.pageSize - 1))
|
|
243
|
-
return { rowIndexFrom, rowIndexTo }
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
isSelected(rowIndex: number, columnIndex: number) {
|
|
247
|
-
return this.pointerIndexes.row === rowIndex && this.pointerIndexes.column === columnIndex
|
|
248
|
-
}
|
|
249
|
-
getColor(_value: string, isSelected: boolean, isEditable: boolean) {
|
|
250
|
-
const value = isEditable ? _value : dim(_value)
|
|
251
|
-
if (!isSelected) {
|
|
252
|
-
return value
|
|
253
|
-
}
|
|
254
|
-
if (this.editing) {
|
|
255
|
-
return isEditable ? this.options.colors.editing(value) : this.options.colors.selected(value)
|
|
256
|
-
}
|
|
257
|
-
return isEditable ? this.options.colors.editable(value) : this.options.colors.selected(value)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
render(isDisconnect = false, message = '') {
|
|
261
|
-
let content = this.getQuestion()
|
|
262
|
-
let bottomContent: string | ChalkInstance = ''
|
|
263
|
-
|
|
264
|
-
content += this.options.description ? `\n${this.options.description}` : ''
|
|
265
|
-
|
|
266
|
-
const { rowIndexFrom, rowIndexTo } = this.getShowRowIndex()
|
|
267
|
-
const table = new Table({
|
|
268
|
-
head: this.table.header,
|
|
269
|
-
})
|
|
270
|
-
for (let i = 0; i < this.table.rows.length; i++) {
|
|
271
|
-
if (i < rowIndexFrom || i > rowIndexTo) {
|
|
272
|
-
continue
|
|
273
|
-
}
|
|
274
|
-
const columnValues: string[] = []
|
|
275
|
-
for (let ii = 0; ii < (this.table.rows[i] || []).length; ii++) {
|
|
276
|
-
const isEditable = this.isEditable(ii)
|
|
277
|
-
const value = this.table.rows[i][ii]
|
|
278
|
-
const parser = this.table.getParser(ii)
|
|
279
|
-
columnValues.push(
|
|
280
|
-
this.getColor(parser(value, this.table.getRaw(), this.answers), this.isSelected(i, ii), isEditable)
|
|
281
|
-
)
|
|
282
|
-
}
|
|
283
|
-
table.push(columnValues)
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
content += '\n\n' + table.toString()
|
|
287
|
-
|
|
288
|
-
if (!isDisconnect) {
|
|
289
|
-
const validateResult = this.validate()
|
|
290
|
-
if (!this.editing && validateResult.isValid) {
|
|
291
|
-
bottomContent = this.options.confirmMessage
|
|
292
|
-
}
|
|
293
|
-
if (!validateResult.isValid) {
|
|
294
|
-
bottomContent = validateResult.message || error('Something went wrong.')
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
if (this.isNextCloses.escape) {
|
|
298
|
-
bottomContent = this.options.escapeMessage
|
|
299
|
-
}
|
|
300
|
-
if (message) {
|
|
301
|
-
bottomContent = message
|
|
302
|
-
}
|
|
303
|
-
this.screen.render(content, bottomContent as string)
|
|
304
|
-
}
|
|
305
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
import inquirer from 'inquirer'
|
|
3
|
-
|
|
4
|
-
import { success, warn, hop, cyan, error } from '../utils/chalk.js'
|
|
5
|
-
|
|
6
|
-
import { TableQuestion } from './table/types.js'
|
|
7
|
-
import { TablePlugin } from './table.js'
|
|
8
|
-
|
|
9
|
-
const run = async () => {
|
|
10
|
-
inquirer.registerPrompt('table-input', TablePlugin)
|
|
11
|
-
// const rows = [...Array(33).keys()].map(i => [cyan(`--path ${i}--`), '--id--', true, i + 100, '--lock--'])
|
|
12
|
-
const question: TableQuestion = {
|
|
13
|
-
type: 'table-input',
|
|
14
|
-
name: 'settings',
|
|
15
|
-
message: 'SETTINGS',
|
|
16
|
-
description: `You can edit data.`,
|
|
17
|
-
disableColumnIndexes: [0],
|
|
18
|
-
colors: {
|
|
19
|
-
selected: warn,
|
|
20
|
-
editable: success,
|
|
21
|
-
editing: hop,
|
|
22
|
-
},
|
|
23
|
-
// columns: [
|
|
24
|
-
// { name: cyan.bold('filepath'), value: 'path', type: 'input' },
|
|
25
|
-
// { name: cyan.bold('id'), value: 'id', type: 'input', parser: () => 'aaa' },
|
|
26
|
-
// { name: cyan.bold('title'), value: 'title', type: 'confirm' },
|
|
27
|
-
// {
|
|
28
|
-
// name: cyan.bold('num'),
|
|
29
|
-
// value: 'output',
|
|
30
|
-
// type: 'number',
|
|
31
|
-
// validate: (value: string) => /^\d*$/.test(value) || value === '0' || !value,
|
|
32
|
-
// },
|
|
33
|
-
// { name: cyan.bold('lock'), value: 'lock', type: 'input' },
|
|
34
|
-
// ],
|
|
35
|
-
// rows: rows,
|
|
36
|
-
columns: [
|
|
37
|
-
{ name: cyan.bold('ID'), value: 'id', type: 'input' },
|
|
38
|
-
// { name: cyan.bold('title'), value: 'title', type: 'input', parser: (input: string) => `${input}.md` },
|
|
39
|
-
{ name: cyan.bold('title'), value: 'title', type: 'input', parser: (input: string) => /\.md$/.test(input) ? input : `${input}.md` },
|
|
40
|
-
{ name: cyan.bold('count'), value: 'cnt', type: 'number', validate: (input) => {
|
|
41
|
-
const isValid = input !== '0'
|
|
42
|
-
return {
|
|
43
|
-
isValid,
|
|
44
|
-
message: isValid ? '' : error('should be more than 0')
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
{ name: cyan.bold('IS LOCK'), value: 'lock', type: 'confirm' },
|
|
49
|
-
],
|
|
50
|
-
rows: [
|
|
51
|
-
[chalk.bold('ABCD'), 'hoge', 0, true],
|
|
52
|
-
[chalk.bold('EFG'), 'foo', 1, false]
|
|
53
|
-
],
|
|
54
|
-
validate: (inputs, _answers) => {
|
|
55
|
-
const isValid = inputs.every((i: any) => Object.values(i).every(v => `${v}`.length))
|
|
56
|
-
return {
|
|
57
|
-
isValid,
|
|
58
|
-
message: isValid ? '' : 'not allow empty input',
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
}
|
|
62
|
-
const answers = await inquirer.prompt([
|
|
63
|
-
// {
|
|
64
|
-
// type: 'confirm',
|
|
65
|
-
// name: 'id',
|
|
66
|
-
// message: 'type id',
|
|
67
|
-
// choices: ['aa', 'bb']
|
|
68
|
-
// },
|
|
69
|
-
question,
|
|
70
|
-
])
|
|
71
|
-
console.log('answers', JSON.stringify(answers, null, 2))
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
run().then(() => {
|
|
75
|
-
console.log('--- end ---')
|
|
76
|
-
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type Done = (value: any) => void
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line no-control-regex
|
|
4
|
-
export const trim = (txt: string) => txt.replace(/\u001b\[[0-9;]*m/g, '')
|
|
5
|
-
|
|
6
|
-
export const success = chalk.green
|
|
7
|
-
export const warn = chalk.hex('#FFA500')
|
|
8
|
-
export const info = chalk.hex('#77CFF5')
|
|
9
|
-
export const error = chalk.red
|
|
10
|
-
export const hop = chalk.hex('#F577F5')
|
|
11
|
-
export const cyan = chalk.cyan
|
|
12
|
-
export const bold = chalk.bold
|
|
13
|
-
export const dim = chalk.dim
|