Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

colors

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

colors - npm Package Compare versions

Comparing version 0.5.1 to 0.6.0

test.js

94

colors.js
/*
colors.js
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
Copyright (c) 2010
Marak Squires
Alexis Sellier (cloudhead)
Permission is hereby granted, free of charge, to any person obtaining a copy

@@ -26,7 +29,25 @@ of this software and associated documentation files (the "Software"), to deal

exports.mode = "console";
var isHeadless = false;
// prototypes the string object to have additional method calls that add terminal colors
if (typeof module !== 'undefined') {
isHeadless = true;
}
if (!isHeadless) {
var exports = {};
var module = {};
var colors = exports;
exports.mode = "browser";
} else {
exports.mode = "console";
}
//
// Prototypes the string object to have additional method calls that add terminal colors
//
var addProperty = function (color, func) {
var allowOverride = ['bold'];
if (""[color] && allowOverride.indexOf(color) === -1) {
throw new Error(color + ' already exists on String.prototype, cannot override.')
}
exports[color] = function(str) {

@@ -38,5 +59,9 @@ return func.apply(str);

var isHeadless = (typeof module !== 'undefined');
['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
//
// Iterate through all default styles and colors
//
var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
x.forEach(function (style) {
// __defineGetter__ at the least works in more browsers

@@ -46,30 +71,48 @@ // http://robertnyman.com/javascript/javascript-getters-setters.html

addProperty(style, function () {
return isHeadless ?
stylize(this, style) : // for those running in node (headless environments)
this.replace(/( )/, '$1'); // and for those running in browsers:
// re: ^ you'd think 'return this' works (but doesn't) so replace coerces the string to be a real string
return stylize(this, style);
});
});
// prototypes string with method "rainbow"
// rainbow will apply a the color spectrum to a string, changing colors every letter
addProperty('rainbow', function () {
if (!isHeadless) {
return this.replace(/( )/, '$1');
function sequencer(map) {
return function () {
if (!isHeadless) {
return this.replace(/( )/, '$1');
}
var exploded = this.split("");
var i = 0;
exploded = exploded.map(map);
return exploded.join("");
}
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = this.split("");
var i=0;
exploded = exploded.map(function(letter) {
if (letter==" ") {
}
var rainbowMap = (function () {
var rainbowColors = ['red','yellow','green','blue','magenta']; //RoY G BiV
return function (letter, i, exploded) {
if (letter == " ") {
return letter;
} else {
return stylize(letter, rainbowColors[i++ % rainbowColors.length]);
}
else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
}
});
return exploded.join("");
}
})();
exports.addSequencer = function (name, map) {
addProperty(name, sequencer(map));
}
exports.addSequencer('rainbow', rainbowMap);
exports.addSequencer('zebra', function (letter, i, exploded) {
return i % 2 === 0 ? letter : letter.inverse;
});
exports.setTheme = function (theme) {
Object.keys(theme).forEach(function(prop){
addProperty(prop, function(){
return exports[theme[prop]](this);
});
});
}
function stylize(str, style) {
if (exports.mode == 'console') {

@@ -114,7 +157,6 @@ var styles = {

} else if (exports.mode == 'none') {
return str;
return str;
} else {
console.log('unsupported mode, try "browser", "console" or "none"');
}
return styles[style][0] + str + styles[style][1];

@@ -121,0 +163,0 @@ };

@@ -1,2 +0,1 @@

var util = require('util');
var colors = require('./colors');

@@ -7,15 +6,61 @@

var test = colors.red("hopefully colorless output");
util.puts('Rainbows are fun!'.rainbow);
util.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
util.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
//util.puts('zalgo time!'.zalgo);
util.puts(test.stripColors);
util.puts("a".grey + " b".black);
console.log('Rainbows are fun!'.rainbow);
console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
console.log('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
//console.log('zalgo time!'.zalgo);
console.log(test.stripColors);
console.log("a".grey + " b".black);
util.puts(colors.rainbow('Rainbows are fun!'));
util.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
util.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//util.puts(colors.zalgo('zalgo time!'));
util.puts(colors.stripColors(test));
util.puts(colors.grey("a") + colors.black(" b"));
console.log("Zebras are so fun!".zebra);
console.log(colors.rainbow('Rainbows are fun!'));
console.log(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
console.log(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//console.log(colors.zalgo('zalgo time!'));
console.log(colors.stripColors(test));
console.log(colors.grey("a") + colors.black(" b"));
colors.addSequencer("america", function(letter, i, exploded) {
if(letter === " ") return letter;
switch(i%3) {
case 0: return letter.red;
case 1: return letter.white;
case 2: return letter.blue;
}
});
colors.addSequencer("random", (function() {
var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
return function(letter, i, exploded) {
return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
};
})());
console.log("AMERICA! F--K YEAH!".america);
console.log("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random);
//
// Custom themes
//
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);

@@ -1,3 +0,6 @@

Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
Copyright (c) 2010
Marak Squires
Alexis Sellier (cloudhead)
Permission is hereby granted, free of charge, to any person obtaining a copy

@@ -4,0 +7,0 @@ of this software and associated documentation files (the "Software"), to deal

{
"name": "colors",
"description": "get colors in your node.js console like what",
"version": "0.5.1",
"version": "0.6.0",
"author": "Marak Squires",

@@ -6,0 +6,0 @@ "repository": {

@@ -1,14 +0,12 @@

<h1>colors.js - get color and style in your node.js console like what</h1>
# colors.js - get color and style in your node.js console ( and browser ) like what
<img src="http://i.imgur.com/goJdO.png" border = "0"/>
var sys = require('sys');
var colors = require('./colors');
sys.puts('hello'.green); // outputs green text
sys.puts('i like cake and pies'.underline.red) // outputs red underlined text
sys.puts('inverse the color'.inverse); // inverses the color
sys.puts('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
<h2>colors and styles!</h2>
## Installation
npm install colors
## colors and styles!
- bold

@@ -26,6 +24,55 @@ - italic

- blue
- rainbow
- zebra
- random
## Usage
### Authors
``` js
var colors = require('./colors');
#### Alexis Sellier (cloudhead) , Marak Squires , Justin Campbell, Dustin Diaz (@ded)
console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
```
# Creating Custom themes
```js
var require('colors');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);
```
### Contributors
Marak (Marak Squires)
Alexis Sellier (cloudhead)
mmalecki (Maciej Małecki)
nicoreed (Nico Reed)
morganrallen (Morgan Allen)
JustinCampbell (Justin Campbell)
ded (Dustin Diaz)
#### , Marak Squires , Justin Campbell, Dustin Diaz (@ded)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc