-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelp.ts
More file actions
61 lines (47 loc) · 1.49 KB
/
Copy pathhelp.ts
File metadata and controls
61 lines (47 loc) · 1.49 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
import { color } from 'rslog';
declare global {
const RSTACK_VERSION: string;
}
export type HelpItem = readonly [label: string, description: string];
export type HelpSection =
| {
title: string;
items: readonly HelpItem[];
}
| {
content: string;
dim?: boolean;
};
export type HelpDefinition = {
usage: string;
description?: string;
sections?: readonly HelpSection[];
};
export const hasHelpFlag = (args: readonly string[]): boolean => {
const end = args.indexOf('--');
const flags = end === -1 ? args : args.slice(0, end);
return flags.some((flag) => flag === '-h' || flag === '--help');
};
const renderItems = (items: readonly HelpItem[]): string => {
const labelWidth = items.reduce((width, [label]) => Math.max(width, label.length), 0);
return items
.map(([label, description]) => ` ${label.padEnd(labelWidth)} ${description}`)
.join('\n');
};
const renderSection = (section: HelpSection): string => {
if ('items' in section) {
return `${color.cyan(section.title)}:\n${renderItems(section.items)}`;
}
return section.dim ? color.dim(section.content) : section.content;
};
export const renderHelp = ({ usage, description, sections = [] }: HelpDefinition): string => {
const blocks = [
color.bold(`Rstack v${RSTACK_VERSION}`),
`${color.cyan('Usage')}:\n${color.yellow(` $ ${usage}`)}`,
];
if (description) {
blocks.push(description);
}
blocks.push(...sections.map(renderSection));
return blocks.join('\n\n');
};