| 4853 | this.display = display ?? ":title :percent :bar :time :completed/:total"; |
| 4854 | } |
| 4855 | render(completed, options = {}) { |
| 4856 | if (this.isCompleted || !isTTY1) return; |
| 4857 | if (completed < 0) { |
| 4858 | throw new Error(`completed must greater than or equal to 0`); |
| 4859 | } |
| 4860 | const total = options.total ?? this.total ?? 100; |
| 4861 | const now = Date.now(); |
| 4862 | const ms = now - this.lastRender; |
| 4863 | if (ms < this.interval && completed < total) return; |
| 4864 | this.lastRender = now; |
| 4865 | const time = ((now - this.start) / 1000).toFixed(1) + "s"; |
| 4866 | const eta = completed == 0 ? "-" : (completed >= total ? 0 : (total / completed - 1) * (now - this.start) / 1000).toFixed(1) + "s"; |
| 4867 | const percent = (completed / total * 100).toFixed(2) + "%"; |
| 4868 | let str = this.display.replace(":title", options.title ?? this.title).replace(":time", time).replace(":eta", eta).replace(":percent", percent).replace(":completed", completed + "").replace(":total", total + ""); |
| 4869 | let availableSpace = Math.max(0, this.ttyColumns - str.replace(":bar", "").length); |
| 4870 | if (availableSpace && isWindow1) availableSpace -= 1; |
| 4871 | const width = Math.min(this.width, availableSpace); |
| 4872 | const finished = completed >= total; |
| 4873 | const preciseBar = options.preciseBar ?? this.preciseBar; |
| 4874 | const precision = preciseBar.length > 1; |
| 4875 | const completeLength = width * completed / total; |
| 4876 | const roundedCompleteLength = Math.floor(completeLength); |
| 4877 | let precise = ""; |
| 4878 | if (precision) { |
| 4879 | const preciseLength = completeLength - roundedCompleteLength; |
| 4880 | precise = finished ? "" : preciseBar[Math.floor(preciseBar.length * preciseLength)]; |
| 4881 | } |
| 4882 | const complete = new Array(roundedCompleteLength).fill(options.complete ?? this.complete).join(""); |
| 4883 | const incomplete = new Array(Math.max(width - roundedCompleteLength - (precision ? 1 : 0), 0)).fill(options.incomplete ?? this.incomplete).join(""); |
| 4884 | str = str.replace(":bar", complete + precise + incomplete); |
| 4885 | if (str.length < this.lastStr.length) { |
| 4886 | str += " ".repeat(this.lastStr.length - str.length); |
| 4887 | } |
| 4888 | if (str !== this.lastStr) { |
| 4889 | this.write(str); |
| 4890 | this.lastStr = str; |
| 4891 | } |
| 4892 | if (finished) this.end(); |
| 4893 | } |
| 4894 | end() { |
| 4895 | this.isCompleted = true; |
| 4896 | if (this.clear) { |