Skip to content

Commit 312ad8b

Browse files
authored
feat: distinct colors for each parameter type (#444)
1 parent aae17a9 commit 312ad8b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/theme.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class Theme {
2121
this.theme = options;
2222
}
2323

24+
hasDistinctStylesForTypes() {
25+
return (this.theme['exampleBool']
26+
&& this.theme['exampleNumber']
27+
&& this.theme['exampleString']);
28+
}
29+
2430
getStylingFunction(partName) {
2531
let styles = this.theme[partName];
2632
return buildStylingFunction(styles);
@@ -43,7 +49,18 @@ class Theme {
4349
}
4450

4551
renderExampleToken(text) {
46-
return this.getStylingFunction('exampleToken')(text);
52+
let tokenName = 'exampleToken';
53+
if (!this.hasDistinctStylesForTypes())
54+
return this.getStylingFunction(tokenName)(text);
55+
56+
if (!Number.isNaN(Number(text)))
57+
tokenName = 'exampleNumber';
58+
if (Number.isNaN(Number(text)))
59+
tokenName = 'exampleString';
60+
if (/true|false/.test(text))
61+
tokenName = 'exampleBool';
62+
63+
return this.getStylingFunction(tokenName)(text);
4764
}
4865
}
4966

test/theme.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,53 @@ describe('Theme', () => {
8686
chalk.white('text'));
8787
});
8888
});
89+
90+
describe('Rendering with distinct colors for each token type', () => {
91+
92+
let theme = new Theme({
93+
commandName: 'greenBright, bold',
94+
mainDescription: 'greenBright, bold',
95+
exampleDescription: 'greenBright',
96+
exampleCode: 'redBright',
97+
exampleBool: 'magenta',
98+
exampleNumber: 'white',
99+
exampleString: 'blue'
100+
});
101+
102+
it('should render name with greenBright and bold', () => {
103+
theme.renderCommandName('text')
104+
.should.equal(
105+
chalk.greenBright.bold('text'));
106+
});
107+
108+
it('should render description with greenBright and bold', () => {
109+
theme.renderMainDescription('text')
110+
.should.equal(
111+
chalk.greenBright.bold('text'));
112+
});
113+
114+
it('should render example description with greenBright', () => {
115+
theme.renderExampleDescription('text')
116+
.should.equal(
117+
chalk.greenBright('text'));
118+
});
119+
120+
it('should render example code with redBright', () => {
121+
theme.renderExampleCode('text')
122+
.should.equal(
123+
chalk.redBright('text'));
124+
});
125+
126+
it('should render example arguments with magenta, white, and blue, for boolean, number, and string respectively', () => {
127+
theme.renderExampleToken('true')
128+
.should.equal(
129+
chalk.magenta('true'));
130+
theme.renderExampleToken('9')
131+
.should.equal(
132+
chalk.white('9'));
133+
theme.renderExampleToken('text')
134+
.should.equal(
135+
chalk.blue('text'));
136+
});
137+
});
89138
});

0 commit comments

Comments
 (0)