Skip to content

Instantly share code, notes, and snippets.

@CedricSch
Last active June 2, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CedricSch/58a595aab5108969da0f67fe42572341 to your computer and use it in GitHub Desktop.
Save CedricSch/58a595aab5108969da0f67fe42572341 to your computer and use it in GitHub Desktop.
Exercise 8.3 Colored console output
class DecoratedConsole {
constructor(console) {
this.console = console;
this.RED = "\x1b[31m";
this.GREEN = "\x1b[32m";
this.YELLOW = "\x1b[33m";
this.RESET = "\x1b[0m";
}
_getColorString(color, message) {
return `${color}${message}${this.RESET}`;
}
log(message) {
this.console.log(message);
}
red(message) {
this.console.log(this._getColorString(this.RED, message));
}
green(message) {
this.console.log(this._getColorString(this.GREEN, message));
}
yellow(message) {
this.console.log(this._getColorString(this.YELLOW, message));
}
}
const decorated = new DecoratedConsole(console);
decorated.red("I´m a real pirate");
decorated.green("I´m a turtle");
decorated.yellow("I´m a banana");
decorated.log("I´m not interesting");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment