marak / colors.js Goto Github PK
View Code? Open in Web Editor NEWget colors in your node.js console
Home Page: https://github.com/Marak/colors.js
License: Other
get colors in your node.js console
Home Page: https://github.com/Marak/colors.js
License: Other
This is a feature request.
add a function for coloring a valid json string. Otherwise just return it as it is.
So when doing console.log('{"foo":"bar"}'.highlightJson);
the json string gets printed nicely colored.
patch @ smtlaissezfaire@a17c714
A program I wrote using color.js had some weird behavior. I got this exception:
>> ExecutionError: N͚̩̻̯e͓̤͌̾̍̅z̀̓̚p͕̝̣̬̙͍̊̚ȅ͖̘̬̿ͫr̃ͧ̉҉̩̥̰d͈̹̞͙̣͎͈̑̓i̵̘͇͉͒ͮͤͧ̆a̭̯͌n͊ͦ̿̄̾͏̻̱̯̲ ̙͔̪̒ͣͦ͊̇h̶͚͉͇̬̳̑ͭ̚ĩ̯̬̓ͬ̀̆ͅv̻̱̽̔ͥ̒ͤe̓͐͏͙͉̬-̹͓̳̯̀m͕͂ͪͨͮi̞̮̼̙̫̬͆͛͂ͬ̀ͣń̳̖̻̞͈͚͔ͬ͒͂ͯ̀d̻̟̒̓ͫ͊͊ͧ͜ ̹͌͑h̠̤͕͖ͦ͜a͕͗̏ͩ̏ͪͯ̓ṣ̲̆͌ͯͬͯ̓ ̠̫͓͓̫̩̃̑̀b͘e͊̑ͮ҉̮̦̱̩͙ė̍ͧͯͮ͏̼̖̪̟̟̥n͕̣̲͌͐̓̇̒ ̂͆͒̓̏i̪ͣ̏͊̒̔n̮̫̰̓̆̃̽͡ͅv̘̋̎ͪͬ̋͆ͦo̭̱͕̺͔͚̲ͦ̀̚͟k͙͈̮̩͒ē̺̈ͪd͇̯̤͎̗ͥ̈́̄̓ͩͨ.̮̭̩̀͜ ̺͉̬̿̅͗H̟͍͙̫ͭͪ̉͡e̷̮̊̈ͤ ̱̠̣̮ͣ̓̍͊̏w̹̬̟̼̺ͥ͌̊̀ͣͪȃ͌ͭ͊̉͏i̿̿̒ͥt̨͎̠̭̞̂ͧͩs̷͑͐͋̓͑ ̸̝̈́̅̓b̳̰̓̒ͨ́e̺̱̪͖̲̽̍ͧ̃h̘̗̃́͗̐͗̋̈́͜i̫̞̲̳̟͆̑̔n̹̺ͯ̍̉ͥͮͯ́d̖͇͖̹ͣ͑̀ ̤͇̪̙̜̩͕͞ẗ̺̔ͬh͕̠͍̹̥͖ȩ̙͙̲̍̓̓ ͓̗̅̃̔͊w̌̐ạ̵̝̼͎̏l̶͈͐ͬ̎l̢̮͐̐͊̿̑̍.̒ͧ͛̄̿͏̜̭̦͈̫
>> at Function.defineProperty (native)
>> at /Users/murdered/Documents/projects/apiary/node_modules/colors/colors.js:341:12
>> at /Users/murdered/Documents/projects/apiary/node_modules/colors/colors.js:49:3
>> at Array.forEach (native)
>> at Object.<anonymous> (/Users/murdered/Documents/projects/apiary/node_modules/colors/colors.js:5̪̬̪͕̇͜6̞ͫͣ̆ͦ̑̓̐3̧̳̺̥̯͔̊̂̐:3)
>> at Module._conjur (module.js:446:26)
>> at Object..js (module.js:464:10)
>> at Module.corrupt (module.js:353:31)
>> at Function._load (module.js:311:12)
>> at Module.require (module.js:359:17)
The exception was inexplicable. My lights shut off on their own volition, and I may not escape. Please fix this bug.
the yellow color in browser, it present badly, i need a brown or anothor, how to do it?
It doesn't work in the browsers.
It says "exports is undefined".
Great module.
Is there a way to strip the color formatting from a string. I set the color in the code depending on the context and send it to the logger. The logger sends it to the console and also to a log file. I am getting things like "�[39m�[22m" appearing in my log file. I would like to strip the color information before it gets logged to file.
Thanks.
I'm running a repl
in a node.js 0.6.19 app, and passing to the repl context a function which invokes cli-table which in turn uses this module. As soon as colors is loaded, it throws a fatal error:
TypeError: Cannot redefine property: underline
At first I thought it was a conflict with REPL's built-in _ variable (underline/underscore, I dunno) but commenting that out throws the same error on "italics" and every other property on line 27 of colors.js.
This might be related to the global scope issue identified in Tweak to keep it from polluting the browser's global namespace.
Is there a workaround for this? Is there a way to modify colors.js so it doesn't modify the global scope?
Thanks!
Currently colors.js uses defineGetter to add new prototyped properties to the String object. These properties are not intended to be enumerable but by using defineGetter they are by default declared enumerable on the String object. With ECMA5 the new / correct way to add properties should be with defineProperty and setting the enumerable flag to false.
But fear not. I will fork and submit a pull request with the simple check / change.
It would be very useful for have that feature for the cosole.
The last release of this was a year ago, and quite a few important fixes have since been made. Could we get a new version released?
When colors.mode == 'none', properties created by setTheme({error:'red'}) return an object instead of a string.
It looks like all the properties added by applyTheme have this behaviour of exploding strings into objects, but stylize coerces them back to strings if mode is console or browser.
Pull request incoming.
nickname ="foo";
console.log('hello'+nickname.green); //error
console.log('hello'+nickname+' - '+.green); //no color
thanks
>> TypeError: Cannot redefine property: underline
>> at Function.defineProperty (native)
>> at /Users/almad/Documents/projects/apiary/node_modules/winston/node_modules/colors/colors.js:53:12
>> at /Users/almad/Documents/projects/apiary/node_modules/winston/node_modules/colors/colors.js:147:3
>> at Array.forEach (native)
>> at Object.<anonymous> (/Users/almad/Documents/projects/apiary/node_modules/winston/node_modules/colors/colors.js:142:3)
>> at Module._compile (module.js:446:26)
>> at Object..js (module.js:464:10)
>> at Module.load (module.js:353:31)
>> at Function._load (module.js:311:12)
>> at Module.require (module.js:359:17)
Hi guys,
It would be great if you could identify the License which is used by colors.js. License is an important aspect that could be vital for all projects that are using color.js right now.
I could suggest two very nice licences, one is WTFPB
Very simple and understandable. Another very popular license in node.js community is MIT:
http://opensource.org/licenses/MIT
Don't forget to put LICENSE file in the root and add 'license' in your package.json. I can also do a pull request for license of your choice ;)
Thanks!
Renat
Sebi2020 wrote:
I think the colors are nice, but are a little bit buggy. It makes the output unreadable. So if it modify the color, the cmd output is >still using this color after executing the jitsu command. If I set colors:false in the config file it seems to have no affect to the >jitsu install & deplay command. Please fix that! I'm running a Windows 7 system:
Copy pasted from nodejitsu/jitsu#279
Is there any changelog available?
It looks like that the code in the repo support multiple colors with an array. Ex:
colors.setTheme({
silly: 'rainbow',
input: 'grey',
info: ['green', 'inverse'],
error: ['red', 'inverse']
});
However the version in npm does not support it at all.
It is also not documented on the readme.md
It is very verbose. It would be nicer to be able to use a space " ". Ex:
colors.setTheme({
silly: 'rainbow',
input: 'grey',
info: 'green inverse',
error: 'red inverse'
});
Can you please update documentation, readme and consider the use od spaces instead of arrays?
It should be colours.js
.
how to apply compound 'red' and 'bold' when i'm using setTheme() ?
and i suggest to add a option to color background, now i have to use 'inverse' to workaround...
A simple test harness to illustrate:
require('colors');
console.log("hello".red);
Running node test.js --no-color
results in "hello" in red rather than with no colour.
Having had a quick dig, I can see that the colors.enabled
flag is being set to false
correctly but the applyStyle
function that uses the flag isn't being invoked.
It seems there is a .travis.yml
in place and since there are tests, one could assume that the test are run at Travis CI.
Would it be possible to show a badge for the current tests passing state in the README.md
?
Could you please make your methods not enumerable since you had monkey-patched the String prototype? For example, this code should capitalize consonants but yields a long colourful "FoofoofooӺǾѺ..." snake:
var colors = require("colors");
var input = "foo", output = "";
for (var i in input) {
var ch = input[i];
if (ch.match(/[eyuioa]/)) output += ch;
else output += ch.toUpperCase();
}
console.log(output);
It would be useful to have a style that returns the text, unmodified from it's origin version. One use case is when you have some function name or variable name and you don't want to type it more than once.
getAStringFromSomewhere()[ inverseFlag ? 'inverse' : 'blank' ]
Without it, I have to repeat the code twice:
inverseFlag ? getAStringFromSomewhere().inverse : getAStringFromSomewhere();
If this will be included, I can create a pull request... does the 'blank' or 'null' name make sense?
The current version on npm
is 9months old. The old version suffers many fails—and leaks global variables that flag up our mocha
tests since colors.js
is used as a subdependency of one of our modules.
Any chance on getting the current master version-bumped and pushed to npm? Please.
Would it be possible (in a future update) to be able to use colors on string substitutions?
That'd be cool :3
When trying to develop a Yeoman generatorfor Cordova, I run into this problem of:
Unable to register generator-cordova/app/index.js (Error: underline already exists on String.prototype, cannot override.)
I believe this is due to generator-generator relying on cli-table which has a dependency on colors (https://github.com/yeoman/generator/blob/master/package.json + https://github.com/LearnBoost/cli-table/blob/master/package.json) AND cordova-cli relies directly on colors (https://github.com/apache/cordova-cli/blob/master/package.json)
So when I run the generator, I get the follow error message:
Unable to register generator-cordova/app/index.js (Error: underline already exists on String.prototype, cannot override.)
This could be due to cli-table using an old version of colors, but I've tried updating this to 0.6.0 but didn't seem to get any further.
Hello,
On some script, it would be great to be able to disable colors entirely. For example when you run your script in a crontab, the output will be send in a plain text emails where ANSI escapes sequences might not be desirable.
Looking at the colors.js code, you define new getters for the string object. It would be great to have that code in a callable function (such as enable) and have another one that makes those getter return the plain string.
This when my script get --no-colors, I can call that disabling function and restore the string behavior. "foo".red would just return "foo".
Let me know if the above is not clear enough.
Comprehensive explanation here: https://gist.github.com/1451296
Comment on offending line: e4decd0#commitcomment-781849
Or is it intentionally hidden by design?
Small request: would be helpful to have other types get colored without fussing with the type.
results.length.blue
Shouldn't the definition for 'black' be [30, 39]
https://github.com/Marak/colors.js/blob/master/colors.js#L71-71
Hi,
I have some huge modules that relie on the JS Standard String prototype functions which conflict with the modification made by colors.js, such as String.bold() and are called by a module which uses colors.js to display colored output on the console.
A typical error I get is:
Property 'bold' of object my string is not a function
Using mode = "browser" does not solve my problems because if I have a string var called x, I'd have to change all code to use x.bold inspite of x.bold().
This has also portability issues.
How can I completely restore the original String prototype only within this module?
Thanks in advance
var colors = require('colors');
console.log('hello'.green);
turns out like
�[32mhello�[39m
You specifically say it works for the browser. I assume I dont have to change the format or anything?
When using ansi escape codes, it's best to check if the terminal actually supports that escape code. The way to do this is to query the terminfo database, which looks at the terminal type defined in $TERM and sees if it supports the escape code that you want.
Many terminals are "dumb" such as emacs shell, and don't support escape code. Others support a subset of escape codes. Some can potentially use different escapes.
I'm not sure if there is a node js wrapper for terminfo, but on unix the CLI program tput can be used to query the terminfo database. For instance, "tput setf 1" will write the escape code for blue to the standard output.
Sometimes output gets piped to non-terminal output. In that case, you should check if the output file descriptor is a terminal before sending any escape codes (http://nodejs.org/api/tty.html).
I noticed that a lot of node js programs that use color seemed to break on emacs term.
Messing with native prototypes is dangerous and breaks encapsulation. Having colors be added to the String prototype should be optional so those that have problems have a work around.
It would be great to use combined styles in themes as well:
Example:
colors.setTheme({
error: [ 'red', 'bold' ]
});
I don't think that's possible yet... or is it? If it is, it should be in the readme.
I'm wondering why the color.js doesn't override the console.log method, appending the color configurations as a parameter, but what it does is to modify the prototype of public objects? This is kind of a pollution, I think.
Hey,
I'm trying to install the forever and hook.io modules, which use colors.js, but it looks like it's failing when it gets to installing colors.
From a blank directory:
$ npm install colors
npm http GET https://registry.npmjs.org/colors
npm http 304 https://registry.npmjs.org/colors
npm ERR! error installing [email protected]
npm ERR! Error: ENOENT, no such file or directory '/Users/viatropos/Desktop/lance/node_modules/colors/package.json'
npm ERR! You may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <[email protected]>
npm ERR!
npm ERR! System Darwin 10.7.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "colors"
npm ERR! cwd /Users/viatropos/Desktop/lance
npm ERR! node -v v0.6.11
npm ERR! npm -v 1.1.4
npm ERR! path /Users/viatropos/Desktop/lance/node_modules/colors/package.json
npm ERR! code ENOENT
npm ERR! message ENOENT, no such file or directory '/Users/viatropos/Desktop/lance/node_modules/colors/package.json'
npm ERR! errno {}
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/viatropos/Desktop/lance/npm-debug.log
npm not ok
Is this because of the node/npm versions? Do you know how to fix this? Thanks for your help.
Is stripColors
method still supported?
What is going on here?
I'm seeing:
function bold() { [native code] }
everywhere in my console output
Hello,
After the last update the setTheme function broke somehow.
colors.setTheme({ info: 'green', warning: 'magenta', prompt: 'cyan', error: 'red' });
console.log(colors.warning(this.options[type][message]));
Error TypeError: Object #<Object> has no method 'warning'
You are adding enumerable properties:
var a, b;
require('colors');
a = "as";
for (b in a) {
console.log(b);
}
The solution is probably to add enumerable false
Could you publish a new version to npm that includes the fix for issue #20? The current version 0.6.0-1 doesn't have it.
Thanks!
console.log('white on green'.green.bold.inverse.white);
should be a white string on a green background.
It looks like the module version in npm is a bit out of date. For example it doesn't have the 'none' bugfix. Any chance of publishing the latest version?
For testing purposes, it'd be nice to just access the definitions instead of duplicating from your source.
underline + color, for example.
How does one do bold red?
We should be able to switch colors into one of three modes:
The first mode will operate as colors.js works now. It will apply ANSI escape codes to the string.
The second will return an object literal describing the object which was sent to it, i.e. 'foo'.yellow returns { string: 'foo', styling: ['yellow'] }
The third mode ( as suggested by @TooTallNate ) will output colored html markup for the browser.
Hello,
I have setup a theme with a color yellow
associate to a variable. However, o the console the output is grey.
util.prototype.console = function(type, message) {
colors.setTheme({ info: 'green', warning: 'yellow', prompt: 'cyan', error: 'red' });
console.log(colors[type](this.options[type][message]));
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.