Permalink
Browse files

init

  • Loading branch information...
0 parents commit 7573ed7458d11c609179ef4fefda318905a7c6ee @sindresorhus sindresorhus committed Aug 3, 2013
Showing with 182 additions and 0 deletions.
  1. +12 −0 .editorconfig
  2. +1 −0 .gitattributes
  3. +1 −0 .gitignore
  4. +20 −0 .jshintrc
  5. +4 −0 .travis.yml
  6. +34 −0 has-color.js
  7. +49 −0 package.json
  8. +28 −0 readme.md
  9. +33 −0 test.js
@@ -0,0 +1,12 @@
+# editorconfig.org
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.md]
+trim_trailing_whitespace = false
@@ -0,0 +1 @@
+* text=auto
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,20 @@
+{
+ "node": true,
+ "esnext": true,
+ "bitwise": true,
+ "camelcase": true,
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "indent": 4,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "quotmark": "single",
+ "regexp": true,
+ "undef": true,
+ "unused": true,
+ "strict": true,
+ "trailing": true,
+ "smarttabs": true
+}
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - '0.10'
+ - '0.8'
@@ -0,0 +1,34 @@
+'use strict';
+module.exports = (function () {
+ var term;
+
+ if (process.argv.indexOf('--no-color') !== -1) {
+ return false;
+ }
+
+ if (process.argv.indexOf('--color') !== -1) {
+ return true;
+ }
+
+ if (!process.stdout.isTTY) {
+ return false;
+ }
+
+ if (process.platform === 'win32') {
+ return true;
+ }
+
+ if ('COLORTERM' in process.env) {
+ return true;
+ }
+
+ term = process.env.TERM;
+
+ if (!term) {
+ return false;
+ }
+
+ term = term.toLowerCase();
+
+ return term.indexOf('color') !== -1 || term === 'xterm' || term === 'linux';
@passy
passy Aug 3, 2013

Isn't there explicitly xterm-color?

@sindresorhus
sindresorhus Aug 3, 2013 Member

Yes, but there might be others too with color in the name, and I don't think all xterm's have color in the name.

@passy
passy Aug 3, 2013

I thought those without color in the name would implicitly have no color support. But that's just guessing.

@sindresorhus
sindresorhus Aug 3, 2013 Member

Looks like xterm means color support: http://en.wikipedia.org/wiki/Comparison_of_terminal_emulators
My guess is that different distributions names it differently.

In Terminal on my Mac i have to choice to emulate the following xterms: xterm, xterm-16color, xterm-256color

The above check would match all of them.

// @sleeper @btford

@sleeper
sleeper Aug 3, 2013

As said the easiest IMHO is to rely on the output of tput colors

@passy
passy Aug 3, 2013

You are right. I just started xterm and it identifies itself just as xterm while it displays colors just fine.

@sleeper
sleeper Aug 3, 2013

Additionally one can set his own terminal name .. As long as he'll fill-in correctly the termninfo, the tput colors will output the number of color it supports ... As long as it's higher than 8, you can consider ANSi is working ;)

@passy
passy via email Aug 3, 2013
@sindresorhus
sindresorhus Aug 3, 2013 Member

Downside of that is that tput would require spawning which is async. Introducing async would complicate everything. I could use execSync or something, but it has to compile and stuff... Suggestions?

@sleeper
sleeper Aug 3, 2013

Agreed. I would use execSync, only 1 time at the beginning ..or even write it as another function: the "simple" check is done by default, the tput check done is user really wants to ensure ...

@passy
passy via email Aug 3, 2013
@sindresorhus
sindresorhus Aug 3, 2013 Member

Hah, I found it yesterday too. lol. I even looked at the curses source code for hints on how to check.

@passy
passy Aug 3, 2013

Someone should shut one of us down. Too much redundancy.

+})();
@@ -0,0 +1,49 @@
+{
+ "name": "has-color",
+ "version": "0.0.0",
+ "description": "Detect whether a terminal supports color",
+ "keywords": [
+ "color",
+ "colour",
+ "colors",
+ "terminal",
+ "console",
+ "cli",
+ "ansi",
+ "styles",
+ "tty",
+ "rgb",
+ "256",
+ "shell",
+ "xterm",
+ "command-line",
+ "support",
+ "capability",
+ "detect"
+ ],
+ "homepage": "https://github.com/sindresorhus/has-color",
+ "bugs": "https://github.com/sindresorhus/has-color/issues",
+ "license": "MIT",
+ "author": {
+ "name": "Sindre Sorhus",
+ "email": "sindresorhus@gmail.com",
+ "url": "http://sindresorhus.com"
+ },
+ "files": [
+ "has-color.js"
+ ],
+ "main": "has-color",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/sindresorhus/has-color.git"
+ },
+ "scripts": {
+ "test": "mocha"
+ },
+ "devDependencies": {
+ "mocha": "~1.12.0"
+ },
+ "engines": {
+ "node": ">=0.8.0"
+ }
+}
@@ -0,0 +1,28 @@
+# has-color [![Build Status](https://secure.travis-ci.org/sindresorhus/has-color.png?branch=master)](http://travis-ci.org/sindresorhus/has-color)
+
+> Detect whether a terminal supports color.
+
+*Used in the terminal color module [chalk](https://github.com/sindresorhus/chalk).*
+
+
+## Install
+
+Install with [npm](https://npmjs.org/package/has-color): `npm install --save has-color`
+
+
+## Example
+
+```js
+var hasColor = require('has-color');
+
+if (hasColor) {
+ console.log('Terminal supports color.');
+}
+```
+
+It can be overriden by the user with the flags `--color` and `--no-color`.
+
+
+## License
+
+MIT License • © [Sindre Sorhus](http://sindresorhus.com)
@@ -0,0 +1,33 @@
+/*global describe, it, beforeEach */
+'use strict';
+var assert = require('assert');
+
+describe('hasColor', function () {
+ beforeEach(function () {
+ // clear the cache of the tested module
+ delete require.cache[require.resolve('./has-color')];
+ process.stdout.isTTY = true;
+ process.argv = [];
+ process.env = {};
+ });
+
+ it('should return false if not TTY', function () {
+ process.stdout.isTTY = false;
+ assert.equal(require('./has-color'), false);
+ });
+
+ it('should return false if --no-color flag is used', function () {
+ process.argv = ['--no-color'];
+ assert.equal(require('./has-color'), false);
+ });
+
+ it('should return true if --color flag is used', function () {
+ process.argv = ['--color'];
+ assert.equal(require('./has-color'), true);
+ });
+
+ it('should return true if `COLORTERM` is in env', function () {
+ process.env.COLORTERM = true;
+ assert.equal(require('./has-color'), true);
+ });
+});

0 comments on commit 7573ed7

Please sign in to comment.