Skip to content

Commit

Permalink
Change default text color to reset instead of gray.dim
Browse files Browse the repository at this point in the history
Fixes #275
  • Loading branch information
gustavohenke committed May 8, 2021
1 parent 70b92da commit ecf1c5b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -147,7 +147,7 @@ Prefix styling
- Available background colors: bgBlack, bgRed,
bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
See https://www.npmjs.com/package/chalk for more
information. [string] [default: "gray.dim"]
information. [string] [default: "reset"]
-l, --prefix-length Limit how many characters of the command is displayed
in prefix. The option can be used to shorten the
prefix when it is set to "command"
Expand Down
2 changes: 1 addition & 1 deletion src/defaults.js
Expand Up @@ -14,7 +14,7 @@ module.exports = {
// Which prefix style to use when logging processes output.
prefix: '',
// Refer to https://www.npmjs.com/package/chalk
prefixColors: 'gray.dim',
prefixColors: 'reset',
// How many bytes we'll show on the command prefix
prefixLength: 10,
raw: false,
Expand Down
7 changes: 4 additions & 3 deletions src/logger.js
Expand Up @@ -61,7 +61,8 @@ module.exports = class Logger {
if (command.prefixColor && command.prefixColor.startsWith('#')) {
color = chalk.hex(command.prefixColor);
} else {
color = _.get(chalk, command.prefixColor, chalk.gray.dim);
const defaultColor = _.get(chalk, defaults.prefixColors, chalk.reset);
color = _.get(chalk, command.prefixColor, defaultColor);
}
return color(text);
}
Expand All @@ -71,7 +72,7 @@ module.exports = class Logger {
return;
}

this.logCommandText(chalk.gray.dim(text) + '\n', command);
this.logCommandText(chalk.reset(text) + '\n', command);
}

logCommandText(text, command) {
Expand All @@ -84,7 +85,7 @@ module.exports = class Logger {
return;
}

this.log(chalk.gray.dim('-->') + ' ', chalk.gray.dim(text) + '\n');
this.log(chalk.reset('-->') + ' ', chalk.reset(text) + '\n');
}

log(prefix, text) {
Expand Down
32 changes: 17 additions & 15 deletions src/logger.spec.js
Expand Up @@ -6,6 +6,8 @@ const Logger = require('./logger');
let outputStream;
beforeEach(() => {
outputStream = createMockInstance(Writable);
// Force chalk to use colours, otherwise tests may pass when they were supposed to be failing.
chalk.level = 3;
});

const createLogger = options => {
Expand Down Expand Up @@ -65,8 +67,8 @@ describe('#logGlobalEvent()', () => {
logger.logGlobalEvent('foo');

expect(logger.log).toHaveBeenCalledWith(
chalk.gray.dim('-->') + ' ',
chalk.gray.dim('foo') + '\n'
chalk.reset('-->') + ' ',
chalk.reset('foo') + '\n'
);
});
});
Expand All @@ -76,14 +78,14 @@ describe('#logCommandText()', () => {
const logger = createLogger();
logger.logCommandText('foo', { name: 'bla' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[bla]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[bla]') + ' ', 'foo');
});

it('logs with index if no prefixFormat is set, and command has no name', () => {
const logger = createLogger();
logger.logCommandText('foo', { index: 2 });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[2]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[2]') + ' ', 'foo');
});

it('logs with prefixFormat set to pid', () => {
Expand All @@ -93,64 +95,64 @@ describe('#logCommandText()', () => {
info: {}
});

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[123]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[123]') + ' ', 'foo');
});

it('logs with prefixFormat set to name', () => {
const logger = createLogger({ prefixFormat: 'name' });
logger.logCommandText('foo', { name: 'bar' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[bar]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[bar]') + ' ', 'foo');
});

it('logs with prefixFormat set to index', () => {
const logger = createLogger({ prefixFormat: 'index' });
logger.logCommandText('foo', { index: 3 });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[3]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[3]') + ' ', 'foo');
});

it('logs with prefixFormat set to time (with timestampFormat)', () => {
const logger = createLogger({ prefixFormat: 'time', timestampFormat: 'yyyy' });
logger.logCommandText('foo', {});

const year = new Date().getFullYear();
expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim(`[${year}]`) + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset(`[${year}]`) + ' ', 'foo');
});

it('logs with templated prefixFormat', () => {
const logger = createLogger({ prefixFormat: '{index}-{name}' });
logger.logCommandText('foo', { index: 0, name: 'bar' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('0-bar') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('0-bar') + ' ', 'foo');
});

it('does not strip spaces from beginning or end of prefixFormat', () => {
const logger = createLogger({ prefixFormat: ' {index}-{name} ' });
logger.logCommandText('foo', { index: 0, name: 'bar' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim(' 0-bar ') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset(' 0-bar ') + ' ', 'foo');
});

it('logs with no prefix', () => {
const logger = createLogger({ prefixFormat: 'none' });
logger.logCommandText('foo', { command: 'echo foo' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim(''), 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset(''), 'foo');
});

it('logs prefix using command line itself', () => {
const logger = createLogger({ prefixFormat: 'command' });
logger.logCommandText('foo', { command: 'echo foo' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[echo foo]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[echo foo]') + ' ', 'foo');
});

it('logs prefix using command line itself, capped at prefixLength bytes', () => {
const logger = createLogger({ prefixFormat: 'command', prefixLength: 6 });
logger.logCommandText('foo', { command: 'echo foo' });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[ec..oo]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[ec..oo]') + ' ', 'foo');
});

it('logs prefix using prefixColor from command', () => {
Expand All @@ -164,7 +166,7 @@ describe('#logCommandText()', () => {
const logger = createLogger();
logger.logCommandText('foo', { prefixColor: 'blue.fake', index: 1 });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[1]') + ' ', 'foo');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[1]') + ' ', 'foo');
});

it('logs prefix using prefixColor from command if prefixColor is a hex value', () => {
Expand All @@ -188,6 +190,6 @@ describe('#logCommandEvent()', () => {
const logger = createLogger();
logger.logCommandEvent('foo', { index: 1 });

expect(logger.log).toHaveBeenCalledWith(chalk.gray.dim('[1]') + ' ', chalk.gray.dim('foo') + '\n');
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[1]') + ' ', chalk.reset('foo') + '\n');
});
});

0 comments on commit ecf1c5b

Please sign in to comment.