-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: only one cli print at the same time
- Loading branch information
Showing
8 changed files
with
114 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/download/transfer-visualize/transfer-cli/cli-animation-wrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import DownloadEngineNodejs from "../../download-engine/engine/download-engine-nodejs.js"; | ||
import DownloadEngineMultiDownload from "../../download-engine/engine/download-engine-multi-download.js"; | ||
import switchCliProgressStyle, {AvailableCLIProgressStyle} from "./progress-bars/switch-cli-progress-style.js"; | ||
import {CliFormattedStatus} from "./progress-bars/base-transfer-cli-progress-bar.js"; | ||
import TransferCli, {CLI_LEVEL, TransferCliOptions} from "./transfer-cli.js"; | ||
|
||
const DEFAULT_CLI_STYLE: AvailableCLIProgressStyle = "fancy"; | ||
type AllowedDownloadEngines = DownloadEngineNodejs | DownloadEngineMultiDownload; | ||
|
||
export type CliProgressDownloadEngineOptions = { | ||
truncateName?: boolean | number; | ||
cliProgress?: boolean; | ||
cliStyle?: AvailableCLIProgressStyle | ((status: CliFormattedStatus) => string) | ||
cliName?: string; | ||
cliAction?: string; | ||
fetchStrategy?: "localFile" | "fetch"; | ||
/** @internal */ | ||
cliLevel?: CLI_LEVEL; | ||
}; | ||
|
||
export default class CliAnimationWrapper { | ||
private readonly _downloadEngine: Promise<AllowedDownloadEngines>; | ||
private readonly _options: CliProgressDownloadEngineOptions; | ||
private _activeCLI?: TransferCli; | ||
|
||
public constructor(downloadEngine: Promise<AllowedDownloadEngines>, _options: CliProgressDownloadEngineOptions) { | ||
this._options = _options; | ||
this._downloadEngine = downloadEngine; | ||
this._init(); | ||
} | ||
|
||
private _init() { | ||
if (!this._options.cliProgress) { | ||
return; | ||
} | ||
this._options.cliAction ??= this._options.fetchStrategy === "localFile" ? "Copying" : "Downloading"; | ||
|
||
const cliOptions: Partial<TransferCliOptions> = {...this._options}; | ||
if (this._options.cliAction) { | ||
cliOptions.action = this._options.cliAction; | ||
} | ||
if (this._options.cliName) { | ||
cliOptions.name = this._options.cliName; | ||
} | ||
|
||
cliOptions.createProgressBar = typeof this._options.cliStyle === "function" ? | ||
this._options.cliStyle : | ||
switchCliProgressStyle(this._options.cliStyle ?? DEFAULT_CLI_STYLE, {truncateName: this._options.truncateName}); | ||
|
||
this._activeCLI = new TransferCli(cliOptions, this._options.cliLevel); | ||
} | ||
|
||
public async attachAnimation() { | ||
if (!this._activeCLI) { | ||
return; | ||
} | ||
this._activeCLI.loadingAnimation.start(); | ||
const engine = await this._downloadEngine; | ||
this._activeCLI.loadingAnimation.stop(); | ||
|
||
engine.once("start", () => { | ||
this._activeCLI?.start(); | ||
|
||
engine.on("progress", () => { | ||
this._activeCLI?.updateStatues(engine.downloadStatues); | ||
}); | ||
|
||
engine.on("closed", () => { | ||
this._activeCLI?.stop(); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters