项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # colors.js [![Build Status](https://travis-ci.org/Marak/colors.js.svg?branch=master)](https://travis-ci.org/Marak/colors.js)
  2. ## get color and style in your node.js console
  3. ![Demo](https://raw.githubusercontent.com/Marak/colors.js/master/screenshots/colors.png)
  4. ## Installation
  5. npm install colors
  6. ## colors and styles!
  7. ### text colors
  8. - black
  9. - red
  10. - green
  11. - yellow
  12. - blue
  13. - magenta
  14. - cyan
  15. - white
  16. - gray
  17. - grey
  18. ### background colors
  19. - bgBlack
  20. - bgRed
  21. - bgGreen
  22. - bgYellow
  23. - bgBlue
  24. - bgMagenta
  25. - bgCyan
  26. - bgWhite
  27. ### styles
  28. - reset
  29. - bold
  30. - dim
  31. - italic
  32. - underline
  33. - inverse
  34. - hidden
  35. - strikethrough
  36. ### extras
  37. - rainbow
  38. - zebra
  39. - america
  40. - trap
  41. - random
  42. ## Usage
  43. By popular demand, `colors` now ships with two types of usages!
  44. The super nifty way
  45. ```js
  46. var colors = require('colors');
  47. console.log('hello'.green); // outputs green text
  48. console.log('i like cake and pies'.underline.red) // outputs red underlined text
  49. console.log('inverse the color'.inverse); // inverses the color
  50. console.log('OMG Rainbows!'.rainbow); // rainbow
  51. console.log('Run the trap'.trap); // Drops the bass
  52. ```
  53. or a slightly less nifty way which doesn't extend `String.prototype`
  54. ```js
  55. var colors = require('colors/safe');
  56. console.log(colors.green('hello')); // outputs green text
  57. console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
  58. console.log(colors.inverse('inverse the color')); // inverses the color
  59. console.log(colors.rainbow('OMG Rainbows!')); // rainbow
  60. console.log(colors.trap('Run the trap')); // Drops the bass
  61. ```
  62. I prefer the first way. Some people seem to be afraid of extending `String.prototype` and prefer the second way.
  63. If you are writing good code you will never have an issue with the first approach. If you really don't want to touch `String.prototype`, the second usage will not touch `String` native object.
  64. ## Disabling Colors
  65. To disable colors you can pass the following arguments in the command line to your application:
  66. ```bash
  67. node myapp.js --no-color
  68. ```
  69. ## Console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data)
  70. ```js
  71. var name = 'Marak';
  72. console.log(colors.green('Hello %s'), name);
  73. // outputs -> 'Hello Marak'
  74. ```
  75. ## Custom themes
  76. ### Using standard API
  77. ```js
  78. var colors = require('colors');
  79. colors.setTheme({
  80. silly: 'rainbow',
  81. input: 'grey',
  82. verbose: 'cyan',
  83. prompt: 'grey',
  84. info: 'green',
  85. data: 'grey',
  86. help: 'cyan',
  87. warn: 'yellow',
  88. debug: 'blue',
  89. error: 'red'
  90. });
  91. // outputs red text
  92. console.log("this is an error".error);
  93. // outputs yellow text
  94. console.log("this is a warning".warn);
  95. ```
  96. ### Using string safe API
  97. ```js
  98. var colors = require('colors/safe');
  99. // set single property
  100. var error = colors.red;
  101. error('this is red');
  102. // set theme
  103. colors.setTheme({
  104. silly: 'rainbow',
  105. input: 'grey',
  106. verbose: 'cyan',
  107. prompt: 'grey',
  108. info: 'green',
  109. data: 'grey',
  110. help: 'cyan',
  111. warn: 'yellow',
  112. debug: 'blue',
  113. error: 'red'
  114. });
  115. // outputs red text
  116. console.log(colors.error("this is an error"));
  117. // outputs yellow text
  118. console.log(colors.warn("this is a warning"));
  119. ```
  120. You can also combine them:
  121. ```javascript
  122. var colors = require('colors');
  123. colors.setTheme({
  124. custom: ['red', 'underline']
  125. });
  126. console.log('test'.custom);
  127. ```
  128. *Protip: There is a secret undocumented style in `colors`. If you find the style you can summon him.*