You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # color-support
  2. A module which will endeavor to guess your terminal's level of color
  3. support.
  4. [![Build Status](https://travis-ci.org/isaacs/color-support.svg?branch=master)](https://travis-ci.org/isaacs/color-support) [![Coverage Status](https://coveralls.io/repos/github/isaacs/color-support/badge.svg?branch=master)](https://coveralls.io/github/isaacs/color-support?branch=master)
  5. This is similar to `supports-color`, but it does not read
  6. `process.argv`.
  7. 1. If not in a node environment, not supported.
  8. 2. If stdout is not a TTY, not supported, unless the `ignoreTTY`
  9. option is set.
  10. 3. If the `TERM` environ is `dumb`, not supported, unless the
  11. `ignoreDumb` option is set.
  12. 4. If on Windows, then support 16 colors.
  13. 5. If using Tmux, then support 256 colors.
  14. 7. Handle continuous-integration servers. If `CI` or
  15. `TEAMCITY_VERSION` are set in the environment, and `TRAVIS` is not
  16. set, then color is not supported, unless `ignoreCI` option is set.
  17. 6. Guess based on the `TERM_PROGRAM` environ. These terminals support
  18. 16m colors:
  19. - `iTerm.app` version 3.x supports 16m colors, below support 256
  20. - `MacTerm` supports 16m colors
  21. - `Apple_Terminal` supports 256 colors
  22. - Have more things that belong on this list? Send a PR!
  23. 8. Make a guess based on the `TERM` environment variable. Any
  24. `xterm-256color` will get 256 colors. Any screen, xterm, vt100,
  25. color, ansi, cygwin, or linux `TERM` will get 16 colors.
  26. 9. If `COLORTERM` environment variable is set, then support 16 colors.
  27. 10. At this point, we assume that color is not supported.
  28. ## USAGE
  29. ```javascript
  30. var testColorSupport = require('color-support')
  31. var colorSupport = testColorSupport(/* options object */)
  32. if (!colorSupport) {
  33. console.log('color is not supported')
  34. } else if (colorSupport.has16m) {
  35. console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m')
  36. } else if (colorSupport.has256) {
  37. console.log('\x1b[38;5;119m256 colors\x1b[0m')
  38. } else if (colorSupport.hasBasic) {
  39. console.log('\x1b[31mbasic colors\x1b[0m')
  40. } else {
  41. console.log('this is impossible, but colors are not supported')
  42. }
  43. ```
  44. If you don't have any options to set, you can also just look at the
  45. flags which will all be set on the test function itself. (Of course,
  46. this doesn't return a falsey value when colors aren't supported, and
  47. doesn't allow you to set options.)
  48. ```javascript
  49. var colorSupport = require('color-support')
  50. if (colorSupport.has16m) {
  51. console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m')
  52. } else if (colorSupport.has256) {
  53. console.log('\x1b[38;5;119m256 colors\x1b[0m')
  54. } else if (colorSupport.hasBasic) {
  55. console.log('\x1b[31mbasic colors\x1b[0m')
  56. } else {
  57. console.log('colors are not supported')
  58. }
  59. ```
  60. ## Options
  61. You can pass in the following options.
  62. * ignoreTTY - default false. Ignore the `isTTY` check.
  63. * ignoreDumb - default false. Ignore `TERM=dumb` environ check.
  64. * ignoreCI - default false. Ignore `CI` environ check.
  65. * env - Object for environment vars. Defaults to `process.env`.
  66. * stream - Stream for `isTTY` check. Defaults to `process.stdout`.
  67. * term - String for `TERM` checking. Defaults to `env.TERM`.
  68. * alwaysReturn - default false. Return an object when colors aren't
  69. supported (instead of returning `false`).
  70. * level - A number from 0 to 3. This will return a result for the
  71. specified level. This is useful if you want to be able to set the
  72. color support level explicitly as a number in an environment
  73. variable or config, but then use the object flags in your program.
  74. Except for `alwaysReturn` to return an object for level 0, all other
  75. options are ignored, since no checking is done if a level is
  76. explicitly set.
  77. ## Return Value
  78. If no color support is available, then `false` is returned by default,
  79. unless the `alwaysReturn` flag is set to `true`. This is so that the
  80. simple question of "can I use colors or not" can treat any truthy
  81. return as "yes".
  82. Otherwise, the return object has the following fields:
  83. * `level` - A number from 0 to 3
  84. * `0` - No color support
  85. * `1` - Basic (16) color support
  86. * `2` - 256 color support
  87. * `3` - 16 million (true) color support
  88. * `hasBasic` - Boolean
  89. * `has256` - Boolean
  90. * `has16m` - Boolean
  91. ## CLI
  92. You can run the `color-support` bin from the command line which will
  93. just dump the values as this module calculates them in whatever env
  94. it's run. It takes no command line arguments.
  95. ## Credits
  96. This is a spiritual, if not actual, fork of
  97. [supports-color](http://npm.im/supports-color) by the ever prolific
  98. [Sindre Sorhus](http://npm.im/~sindresorhus).