mirror of
https://github.com/flynx/colors.js.git
synced 2025-10-29 11:00:11 +00:00
commit
cc857f2977
4
.npmignore
Normal file
4
.npmignore
Normal file
@ -0,0 +1,4 @@
|
||||
# Development files
|
||||
/tests/
|
||||
/.travis.yml
|
||||
/screenshots
|
||||
@ -1,5 +1,10 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "9"
|
||||
- "8"
|
||||
- "7"
|
||||
- "6"
|
||||
- "5"
|
||||
- "4"
|
||||
- "0.12"
|
||||
- "0.11"
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
# colors.js [](https://travis-ci.org/Marak/colors.js)
|
||||
# colors.js
|
||||
[](https://travis-ci.org/Marak/colors.js)
|
||||
[](https://www.npmjs.org/package/colors)
|
||||
[](https://david-dm.org/Marak/colors.js)
|
||||
[](https://david-dm.org/Marak/colors.js#info=devDependencies)
|
||||
|
||||
**Update on colors.js roadmap (2018-02-16):** The maintainers have published a pre-release version of `colors` that addresses a number of compatibility issues raised by community. You may install the
|
||||
RC version by specifying `next` or `>=1.2.0-rc0` as the version for `colors` in your `package.json`. We are eager to receive community feedback on this version in order to move it out of pre-release.
|
||||
Please open Issues to provide feedback, and check the `develop` branch for the latest bleeding-edge updates. See also the [roadmap](ROADMAP.md).
|
||||
Please check out the [roadmap](ROADMAP.md) for upcoming features and releases. Please open Issues to provide feedback, and check the `develop` branch for the latest bleeding-edge updates.
|
||||
|
||||
## get color and style in your node.js console
|
||||
|
||||
@ -60,7 +60,11 @@ console.log("this is an input".input);
|
||||
console.log('Generic logging theme as file'.green.bold.underline);
|
||||
|
||||
// Load a theme from file
|
||||
colors.setTheme(__dirname + '/../themes/generic-logging.js');
|
||||
try {
|
||||
colors.setTheme(require(__dirname + '/../themes/generic-logging.js'));
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
// outputs red text
|
||||
console.log("this is an error".error);
|
||||
|
||||
134
index.d.ts
vendored
Normal file
134
index.d.ts
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
// Type definitions for Colors.js 1.2
|
||||
// Project: https://github.com/Marak/colors.js
|
||||
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Staffan Eketorp <https://github.com/staeke>
|
||||
// Definitions: https://github.com/Marak/colors.js
|
||||
|
||||
export interface Color {
|
||||
(text: string): string;
|
||||
|
||||
strip: Color;
|
||||
stripColors: Color;
|
||||
|
||||
black: Color;
|
||||
red: Color;
|
||||
green: Color;
|
||||
yellow: Color;
|
||||
blue: Color;
|
||||
magenta: Color;
|
||||
cyan: Color;
|
||||
white: Color;
|
||||
gray: Color;
|
||||
grey: Color;
|
||||
|
||||
bgBlack: Color;
|
||||
bgRed: Color;
|
||||
bgGreen: Color;
|
||||
bgYellow: Color;
|
||||
bgBlue: Color;
|
||||
bgMagenta: Color;
|
||||
bgCyan: Color;
|
||||
bgWhite: Color;
|
||||
|
||||
reset: Color;
|
||||
bold: Color;
|
||||
dim: Color;
|
||||
italic: Color;
|
||||
underline: Color;
|
||||
inverse: Color;
|
||||
hidden: Color;
|
||||
strikethrough: Color;
|
||||
|
||||
rainbow: Color;
|
||||
zebra: Color;
|
||||
america: Color;
|
||||
trap: Color;
|
||||
random: Color;
|
||||
zalgo: Color;
|
||||
}
|
||||
|
||||
export function setTheme(theme: any): void;
|
||||
|
||||
export let enabled: boolean;
|
||||
|
||||
export const strip: Color;
|
||||
export const stripColors: Color;
|
||||
|
||||
export const black: Color;
|
||||
export const red: Color;
|
||||
export const green: Color;
|
||||
export const yellow: Color;
|
||||
export const blue: Color;
|
||||
export const magenta: Color;
|
||||
export const cyan: Color;
|
||||
export const white: Color;
|
||||
export const gray: Color;
|
||||
export const grey: Color;
|
||||
|
||||
export const bgBlack: Color;
|
||||
export const bgRed: Color;
|
||||
export const bgGreen: Color;
|
||||
export const bgYellow: Color;
|
||||
export const bgBlue: Color;
|
||||
export const bgMagenta: Color;
|
||||
export const bgCyan: Color;
|
||||
export const bgWhite: Color;
|
||||
|
||||
export const reset: Color;
|
||||
export const bold: Color;
|
||||
export const dim: Color;
|
||||
export const italic: Color;
|
||||
export const underline: Color;
|
||||
export const inverse: Color;
|
||||
export const hidden: Color;
|
||||
export const strikethrough: Color;
|
||||
|
||||
export const rainbow: Color;
|
||||
export const zebra: Color;
|
||||
export const america: Color;
|
||||
export const trap: Color;
|
||||
export const random: Color;
|
||||
export const zalgo: Color;
|
||||
|
||||
declare global {
|
||||
interface String {
|
||||
strip: string;
|
||||
stripColors: string;
|
||||
|
||||
black: string;
|
||||
red: string;
|
||||
green: string;
|
||||
yellow: string;
|
||||
blue: string;
|
||||
magenta: string;
|
||||
cyan: string;
|
||||
white: string;
|
||||
gray: string;
|
||||
grey: string;
|
||||
|
||||
bgBlack: string;
|
||||
bgRed: string;
|
||||
bgGreen: string;
|
||||
bgYellow: string;
|
||||
bgBlue: string;
|
||||
bgMagenta: string;
|
||||
bgCyan: string;
|
||||
bgWhite: string;
|
||||
|
||||
reset: string;
|
||||
// @ts-ignore
|
||||
bold: string;
|
||||
dim: string;
|
||||
italic: string;
|
||||
underline: string;
|
||||
inverse: string;
|
||||
hidden: string;
|
||||
strikethrough: string;
|
||||
|
||||
rainbow: string;
|
||||
zebra: string;
|
||||
america: string;
|
||||
trap: string;
|
||||
random: string;
|
||||
zalgo: string;
|
||||
}
|
||||
}
|
||||
@ -36,10 +36,10 @@ colors.themes = {};
|
||||
var ansiStyles = colors.styles = require('./styles');
|
||||
var defineProps = Object.defineProperties;
|
||||
|
||||
colors.supportsColor = require('./system/supports-colors');
|
||||
colors.supportsColor = require('./system/supports-colors').supportsColor;
|
||||
|
||||
if (typeof colors.enabled === "undefined") {
|
||||
colors.enabled = colors.supportsColor;
|
||||
colors.enabled = colors.supportsColor() !== false;
|
||||
}
|
||||
|
||||
colors.stripColors = colors.strip = function(str){
|
||||
@ -115,7 +115,14 @@ function applyStyle() {
|
||||
return str;
|
||||
}
|
||||
|
||||
function applyTheme (theme) {
|
||||
colors.setTheme = function (theme) {
|
||||
if (typeof theme === 'string') {
|
||||
console.log('colors.setTheme now only accepts an object, not a string. ' +
|
||||
'If you are trying to set a theme from a file, it is now your (the caller\'s) responsibility to require the file. ' +
|
||||
'The old syntax looked like colors.setTheme(__dirname + \'/../themes/generic-logging.js\'); ' +
|
||||
'The new syntax looks like colors.setTheme(require(__dirname + \'/../themes/generic-logging.js\'));');
|
||||
return;
|
||||
}
|
||||
for (var style in theme) {
|
||||
(function(style){
|
||||
colors[style] = function(str){
|
||||
@ -132,21 +139,6 @@ function applyTheme (theme) {
|
||||
}
|
||||
}
|
||||
|
||||
colors.setTheme = function (theme) {
|
||||
if (typeof theme === 'string') {
|
||||
try {
|
||||
colors.themes[theme] = require(theme);
|
||||
applyTheme(colors.themes[theme]);
|
||||
return colors.themes[theme];
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
applyTheme(theme);
|
||||
}
|
||||
};
|
||||
|
||||
function init() {
|
||||
var ret = {};
|
||||
Object.keys(styles).forEach(function (name) {
|
||||
|
||||
@ -67,7 +67,7 @@ module['exports'] = function () {
|
||||
var stringPrototypeBlacklist = [
|
||||
'__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
|
||||
'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
|
||||
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
|
||||
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'repeat', 'replace', 'search', 'slice', 'split', 'substring',
|
||||
'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
|
||||
];
|
||||
|
||||
|
||||
23
lib/system/has-flag.js
Normal file
23
lib/system/has-flag.js
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function (flag, argv) {
|
||||
argv = argv || process.argv;
|
||||
|
||||
var terminatorPos = argv.indexOf('--');
|
||||
var prefix = /^-{1,2}/.test(flag) ? '' : '--';
|
||||
var pos = argv.indexOf(prefix + flag);
|
||||
|
||||
return pos !== -1 && (terminatorPos === -1 ? true : pos < terminatorPos);
|
||||
};
|
||||
@ -23,39 +23,125 @@ THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
var argv = process.argv;
|
||||
'use strict';
|
||||
|
||||
module.exports = (function () {
|
||||
if (argv.indexOf('--no-color') !== -1 ||
|
||||
argv.indexOf('--color=false') !== -1) {
|
||||
return false;
|
||||
}
|
||||
var os = require('os');
|
||||
var hasFlag = require('./has-flag.js');
|
||||
|
||||
if (argv.indexOf('--color') !== -1 ||
|
||||
argv.indexOf('--color=true') !== -1 ||
|
||||
argv.indexOf('--color=always') !== -1) {
|
||||
return true;
|
||||
}
|
||||
var env = process.env;
|
||||
|
||||
if (process.stdout && !process.stdout.isTTY) {
|
||||
return false;
|
||||
}
|
||||
var forceColor = void 0;
|
||||
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) {
|
||||
forceColor = false;
|
||||
} else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) {
|
||||
forceColor = true;
|
||||
}
|
||||
if ('FORCE_COLOR' in env) {
|
||||
forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0;
|
||||
}
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
return true;
|
||||
}
|
||||
function translateLevel(level) {
|
||||
if (level === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ('COLORTERM' in process.env) {
|
||||
return true;
|
||||
}
|
||||
return {
|
||||
level: level,
|
||||
hasBasic: true,
|
||||
has256: level >= 2,
|
||||
has16m: level >= 3
|
||||
};
|
||||
}
|
||||
|
||||
if (process.env.TERM === 'dumb') {
|
||||
return false;
|
||||
}
|
||||
function supportsColor(stream) {
|
||||
if (forceColor === false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) {
|
||||
return true;
|
||||
}
|
||||
if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
return false;
|
||||
})();
|
||||
if (hasFlag('color=256')) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (stream && !stream.isTTY && forceColor !== true) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var min = forceColor ? 1 : 0;
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
// Node.js 7.5.0 is the first version of Node.js to include a patch to
|
||||
// libuv that enables 256 color output on Windows. Anything earlier and it
|
||||
// won't work. However, here we target Node.js 8 at minimum as it is an LTS
|
||||
// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
|
||||
// release that supports 256 colors. Windows 10 build 14931 is the first release
|
||||
// that supports 16m/TrueColor.
|
||||
var osRelease = os.release().split('.');
|
||||
if (Number(process.versions.node.split('.')[0]) >= 8 && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
||||
return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ('CI' in env) {
|
||||
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(function (sign) {
|
||||
return sign in env;
|
||||
}) || env.CI_NAME === 'codeship') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
if ('TEAMCITY_VERSION' in env) {
|
||||
return (/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0
|
||||
);
|
||||
}
|
||||
|
||||
if ('TERM_PROGRAM' in env) {
|
||||
var version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
||||
|
||||
switch (env.TERM_PROGRAM) {
|
||||
case 'iTerm.app':
|
||||
return version >= 3 ? 3 : 2;
|
||||
case 'Hyper':
|
||||
return 3;
|
||||
case 'Apple_Terminal':
|
||||
return 2;
|
||||
// No default
|
||||
}
|
||||
}
|
||||
|
||||
if (/-256(color)?$/i.test(env.TERM)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ('COLORTERM' in env) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (env.TERM === 'dumb') {
|
||||
return min;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
function getSupportLevel(stream) {
|
||||
var level = supportsColor(stream);
|
||||
return translateLevel(level);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
supportsColor: getSupportLevel,
|
||||
stdout: getSupportLevel(process.stdout),
|
||||
stderr: getSupportLevel(process.stderr)
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "colors",
|
||||
"description": "get colors in your node.js console",
|
||||
"version": "1.1.2",
|
||||
"version": "1.2.0",
|
||||
"author": "Marak Squires",
|
||||
"homepage": "https://github.com/Marak/colors.js",
|
||||
"bugs": "https://github.com/Marak/colors.js/issues",
|
||||
@ -17,7 +17,7 @@
|
||||
"engines": {
|
||||
"node": ">=0.1.90"
|
||||
},
|
||||
"main": "lib",
|
||||
"main": "lib/index.js",
|
||||
"files": [
|
||||
"examples",
|
||||
"lib",
|
||||
|
||||
45
safe.d.ts
vendored
Normal file
45
safe.d.ts
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
// Type definitions for Colors.js 1.2
|
||||
// Project: https://github.com/Marak/colors.js
|
||||
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Staffan Eketorp <https://github.com/staeke>
|
||||
// Definitions: https://github.com/Marak/colors.js
|
||||
|
||||
export const enabled: boolean;
|
||||
|
||||
export function strip(str: string): string;
|
||||
export function stripColors(str: string): string;
|
||||
|
||||
export function black(str: string): string;
|
||||
export function red(str: string): string;
|
||||
export function green(str: string): string;
|
||||
export function yellow(str: string): string;
|
||||
export function blue(str: string): string;
|
||||
export function magenta(str: string): string;
|
||||
export function cyan(str: string): string;
|
||||
export function white(str: string): string;
|
||||
export function gray(str: string): string;
|
||||
export function grey(str: string): string;
|
||||
|
||||
export function bgBlack(str: string): string;
|
||||
export function bgRed(str: string): string;
|
||||
export function bgGreen(str: string): string;
|
||||
export function bgYellow(str: string): string;
|
||||
export function bgBlue(str: string): string;
|
||||
export function bgMagenta(str: string): string;
|
||||
export function bgCyan(str: string): string;
|
||||
export function bgWhite(str: string): string;
|
||||
|
||||
export function reset(str: string): string;
|
||||
export function bold(str: string): string;
|
||||
export function dim(str: string): string;
|
||||
export function italic(str: string): string;
|
||||
export function underline(str: string): string;
|
||||
export function inverse(str: string): string;
|
||||
export function hidden(str: string): string;
|
||||
export function strikethrough(str: string): string;
|
||||
|
||||
export function rainbow(str: string): string;
|
||||
export function zebra(str: string): string;
|
||||
export function america(str: string): string;
|
||||
export function trap(str: string): string;
|
||||
export function random(str: string): string;
|
||||
export function zalgo(str: string): string;
|
||||
Loading…
x
Reference in New Issue
Block a user