Closed tshemsedinov closed 6 years ago
Maybe it have special mode to use functions instead of String modification
Yes, it has:
var colors = require('colors/safe');
console.log(colors.green('Impress'));
There's also a more feature-rich library called cli-color
which doesn't modify String.prototype
too.
Great, but syntax isn't good console.log(api.colors.bold(api.colors.green('Impress')));
What can we do with syntax? Maybe ideas from other languages/platforms?
api.tty.info = function(message) {
return api.colors.bold(api.colors.green(message));
};
If we want concatenation and stuff, or, just for printing:
api.con.info = function() {
console.log.apply(console, Array.prototype.map.call(arguments, function(argument) {
return api.colors.bold(api.colors.green(argument)); // or api.tty.info(argument) if that function is implemented
}));
};
In the worst case:
function functionThatPrints() {
...
var bold = api.colors.bold,
green = api.colors.green;
console.log(bold(green('Impress')));
...
}
But that should be rare since it is an application server, not a rainbow simulation. All the combinations of colors and font styles in the chosen color scheme should be encapsulated into functions with semantic-oriented, not color-based, names.
Meybe something like this api.console.log('Impress red[Application] green+bold[Server]'));
Maybe, or create a custom tag function for ES6 template strings. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals Once upon I did something like that for table-like formatting.
But nevertheless the idea of separate functions that form a solid color scheme, that can be easily changed in one place, and provide an abstraction over real colors seems more sensible to me.
Another option is to define constants with VT100 escape sequences and use ES2015 template strings.
console.log(`${api.colors.GREEN}Impress Application Server${api.colors.DEFAULT}`);
Now we use concolor instead of colors
This library changes String class just for colors. Maybe it have special mode to use functions instead of String modification or we can generete ESC colors, not a big deal