Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit fe8fa45

Browse files
edsadraduh95
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 e5278b7 commit fe8fa45

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)