Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: distinct colors for each parameter type #444

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
});
});
});
Loading