openai-cli-unofficial
Version:
A powerful OpenAI CLI Coding Agent built with TypeScript
207 lines • 6.25 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiPhaseProgress = exports.StepProgress = exports.ProgressBar = void 0;
const chalk_1 = __importDefault(require("chalk"));
/**
* 终端进度条工具
*/
class ProgressBar {
constructor(total, options = {}) {
this.current = 0;
this.total = 100;
this.width = 30;
this.format = '{bar} {percentage}% | {current}/{total} | {phase}';
this.lastLine = '';
this.clear = true;
this.total = total;
this.width = options.width || 30;
this.format = options.format || this.format;
this.clear = options.clear !== false;
}
/**
* 更新进度
*/
update(current, data = {}) {
this.current = Math.min(current, this.total);
const percentage = Math.round((this.current / this.total) * 100);
const completed = Math.round((this.current / this.total) * this.width);
const remaining = this.width - completed;
// 构建进度条
const bar = chalk_1.default.green('='.repeat(completed)) + chalk_1.default.gray('-'.repeat(remaining));
// 构建显示文本
let line = this.format
.replace('{bar}', bar)
.replace('{percentage}', percentage.toString().padStart(3))
.replace('{current}', this.current.toString())
.replace('{total}', this.total.toString())
.replace('{phase}', data.phase || '');
if (data.file) {
line += chalk_1.default.dim(` (${data.file})`);
}
// 清除上一行并输出新的进度
if (this.clear && this.lastLine) {
process.stdout.write('\r' + ' '.repeat(this.lastLine.length) + '\r');
}
process.stdout.write(line);
this.lastLine = line;
}
/**
* 完成进度条
*/
complete(message) {
this.update(this.total);
if (message) {
if (this.clear) {
process.stdout.write('\r' + ' '.repeat(this.lastLine.length) + '\r');
}
else {
process.stdout.write('\n');
}
console.log(chalk_1.default.green('[OK] ' + message));
}
else {
process.stdout.write('\n');
}
}
/**
* 显示错误
*/
error(message) {
if (this.clear && this.lastLine) {
process.stdout.write('\r' + ' '.repeat(this.lastLine.length) + '\r');
}
else if (!message) {
// 如果没有消息且不清除,只是换行
process.stdout.write('\n');
return;
}
else {
process.stdout.write('\n');
}
// 只有当message不为空时才输出错误信息
if (message) {
console.log(chalk_1.default.red('[ERROR] ' + message));
}
}
/**
* 清空当前行
*/
clearLine() {
if (this.lastLine) {
process.stdout.write('\r' + ' '.repeat(this.lastLine.length) + '\r');
this.lastLine = '';
}
}
}
exports.ProgressBar = ProgressBar;
/**
* 创建简单的步骤进度显示器
*/
class StepProgress {
constructor(steps) {
this.steps = [];
this.currentStep = 0;
this.startTime = Date.now();
this.steps = steps;
}
/**
* 开始下一步
*/
nextStep() {
if (this.currentStep < this.steps.length) {
const step = this.steps[this.currentStep];
const stepNum = this.currentStep + 1;
const total = this.steps.length;
console.log(chalk_1.default.blue(`[${stepNum}/${total}]`) + ` ${step}`);
this.currentStep++;
}
}
/**
* 完成所有步骤
*/
complete() {
const elapsed = Date.now() - this.startTime;
const seconds = (elapsed / 1000).toFixed(1);
console.log(chalk_1.default.green(`✅ 所有步骤完成!用时 ${seconds} 秒`));
}
/**
* 显示错误
*/
error(message) {
console.log(chalk_1.default.red(`❌ ${message}`));
}
}
exports.StepProgress = StepProgress;
/**
* 多阶段进度管理器
*/
class MultiPhaseProgress {
constructor(phases) {
this.phases = [];
this.currentPhase = 0;
this.phaseProgress = 0;
this.phases = phases;
const totalWeight = phases.reduce((sum, phase) => sum + phase.weight, 0);
this.progressBar = new ProgressBar(totalWeight, {
format: '{bar} {percentage}% | {phase}'
});
}
/**
* 更新当前阶段的进度
*/
updatePhase(progress, file) {
if (this.currentPhase >= this.phases.length)
return;
const phase = this.phases[this.currentPhase];
this.phaseProgress = Math.min(progress, phase.weight);
// 计算总进度
const totalProgress = this.phases
.slice(0, this.currentPhase)
.reduce((sum, p) => sum + p.weight, 0) + this.phaseProgress;
this.progressBar.update(totalProgress, {
phase: phase.name,
file
});
}
/**
* 进入下一阶段
*/
nextPhase() {
if (this.currentPhase < this.phases.length - 1) {
this.currentPhase++;
this.phaseProgress = 0;
}
}
/**
* 完成所有阶段
*/
complete(message) {
this.progressBar.complete(message);
}
/**
* 显示错误
*/
error(message) {
this.progressBar.error(message);
}
/**
* 清空当前行
*/
clearLine() {
this.progressBar.clearLine();
}
/**
* 直接设置总体进度(用于恢复状态)
*/
setOverallProgress(overallProgress, currentPhase, phaseName) {
this.currentPhase = currentPhase;
this.progressBar.update(overallProgress, {
phase: phaseName || (this.phases[currentPhase]?.name || '')
});
}
}
exports.MultiPhaseProgress = MultiPhaseProgress;
//# sourceMappingURL=progress.js.map