Skip to content

Commit 462d4d3

Browse files
authored
Merge pull request #222 from voidcosmos/fix/help-page
Improve --help output
2 parents e10d1a4 + e9ba62d commit 462d4d3

2 files changed

Lines changed: 53 additions & 33 deletions

File tree

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ansiEscapes from 'ansi-escapes';
21
import {
32
HELP_HEADER,
43
OPTIONS,
@@ -7,7 +6,6 @@ import {
76
} from '../../../constants/cli.constants.js';
87
import { MARGINS, UI_HELP } from '../../../constants/main.constants.js';
98
import { INFO_MSGS } from '../../../constants/messages.constants.js';
10-
import { IPosition } from '../../interfaces/ui-positions.interface.js';
119
import { ConsoleService } from '../../services/console.service.js';
1210
import { BaseUi } from '../base.ui.js';
1311
import colors from 'colors';
@@ -22,52 +20,73 @@ export class HelpCommandUi extends BaseUi {
2220
}
2321

2422
show(): void {
23+
const maxWidth = Math.min(UI_HELP.MAX_WIDTH, this.terminal.columns);
24+
2525
this.clear();
2626
this.print(colors.inverse(INFO_MSGS.HELP_TITLE + '\n\n'));
27-
this.print(HELP_HEADER + '\n\n');
28-
this.print(HELP_PROGRESSBAR + '\n\n');
2927

30-
let lineCount = 0;
31-
OPTIONS.forEach((option, index) => {
32-
this.printAtHelp(
33-
option.arg.reduce((text, arg) => text + ', ' + arg),
34-
{
35-
x: UI_HELP.X_COMMAND_OFFSET,
36-
y: index + UI_HELP.Y_OFFSET + lineCount,
37-
},
38-
);
28+
const headerLines = this.consoleService.splitWordsByWidth(
29+
HELP_HEADER,
30+
maxWidth,
31+
);
32+
headerLines.forEach((line) => this.print(line + '\n'));
33+
this.print('\n');
34+
35+
const progressBarLines = this.consoleService.splitWordsByWidth(
36+
HELP_PROGRESSBAR,
37+
maxWidth,
38+
);
39+
progressBarLines.forEach((line) => this.print(line + '\n'));
40+
this.print('\n');
41+
42+
const maxDescriptionWidth = Math.min(
43+
maxWidth - UI_HELP.X_DESCRIPTION_OFFSET,
44+
this.terminal.columns - UI_HELP.X_DESCRIPTION_OFFSET,
45+
);
46+
47+
OPTIONS.forEach((option) => {
48+
const args = option.arg.reduce((text, arg) => text + ', ' + arg);
49+
const padding = ' '.repeat(UI_HELP.X_COMMAND_OFFSET);
50+
const commandLength = UI_HELP.X_COMMAND_OFFSET + args.length;
51+
52+
const commandTooLong = commandLength >= UI_HELP.X_DESCRIPTION_OFFSET;
53+
54+
if (commandTooLong) {
55+
this.print(padding + args + '\n');
56+
} else {
57+
this.print(padding + args);
58+
}
59+
3960
const description = this.consoleService.splitWordsByWidth(
4061
option.description,
41-
this.terminal.columns - UI_HELP.X_DESCRIPTION_OFFSET,
62+
maxDescriptionWidth,
4263
);
4364

44-
description.forEach((line) => {
45-
this.printAtHelp(line, {
46-
x: UI_HELP.X_DESCRIPTION_OFFSET,
47-
y: index + UI_HELP.Y_OFFSET + lineCount,
48-
});
49-
++lineCount;
65+
description.forEach((line, index) => {
66+
if (index === 0 && !commandTooLong) {
67+
const spaceBetween = ' '.repeat(
68+
UI_HELP.X_DESCRIPTION_OFFSET - commandLength,
69+
);
70+
this.print(spaceBetween + line + '\n');
71+
} else {
72+
const descriptionPadding = ' '.repeat(UI_HELP.X_DESCRIPTION_OFFSET);
73+
this.print(descriptionPadding + line + '\n');
74+
}
5075
});
76+
77+
this.print('\n');
5178
});
5279

53-
this.print(HELP_FOOTER + '\n');
80+
const footerLines = this.consoleService.splitWordsByWidth(
81+
HELP_FOOTER,
82+
maxWidth,
83+
);
84+
footerLines.forEach((line) => this.print(line + '\n'));
5485
}
5586

5687
clear(): void {
5788
for (let row = MARGINS.ROW_RESULTS_START; row < this.terminal.rows; row++) {
5889
this.clearLine(row);
5990
}
6091
}
61-
62-
private printAtHelp(message: string, position: IPosition): void {
63-
this.setCursorAtHelp(position);
64-
this.print(message);
65-
if (!/-[a-zA-Z]/.test(message.substring(0, 2)) && message !== '') {
66-
this.print('\n\n');
67-
}
68-
}
69-
70-
private setCursorAtHelp({ x }: IPosition): void {
71-
this.print(ansiEscapes.cursorTo(x));
72-
}
7392
}

src/constants/main.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const UI_HELP = {
3737
X_COMMAND_OFFSET: 3,
3838
X_DESCRIPTION_OFFSET: 27,
3939
Y_OFFSET: 2,
40+
MAX_WIDTH: 80,
4041
};
4142

4243
export const UI_POSITIONS = {

0 commit comments

Comments
 (0)