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

fix(cli): colored text is unreadable when using light themes #5250

Merged
merged 3 commits into from
Jan 28, 2020
Merged
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
48 changes: 19 additions & 29 deletions packages/aws-cdk/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
import * as colors from 'colors/safe';
import { Writable } from 'stream';
import * as util from 'util';

// tslint:disable:no-console the whole point of those methods is precisely to output to the console...
netroy marked this conversation as resolved.
Show resolved Hide resolved
type StyleFn = (str: string) => string;
const { stdout, stderr } = process;

const logger = (stream: Writable, styles?: StyleFn[]) => (fmt: string, ...args: any[]) => {
let str = util.format(fmt, ...args);
if (styles && styles.length) {
str = styles.reduce((a, style) => style(a), str);
}
stream.write(str + '\n');
};

export let isVerbose = false;

export function setVerbose(enabled = true) {
isVerbose = enabled;
}

export function error(fmt: string, ...args: any[]) {
console.error(colors.red(util.format(fmt, ...args)));
}

export function debug(fmt: string, ...args: any[]) {
if (isVerbose) {
console.error(colors.gray(util.format(fmt, ...args)));
}
}

export function highlight(fmt: string, ...args: any[]) {
console.error(colors.bold(colors.white(util.format(fmt, ...args))));
}
const _debug = logger(stderr, [colors.gray]);

export function success(fmt: string, ...args: any[]) {
console.error(colors.green(util.format(fmt, ...args)));
}

export function warning(fmt: string, ...args: any[]) {
console.error(colors.yellow(util.format(fmt, ...args)));
}

export function print(fmt: string, ...args: any[]) {
console.error(colors.white(util.format(fmt, ...args)));
}

export function data(fmt: string, ...args: any[]) {
console.log(util.format(fmt, ...args));
}
export const debug = (fmt: string, ...args: any[]) => isVerbose && _debug(fmt, ...args);
export const error = logger(stderr, [colors.red]);
export const warning = logger(stderr, [colors.yellow]);
export const success = logger(stderr, [colors.green]);
export const highlight = logger(stderr, [colors.bold]);
export const print = logger(stderr);
export const data = logger(stdout);

export type LoggerFunction = (fmt: string, ...args: any[]) => void;

Expand Down