-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathui.service.ts
More file actions
63 lines (52 loc) · 1.61 KB
/
ui.service.ts
File metadata and controls
63 lines (52 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// import ora from 'ora'
import {Injectable} from '@nestjs/common';
import inquirer from 'inquirer';
import Separator from 'inquirer/lib/objects/separator';
import {getTable} from 'console.table'
@Injectable()
export class UIService {
public async table<T>(config: {
name: string,
message: string,
printColNum?: boolean,
rows: Array<{ row: Record<string, unknown>, short: string, value: T }>,
}): Promise<T> {
const table = getTable(config.rows.map(({row}, index: number) => {
return config.printColNum === false ? row : ({'#': index + 1, ...row});
}))
const [header, separator, ...rows] = table.trim().split('\n')
return this.list({
name: config.name,
message: config.message,
choices: [
new Separator(header),
new Separator(separator),
...rows.map((name: string, index: number) => ({
name: config.name,
short: config.rows[index].short,
value: config.rows[index].value,
})),
new Separator(separator),
new Separator(' '.repeat(separator.length)),
],
})
}
public async list<T>(config: {
name: string,
message: string,
choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>,
}): Promise<T> {
const separatorCount = config
.choices
.filter((c) => c instanceof Separator)
.length
const res = await inquirer.prompt([{
type: 'list',
name: config.name,
pageSize: process.stdout.rows - separatorCount - 1,
message: config.message,
choices: config.choices,
}])
return res[config.name] as T
}
}