From d8d504d7bfc4ed283a4175167d5b8353b17c39bd Mon Sep 17 00:00:00 2001 From: ido Date: Mon, 23 Sep 2024 01:05:10 +0300 Subject: [PATCH] fix(cli): space jump in cli progress --- .../base-transfer-cli-progress-bar.ts | 12 +++---- .../fancy-transfer-cli-progress-bar.ts | 2 +- .../transfer-visualize/utils/data-line.ts | 31 +++++-------------- 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/download/transfer-visualize/transfer-cli/progress-bars/base-transfer-cli-progress-bar.ts b/src/download/transfer-visualize/transfer-cli/progress-bars/base-transfer-cli-progress-bar.ts index 10400e3..8b2d436 100644 --- a/src/download/transfer-visualize/transfer-cli/progress-bars/base-transfer-cli-progress-bar.ts +++ b/src/download/transfer-visualize/transfer-cli/progress-bars/base-transfer-cli-progress-bar.ts @@ -95,6 +95,8 @@ export default class BaseTransferCliProgressBar implements TransferCliProgressBa protected getETA(spacer = " | "): DataLine { + const formatedTimeLeft = this.status.timeLeft < 1_000 ? "0s" : this.status.formatTimeLeft; + const timeLeft = `${formatedTimeLeft.padStart("10s".length)} left`; if (this.showETA) { return [{ type: "spacer", @@ -103,12 +105,8 @@ export default class BaseTransferCliProgressBar implements TransferCliProgressBa formatter: (text) => text }, { type: "timeLeft", - fullText: this.status.formatTimeLeft, - size: Math.max("100ms".length, this.status.formatTimeLeft.length) - }, { - type: "timeLeft", - fullText: " left", - size: " left".length + fullText: timeLeft, + size: timeLeft.length }]; } @@ -189,7 +187,7 @@ export default class BaseTransferCliProgressBar implements TransferCliProgressBa { type: "speed", fullText: formattedSpeed, - size: "000.00kB/s".length + size: "00.00kB/s".length }, ...this.getETA() ]); diff --git a/src/download/transfer-visualize/transfer-cli/progress-bars/fancy-transfer-cli-progress-bar.ts b/src/download/transfer-visualize/transfer-cli/progress-bars/fancy-transfer-cli-progress-bar.ts index cc41607..2dc0530 100644 --- a/src/download/transfer-visualize/transfer-cli/progress-bars/fancy-transfer-cli-progress-bar.ts +++ b/src/download/transfer-visualize/transfer-cli/progress-bars/fancy-transfer-cli-progress-bar.ts @@ -59,7 +59,7 @@ export default class FancyTransferCliProgressBar extends BaseTransferCliProgress }, { type: "speed", fullText: formattedSpeed, - size: "000.00kB/s".length + size: "00.00kB/s".length }, ...this.getETA(" ")]); } diff --git a/src/download/transfer-visualize/utils/data-line.ts b/src/download/transfer-visualize/utils/data-line.ts index 34b981d..46860c7 100644 --- a/src/download/transfer-visualize/utils/data-line.ts +++ b/src/download/transfer-visualize/utils/data-line.ts @@ -20,17 +20,14 @@ export function renderDataPart(dataPart: DataPart) { let text = dataPart.fullText; if (dataPart.cropper != null) { - text = padEqually( - dataPart - .cropper(text, dataPart.size) - .slice(0, dataPart.size), - dataPart.size - ); + text = dataPart + .cropper(text, dataPart.size) + .slice(0, dataPart.size) + .padEnd(dataPart.size); } else { - text = padEqually( - text.slice(0, dataPart.size), - dataPart.size - ); + text = text + .slice(0, dataPart.size) + .padEnd(dataPart.size); } if (dataPart.formatter != null) { @@ -96,17 +93,3 @@ export function resizeDataLine(dataLine: DataLine, lineLength: number) { return res; } - - -function padEqually(text: string, size: number) { - const padAmount = Math.max(0, size - text.length); - for (let i = 0; i < padAmount; i++) { - if (i % 2 == 0) { - text = " " + text; - } else { - text = text + " "; - } - } - - return text; -}