metarhia / impress

Enterprise application server for Node.js and Metarhia private cloud ⚡
https://metarhia.com
MIT License
969 stars 129 forks source link

Remove dependency: colors #547

Closed tshemsedinov closed 6 years ago

tshemsedinov commented 8 years ago

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

aqrln commented 8 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.

tshemsedinov commented 8 years ago

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?

aqrln commented 8 years ago
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
  }));
};
aqrln commented 8 years ago

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.

tshemsedinov commented 8 years ago

Meybe something like this api.console.log('Impress red[Application] green+bold[Server]'));

aqrln commented 8 years ago

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.

aqrln commented 8 years ago

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.

aqrln commented 8 years ago

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}`);
tshemsedinov commented 6 years ago

Now we use concolor instead of colors