|
1 | | -// import ora from 'ora' |
2 | | -import {Injectable} from '@nestjs/common'; |
3 | | -import {prompt, Separator} from 'inquirer'; |
4 | | -import {getTable} from 'console.table' |
5 | | - |
6 | | -@Injectable() |
7 | | -export class UIService { |
8 | | - |
9 | | - public async table<T>(config: { |
10 | | - name: string, |
11 | | - message: string, |
12 | | - printColNum?: boolean, |
13 | | - rows: Array<{ row: Record<string, unknown>, short: string, value: T }>, |
14 | | - }): Promise<T> { |
15 | | - |
16 | | - |
17 | | - const table = getTable(config.rows.map(({row}, index: number) => { |
18 | | - return config.printColNum === false ? row : ({'#': index + 1, ...row}); |
19 | | - })) |
20 | | - |
21 | | - const [header, separator, ...rows] = table.trim().split('\n') |
22 | | - return this.list({ |
23 | | - name: config.name, |
24 | | - message: config.message, |
25 | | - choices: [ |
26 | | - new Separator(header), |
27 | | - new Separator(separator), |
28 | | - ...rows.map((name: string, index: number) => ({ |
29 | | - name, |
30 | | - short: config.rows[index].short, |
31 | | - value: config.rows[index].value, |
32 | | - })), |
33 | | - new Separator(separator), |
34 | | - new Separator(' '.repeat(separator.length)), |
35 | | - ], |
36 | | - }) |
37 | | - } |
38 | | - |
39 | | - public async list<T>(config: { |
40 | | - name: string, |
41 | | - message: string, |
42 | | - choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>, |
43 | | - }): Promise<T> { |
44 | | - |
45 | | - const separatorCount = config |
46 | | - .choices |
47 | | - .filter((c) => c instanceof Separator) |
48 | | - .length |
49 | | - |
50 | | - const res = await prompt([{ |
51 | | - type: 'list', |
52 | | - name: config.name, |
53 | | - pageSize: process.stdout.rows - separatorCount - 1, |
54 | | - message: config.message, |
55 | | - choices: config.choices, |
56 | | - }]) |
57 | | - |
58 | | - return res[config.name] as T |
59 | | - |
60 | | - } |
61 | | - |
62 | | -} |
| 1 | +// import ora from 'ora' |
| 2 | +import {Injectable} from '@nestjs/common'; |
| 3 | +import prompt, {Separator} from 'inquirer'; |
| 4 | +import {getTable} from 'console.table' |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class UIService { |
| 8 | + |
| 9 | + public async table<T>(config: { |
| 10 | + name: string, |
| 11 | + message: string, |
| 12 | + printColNum?: boolean, |
| 13 | + rows: Array<{ row: Record<string, unknown>, short: string, value: T }>, |
| 14 | + }): Promise<T> { |
| 15 | + |
| 16 | + |
| 17 | + const table = getTable(config.rows.map(({row}, index: number) => { |
| 18 | + return config.printColNum === false ? row : ({'#': index + 1, ...row}); |
| 19 | + })) |
| 20 | + |
| 21 | + const [header, separator, ...rows] = table.trim().split('\n') |
| 22 | + return this.list({ |
| 23 | + name: config.name, |
| 24 | + message: config.message, |
| 25 | + choices: [ |
| 26 | + new Separator(header), |
| 27 | + new Separator(separator), |
| 28 | + ...rows.map((name: string, index: number) => ({ |
| 29 | + name, |
| 30 | + short: config.rows[index].short, |
| 31 | + value: config.rows[index].value, |
| 32 | + })), |
| 33 | + new Separator(separator), |
| 34 | + new Separator(' '.repeat(separator.length)), |
| 35 | + ], |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + public async list<T>(config: { |
| 40 | + name: string, |
| 41 | + message: string, |
| 42 | + choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>, |
| 43 | + }): Promise<T> { |
| 44 | + |
| 45 | + const separatorCount = config |
| 46 | + .choices |
| 47 | + .filter((c) => c instanceof Separator) |
| 48 | + .length |
| 49 | + |
| 50 | + const res = await prompt([{ |
| 51 | + type: 'list', |
| 52 | + name: config.name, |
| 53 | + pageSize: process.stdout.rows - separatorCount - 1, |
| 54 | + message: config.message, |
| 55 | + choices: config.choices, |
| 56 | + }]) |
| 57 | + |
| 58 | + return res[config.name] as T |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments