python2ib
Version:
Convert Python code to IB Pseudocode format
199 lines • 5.32 kB
JavaScript
/**
* Progress tracking utilities for large file processing
*/
export class ProgressBar {
options;
current = 0;
startTime = Date.now();
lastUpdateTime = 0;
updateInterval = 100; // Update every 100ms
constructor(options) {
this.options = options;
this.options = {
label: 'Progress',
showPercentage: true,
showETA: true,
width: 40,
...options
};
}
/**
* Update progress
*/
update(current) {
this.current = Math.min(current, this.options.total);
const now = Date.now();
if (now - this.lastUpdateTime < this.updateInterval && current < this.options.total) {
return; // Throttle updates
}
this.lastUpdateTime = now;
this.render();
}
/**
* Increment progress by 1
*/
increment() {
this.update(this.current + 1);
}
/**
* Mark as complete
*/
complete() {
this.current = this.options.total;
this.render();
process.stdout.write('\n');
}
/**
* Render progress bar
*/
render() {
const { total, label, showPercentage, showETA, width } = this.options;
const percentage = Math.floor((this.current / total) * 100);
const filled = Math.floor((this.current / total) * width);
const empty = width - filled;
const bar = '█'.repeat(filled) + '░'.repeat(empty);
let output = `\r${label}: [${bar}]`;
if (showPercentage) {
output += ` ${percentage}%`;
}
output += ` (${this.current}/${total})`;
if (showETA && this.current > 0) {
const elapsed = Date.now() - this.startTime;
const rate = this.current / elapsed;
const remaining = (total - this.current) / rate;
if (remaining > 0 && remaining < Infinity) {
output += ` ETA: ${this.formatTime(remaining)}`;
}
}
process.stdout.write(output);
}
/**
* Format time in human readable format
*/
formatTime(ms) {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return `${hours}h ${minutes % 60}m`;
}
else if (minutes > 0) {
return `${minutes}m ${seconds % 60}s`;
}
else {
return `${seconds}s`;
}
}
}
/**
* Simple progress tracker for batch operations
*/
export class BatchProgress {
total;
onProgress;
completed = 0;
failed = 0;
progressBar;
constructor(total, onProgress) {
this.total = total;
this.onProgress = onProgress;
this.progressBar = new ProgressBar({
total,
label: 'Converting files',
showPercentage: true,
showETA: true
});
}
/**
* Mark one item as completed successfully
*/
success() {
this.completed++;
this.update();
}
/**
* Mark one item as failed
*/
failure() {
this.failed++;
this.update();
}
/**
* Update progress display
*/
update() {
const processed = this.completed + this.failed;
this.progressBar.update(processed);
if (this.onProgress) {
this.onProgress(this.completed, this.failed, this.total);
}
if (processed >= this.total) {
this.complete();
}
}
/**
* Complete the progress tracking
*/
complete() {
this.progressBar.complete();
// Print summary
// eslint-disable-next-line no-console
console.log(`\nCompleted: ${this.completed}/${this.total}`);
if (this.failed > 0) {
// eslint-disable-next-line no-console
console.log(`Failed: ${this.failed}/${this.total}`);
}
}
/**
* Get current statistics
*/
getStats() {
return {
completed: this.completed,
failed: this.failed,
total: this.total,
percentage: Math.floor(((this.completed + this.failed) / this.total) * 100)
};
}
}
/**
* Create a progress bar for file processing
*/
export function createFileProgress(fileCount, label = 'Processing files') {
return new ProgressBar({
total: fileCount,
label,
showPercentage: true,
showETA: true,
width: 30
});
}
/**
* Create a progress bar for line processing
*/
export function createLineProgress(lineCount, filename) {
const label = filename ? `Processing ${filename}` : 'Processing lines';
return new ProgressBar({
total: lineCount,
label,
showPercentage: true,
showETA: false,
width: 20
});
}
/**
* Utility to track progress of async operations
*/
export async function withProgress(items, processor, options) {
const progress = new ProgressBar({
total: items.length,
label: 'Processing',
...options
});
for (let i = 0; i < items.length; i++) {
await processor(items[i], i);
progress.update(i + 1);
}
progress.complete();
}
//# sourceMappingURL=progress.js.map