forked from OpenAPITools/openapi-generator-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.service.ts
More file actions
57 lines (53 loc) · 1.59 KB
/
ui.service.ts
File metadata and controls
57 lines (53 loc) · 1.59 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
// import ora from 'ora'
import { Injectable } from '@nestjs/common';
import select, { Separator } from '@inquirer/select';
import { getTable } from 'console.table';
import { dim } from 'chalk';
@Injectable()
export class UIService {
public async table<T>(config: {
message: string;
printColNum?: boolean;
rows: Array<{ row: Record<string, string>; short: string; value: T }>;
}): Promise<T> {
const table: string = 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({
message: config.message,
choices: [
new Separator(dim(header)),
new Separator(dim(separator)),
...rows.map((name, index) => ({
name,
short: config.rows[index].short,
value: config.rows[index].value,
})),
new Separator(dim(separator)),
new Separator(dim(' '.repeat(separator.length))),
],
});
}
public async list<T>(config: {
message: string;
choices: Array<{ name: string; short?: string; value: T } | Separator>;
}): Promise<T> {
const pageSize = Math.max(1, process.stdout.rows - 2);
try {
const res = await select({
pageSize,
message: config.message,
choices: config.choices,
});
return res;
} catch (err) {
if (err instanceof Error && err.message.startsWith('User force closed the prompt with')) {
process.exit(0);
}
throw err;
}
}
}