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

Support hex prefix colors with modifiers #450

Open
wants to merge 4 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/logger.spec.ts
Expand Up @@ -197,6 +197,21 @@ describe('#logCommandText()', () => {
expect(logger.log).toHaveBeenCalledWith(chalk.hex(prefixColor)('[1]') + ' ', 'foo', cmd);
});

it('logs prefix using prefixColor from command if prefixColor is a hex value with modifiers', () => {
const { logger } = createLogger({});
const prefixColor = '#32bd8a.inverse';
const cmd = new FakeCommand('', undefined, 1, {
prefixColor,
});
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(
chalk.hex(prefixColor).inverse('[1]') + ' ',
'foo',
cmd,
);
});

it('does nothing if command is hidden by name', () => {
const { logger } = createLogger({ hide: ['abc'] });
const cmd = new FakeCommand('abc');
Expand Down
6 changes: 5 additions & 1 deletion src/logger.ts
Expand Up @@ -121,7 +121,11 @@ export class Logger {
colorText(command: Command, text: string) {
let color: chalk.Chalk;
if (command.prefixColor && command.prefixColor.startsWith('#')) {
color = chalk.hex(command.prefixColor);
const [hexColor, ...modifiers] = command.prefixColor.split('.');
color = chalk.hex(hexColor);
if (modifiers.length) {
color = _.get(color, modifiers);
}
} else {
const defaultColor = _.get(chalk, defaults.prefixColors, chalk.reset);
color = _.get(chalk, command.prefixColor ?? '', defaultColor);
Expand Down