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.

146 lines
3.4 KiB

преди 4 години
  1. Flexible ascii progress bar.
  2. ## Installation
  3. ```bash
  4. $ npm install progress
  5. ```
  6. ## Usage
  7. First we create a `ProgressBar`, giving it a format string
  8. as well as the `total`, telling the progress bar when it will
  9. be considered complete. After that all we need to do is `tick()` appropriately.
  10. ```javascript
  11. var ProgressBar = require('progress');
  12. var bar = new ProgressBar(':bar', { total: 10 });
  13. var timer = setInterval(function () {
  14. bar.tick();
  15. if (bar.complete) {
  16. console.log('\ncomplete\n');
  17. clearInterval(timer);
  18. }
  19. }, 100);
  20. ```
  21. ### Options
  22. These are keys in the options object you can pass to the progress bar along with
  23. `total` as seen in the example above.
  24. - `curr` current completed index
  25. - `total` total number of ticks to complete
  26. - `width` the displayed width of the progress bar defaulting to total
  27. - `stream` the output stream defaulting to stderr
  28. - `head` head character defaulting to complete character
  29. - `complete` completion character defaulting to "="
  30. - `incomplete` incomplete character defaulting to "-"
  31. - `renderThrottle` minimum time between updates in milliseconds defaulting to 16
  32. - `clear` option to clear the bar on completion defaulting to false
  33. - `callback` optional function to call when the progress bar completes
  34. ### Tokens
  35. These are tokens you can use in the format of your progress bar.
  36. - `:bar` the progress bar itself
  37. - `:current` current tick number
  38. - `:total` total ticks
  39. - `:elapsed` time elapsed in seconds
  40. - `:percent` completion percentage
  41. - `:eta` estimated completion time in seconds
  42. - `:rate` rate of ticks per second
  43. ### Custom Tokens
  44. You can define custom tokens by adding a `{'name': value}` object parameter to your method (`tick()`, `update()`, etc.) calls.
  45. ```javascript
  46. var bar = new ProgressBar(':current: :token1 :token2', { total: 3 })
  47. bar.tick({
  48. 'token1': "Hello",
  49. 'token2': "World!\n"
  50. })
  51. bar.tick(2, {
  52. 'token1': "Goodbye",
  53. 'token2': "World!"
  54. })
  55. ```
  56. The above example would result in the output below.
  57. ```
  58. 1: Hello World!
  59. 3: Goodbye World!
  60. ```
  61. ## Examples
  62. ### Download
  63. In our download example each tick has a variable influence, so we pass the chunk
  64. length which adjusts the progress bar appropriately relative to the total
  65. length.
  66. ```javascript
  67. var ProgressBar = require('progress');
  68. var https = require('https');
  69. var req = https.request({
  70. host: 'download.github.com',
  71. port: 443,
  72. path: '/visionmedia-node-jscoverage-0d4608a.zip'
  73. });
  74. req.on('response', function(res){
  75. var len = parseInt(res.headers['content-length'], 10);
  76. console.log();
  77. var bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', {
  78. complete: '=',
  79. incomplete: ' ',
  80. width: 20,
  81. total: len
  82. });
  83. res.on('data', function (chunk) {
  84. bar.tick(chunk.length);
  85. });
  86. res.on('end', function () {
  87. console.log('\n');
  88. });
  89. });
  90. req.end();
  91. ```
  92. The above example result in a progress bar like the one below.
  93. ```
  94. downloading [===== ] 39/bps 29% 3.7s
  95. ```
  96. ### Interrupt
  97. To display a message during progress bar execution, use `interrupt()`
  98. ```javascript
  99. var ProgressBar = require('progress');
  100. var bar = new ProgressBar(':bar :current/:total', { total: 10 });
  101. var timer = setInterval(function () {
  102. bar.tick();
  103. if (bar.complete) {
  104. clearInterval(timer);
  105. } else if (bar.curr === 5) {
  106. bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
  107. }
  108. }, 1000);
  109. ```
  110. You can see more examples in the `examples` folder.
  111. ## License
  112. MIT