From 5b36aa05060e26886b0f5eafdc415379bcd25a7d Mon Sep 17 00:00:00 2001 From: Renie Date: Sun, 13 Oct 2024 18:20:03 -0300 Subject: [PATCH] feat: distinct colors for each parameter type --- lib/theme.js | 19 +++++++++++++++++- test/theme.spec.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/theme.js b/lib/theme.js index c70ed9f2..127bfd5e 100644 --- a/lib/theme.js +++ b/lib/theme.js @@ -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); @@ -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); } } diff --git a/test/theme.spec.js b/test/theme.spec.js index c73dbb00..06cff0f7 100644 --- a/test/theme.spec.js +++ b/test/theme.spec.js @@ -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')); + }); + }); });