Skip to content

Commit 612f60c

Browse files
edsadrRafaelGSS
authored andcommitted
cli: style node --help output with util.styleText
Apply util.styleText to the `node --help` output for improved visual hierarchy and scannability. Styling is applied only when the output stream supports color (detected via util.styleText's built-in shouldColorize), so piped/redirected output and NO_COLOR remain plain text with no behavior change. - Bold: Usage lines, Options:, Environment variables: headers - Bold green: CLI option names (e.g. --inspect, -e, --eval) - Bold magenta: environment variable names (e.g. NODE_PATH) - Dim: "(currently set)" annotation - Blue underline: documentation URL Column-width math uses unstyled string lengths to preserve alignment regardless of styling. The "(currently set)" annotation is styled via post-processing after layout to avoid breaking the fold() width calculation. Signed-off-by: Adrian Estrada <edsadr@gmail.com> PR-URL: #64484 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 7a02e4f commit 612f60c

2 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎lib/internal/main/print_help.js‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ const {
2525
}=require('internal/process/pre_execution');
2626

2727
const{ getCLIOptionsInfo, getOptionValue }=require('internal/options');
28+
const{ styleText }=require('util');
2829

2930
consttypeLookup=[];
3031
for(constkeyofObjectKeys(types))
3132
typeLookup[types[key]]=key;
3233

34+
functionstyle(format,text){
35+
returnstyleText(format,text,{stream: process.stdout});
36+
}
37+
3338
// Environment variables are parsed ad-hoc throughout the code base,
3439
// so we gather the documentation here.
3540
const{ hasIntl, hasSmallICU, hasNodeOptions }=internalBinding('config');
@@ -117,7 +122,7 @@ function getArgDescription(type) {
117122
}
118123

119124
functionformat(
120-
{ options, aliases =newSafeMap(), firstColumn, secondColumn },
125+
{ options, aliases =newSafeMap(), firstColumn, secondColumn, nameStyle =[]},
121126
){
122127
lettext='';
123128
letmaxFirstColumnUsed=0;
@@ -176,7 +181,7 @@ function format(
176181
displayHelpText+=' (currently set)';
177182
}
178183

179-
text+=displayName;
184+
text+=style(nameStyle,displayName);
180185
maxFirstColumnUsed=MathMax(maxFirstColumnUsed,displayName.length);
181186
if(displayName.length>=firstColumn)
182187
text+='\n'+StringPrototypeRepeat(' ',firstColumn);
@@ -194,6 +199,7 @@ function format(
194199
aliases,
195200
firstColumn: maxFirstColumnUsed+2,
196201
secondColumn,
202+
nameStyle,
197203
});
198204
}
199205

@@ -214,20 +220,29 @@ function print(stream) {
214220
'interactive mode if a tty)'});
215221
options.set('--',{helpText: 'indicate the end of node options'});
216222
lethelpText=(
217-
'Usage: node [options] [ script.js ] [arguments]\n'+
218-
' node inspect [options] [ script.js | host:port ] [arguments]\n\n'+
219-
'Options:\n');
223+
style('bold',
224+
'Usage: node [options] [ script.js ] [arguments]\n'+
225+
' node inspect [options] [ script.js | host:port ] [arguments]')+
226+
'\n\n'+style('bold','Options:')+'\n');
220227
helpText+=(indent(format({
221-
options, aliases, firstColumn, secondColumn,
228+
options, aliases, firstColumn, secondColumn,nameStyle: ['bold','green'],
222229
}),2));
223230

224-
helpText+=('\nEnvironment variables:\n');
231+
helpText+=('\n'+style('bold','Environment variables:')+'\n');
225232

226233
helpText+=(format({
227-
options: envVars, firstColumn, secondColumn,
234+
options: envVars, firstColumn, secondColumn,nameStyle: ['bold','magenta'],
228235
}));
229236

230-
helpText+=('\nDocumentation can be found at https://nodejs.org/');
237+
helpText+=('\nDocumentation can be found at '+
238+
style(['blue','underline'],'https://nodejs.org/'));
239+
240+
helpText=RegExpPrototypeSymbolReplace(
241+
/\(currentlyset\)/g,
242+
helpText,
243+
style('dim',' (currently set)'),
244+
);
245+
231246
console.log(helpText);
232247
}
233248

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constassert=require('assert');
5+
const{ execFile }=require('child_process');
6+
7+
// eslint-disable-next-line no-control-regex
8+
constANSI_SGR_REGEX=newRegExp('\x1b\\[[0-9;]*m','g');
9+
10+
functionstripAnsi(text){
11+
returntext.replace(ANSI_SGR_REGEX,'');
12+
}
13+
14+
// Test: FORCE_COLOR=1 produces styled output
15+
{
16+
constenv={ ...process.env,FORCE_COLOR: '1'};
17+
execFile(process.execPath,['--help'],{ env },
18+
common.mustSucceed((stdout)=>{
19+
assert.ok(stdout.includes('\x1b[1m'),'bold for headers should be present');
20+
assert.ok(stdout.includes('\x1b[32m'),'green for option names should be present');
21+
assert.ok(stdout.includes('\x1b[35m'),'magenta for env var names should be present');
22+
assert.ok(stdout.includes('\x1b[34m'),'blue for URL should be present');
23+
assert.ok(stdout.includes('\x1b[4m'),'underline for URL should be present');
24+
}));
25+
}
26+
27+
// Test: NO_COLOR=1 produces plain output
28+
{
29+
constenv={ ...process.env,NO_COLOR: '1'};
30+
deleteenv.FORCE_COLOR;
31+
execFile(process.execPath,['--help'],{ env },
32+
common.mustSucceed((stdout)=>{
33+
assert.ok(stripAnsi(stdout)===stdout,
34+
'no ANSI escape sequences should be present with NO_COLOR=1');
35+
}));
36+
}
37+
38+
// Test: piped (non-TTY, no FORCE_COLOR) produces plain output
39+
{
40+
constenv={ ...process.env};
41+
deleteenv.FORCE_COLOR;
42+
deleteenv.NO_COLOR;
43+
deleteenv.NODE_DISABLE_COLORS;
44+
execFile(process.execPath,['--help'],{ env },
45+
common.mustSucceed((stdout)=>{
46+
assert.ok(stripAnsi(stdout)===stdout,
47+
'no ANSI escape sequences should be present when piped (non-TTY)');
48+
}));
49+
}
50+
51+
// Test: alignment preservation - stripped styled output matches plain output
52+
{
53+
constenvStyled={ ...process.env,FORCE_COLOR: '1'};
54+
constenvPlain={ ...process.env,NO_COLOR: '1'};
55+
deleteenvPlain.FORCE_COLOR;
56+
execFile(process.execPath,['--help'],{env: envStyled},
57+
common.mustSucceed((styledStdout)=>{
58+
execFile(process.execPath,['--help'],{env: envPlain},
59+
common.mustSucceed((plainStdout)=>{
60+
assert.ok(stripAnsi(styledStdout)===plainStdout,
61+
'stripped styled output should match plain output (alignment preservation)');
62+
}));
63+
}));
64+
}

0 commit comments

Comments
 (0)