progress.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. /*!
  3. * node-progress
  4. * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
  5. * MIT Licensed
  6. */
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.ProgressCallback = exports.ProgressBar = void 0;
  9. class ProgressBar {
  10. /**
  11. * Initialize a `ProgressBar` with the given `fmt` string and `options` or`total`.
  12. *
  13. * Options:
  14. * - `curr` current completed index
  15. * - `total` total number of ticks to complete
  16. * - `width` the displayed width of the progress bar defaulting to total
  17. * - `stream` the output stream defaulting to stderr
  18. * - `head` head character defaulting to complete character
  19. * - `complete` completion character defaulting to "="
  20. * - `incomplete` incomplete character defaulting to "-"
  21. * - `renderThrottle` minimum time between updates in milliseconds defaulting to 16
  22. * - `callback` optional function to call when the progress bar completes
  23. * - `clear` will clear the progress bar upon termination
  24. *
  25. * Tokens:
  26. * - `:bar` the progress bar itself
  27. * - `:current` current tick number
  28. * - `:total` total ticks
  29. * - `:elapsed` time elapsed in seconds
  30. * - `:percent` completion percentage
  31. * - `:eta` eta in seconds
  32. * - `:rate` rate of ticks per second
  33. */
  34. constructor(format, options = {}) {
  35. this.format = format;
  36. this.current = 0;
  37. this.total = 0;
  38. this.tokens = null;
  39. this.lastDraw = "";
  40. this.start = 0;
  41. this.complete = false;
  42. this.stream = options.stream || process.stderr;
  43. this.total = options.total;
  44. this.width = options.width || this.total;
  45. this.chars = {
  46. complete: options.complete || "=",
  47. incomplete: options.incomplete || "-",
  48. head: options.head || options.complete || "=",
  49. };
  50. }
  51. /**
  52. * "tick" the progress bar with optional `len` and optional `tokens`.
  53. */
  54. tick(delta) {
  55. this.currentAmount = this.current + delta;
  56. }
  57. set currentAmount(value) {
  58. this.current = value;
  59. if (this.complete) {
  60. return;
  61. }
  62. this.render();
  63. if (this.current >= this.total) {
  64. this.complete = true;
  65. this.terminate();
  66. }
  67. }
  68. render() {
  69. // start time for eta
  70. if (this.start === 0) {
  71. this.start = Date.now();
  72. }
  73. const ratio = Math.min(Math.max(this.current / this.total, 0), 1);
  74. const percent = ratio * 100;
  75. const elapsed = Date.now() - this.start;
  76. const eta = percent === 100 ? 0 : elapsed * (this.total / this.current - 1);
  77. const rate = this.current / (elapsed / 1000);
  78. /* populate the bar template with percentages and timestamps */
  79. let str = this.format
  80. .replace(":current", this.current.toString())
  81. .replace(":total", this.total.toString())
  82. .replace(":elapsed", isNaN(elapsed) ? "0.0" : (elapsed / 1000).toFixed(1))
  83. .replace(":eta", isNaN(eta) || !isFinite(eta) ? "0.0" : (eta / 1000).toFixed(1))
  84. .replace(":percent", percent.toFixed(0) + "%")
  85. .replace(":rate", Math.round(rate).toString());
  86. // compute the available space (non-zero) for the bar
  87. let availableSpace = Math.max(0, this.stream.columns - str.replace(":bar", "").length);
  88. if (availableSpace && process.platform === "win32") {
  89. availableSpace -= 1;
  90. }
  91. const width = Math.min(this.width, availableSpace);
  92. const completeLength = Math.round(width * ratio);
  93. let complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
  94. const incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
  95. /* add head to the complete string */
  96. if (completeLength > 0) {
  97. complete = `${complete.slice(0, -1)}${this.chars.head}`;
  98. }
  99. /* fill in the actual progress bar */
  100. str = str.replace(":bar", complete + incomplete);
  101. /* replace the extra tokens */
  102. if (this.tokens != null) {
  103. for (const key of Object.keys(this.tokens)) {
  104. str = str.replace(`:${key}`, this.tokens[key]);
  105. }
  106. }
  107. if (this.lastDraw !== str) {
  108. this.stream.cursorTo(0);
  109. this.stream.write(str);
  110. this.stream.clearLine(1);
  111. this.lastDraw = str;
  112. }
  113. }
  114. /**
  115. * "update" the progress bar to represent an exact percentage.
  116. * The ratio (between 0 and 1) specified will be multiplied by `total` and
  117. * floored, representing the closest available "tick." For example, if a
  118. * progress bar has a length of 3 and `update(0.5)` is called, the progress
  119. * will be set to 1.
  120. *
  121. * A ratio of 0.5 will attempt to set the progress to halfway.
  122. */
  123. update(ratio) {
  124. const goal = Math.floor(ratio * this.total);
  125. const delta = goal - this.current;
  126. this.tick(delta);
  127. }
  128. /**
  129. * "interrupt" the progress bar and write a message above it.
  130. */
  131. interrupt(message) {
  132. // clear the current line
  133. const stream = this.stream;
  134. stream.clearLine();
  135. // move the cursor to the start of the line
  136. stream.cursorTo(0);
  137. // write the message text
  138. stream.write(message);
  139. // terminate the line after writing the message
  140. stream.write("\n");
  141. // re-display the progress bar with its lastDraw
  142. stream.write(this.lastDraw);
  143. }
  144. }
  145. exports.ProgressBar = ProgressBar;
  146. class ProgressCallback {
  147. constructor(progressBar) {
  148. this.progressBar = progressBar;
  149. this.start = Date.now();
  150. this.nextUpdate = this.start + 1000;
  151. }
  152. update(transferred, total) {
  153. const now = Date.now();
  154. if (now >= this.nextUpdate || transferred >= total) {
  155. this.nextUpdate = now + 1000;
  156. this.progressBar.total = total;
  157. this.progressBar.currentAmount = transferred;
  158. }
  159. }
  160. }
  161. exports.ProgressCallback = ProgressCallback;
  162. //# sourceMappingURL=progress.js.map