Skip to content

Commit

Permalink
feat: distinct colors for each parameter type (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
renie authored Oct 23, 2024
1 parent aae17a9 commit 312ad8b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class Theme {
this.theme = options;
}

hasDistinctStylesForTypes() {
return (this.theme['exampleBool']
&& this.theme['exampleNumber']
&& this.theme['exampleString']);
}

getStylingFunction(partName) {
let styles = this.theme[partName];
return buildStylingFunction(styles);
Expand All @@ -43,7 +49,18 @@ class Theme {
}

renderExampleToken(text) {
return this.getStylingFunction('exampleToken')(text);
let tokenName = 'exampleToken';
if (!this.hasDistinctStylesForTypes())
return this.getStylingFunction(tokenName)(text);

if (!Number.isNaN(Number(text)))
tokenName = 'exampleNumber';
if (Number.isNaN(Number(text)))
tokenName = 'exampleString';
if (/true|false/.test(text))
tokenName = 'exampleBool';

return this.getStylingFunction(tokenName)(text);
}
}

Expand Down
49 changes: 49 additions & 0 deletions test/theme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,53 @@ describe('Theme', () => {
chalk.white('text'));
});
});

describe('Rendering with distinct colors for each token type', () => {

let theme = new Theme({
commandName: 'greenBright, bold',
mainDescription: 'greenBright, bold',
exampleDescription: 'greenBright',
exampleCode: 'redBright',
exampleBool: 'magenta',
exampleNumber: 'white',
exampleString: 'blue'
});

it('should render name with greenBright and bold', () => {
theme.renderCommandName('text')
.should.equal(
chalk.greenBright.bold('text'));
});

it('should render description with greenBright and bold', () => {
theme.renderMainDescription('text')
.should.equal(
chalk.greenBright.bold('text'));
});

it('should render example description with greenBright', () => {
theme.renderExampleDescription('text')
.should.equal(
chalk.greenBright('text'));
});

it('should render example code with redBright', () => {
theme.renderExampleCode('text')
.should.equal(
chalk.redBright('text'));
});

it('should render example arguments with magenta, white, and blue, for boolean, number, and string respectively', () => {
theme.renderExampleToken('true')
.should.equal(
chalk.magenta('true'));
theme.renderExampleToken('9')
.should.equal(
chalk.white('9'));
theme.renderExampleToken('text')
.should.equal(
chalk.blue('text'));
});
});
});

0 comments on commit 312ad8b

Please sign in to comment.