1- import ansiEscapes from 'ansi-escapes' ;
21import {
32 HELP_HEADER ,
43 OPTIONS ,
76} from '../../../constants/cli.constants.js' ;
87import { MARGINS , UI_HELP } from '../../../constants/main.constants.js' ;
98import { INFO_MSGS } from '../../../constants/messages.constants.js' ;
10- import { IPosition } from '../../interfaces/ui-positions.interface.js' ;
119import { ConsoleService } from '../../services/console.service.js' ;
1210import { BaseUi } from '../base.ui.js' ;
1311import 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 - z A - 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}
0 commit comments