Skip to content

Commit

Permalink
Reusable TUI progress code
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed May 4, 2024
1 parent dd12899 commit 178474f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 84 deletions.
41 changes: 13 additions & 28 deletions bin/download-flash-cn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {download} from '../util/download.mjs';
import {Hasher, Counter, Void} from '../util/stream.mjs';
import {Crc64} from '../util/crc64.mjs';
import {createPackageUrl} from '../util/ia.mjs';
import {Progress} from '../util/tui.mjs';

async function main() {
// eslint-disable-next-line no-process-env
Expand Down Expand Up @@ -105,35 +106,19 @@ async function main() {
};
};

{
const clear = process.stdout.isTTY
? '\x1B[F\x1B[2K'.repeat(resources.length)
: '';
const update = first => {
let output = first ? '' : clear;
for (const resource of resources) {
const {
info: {name},
progress,
hashes
} = resource;
const status = hashes
? 'DONE'
: `%${(progress * 100).toFixed(2)}`;
output += `${name}: ${status}\n`;
}
process.stdout.write(output);
};

update(true);
const interval = setInterval(update, 1000);
try {
await queue(resources, each, threads);
} finally {
clearInterval(interval);
const progress = new (class extends Progress {
line(resource) {
const status = resource.hashes
? 'DONE'
: `%${(resource.progress * 100).toFixed(2)}`;
return `${resource.info.name}: ${status}`;
}

update();
})(resources);
progress.start(1000);
try {
await queue(resources, each, threads);
} finally {
progress.end();
}

console.log('-'.repeat(80));
Expand Down
41 changes: 13 additions & 28 deletions bin/download-harman-air-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {download} from '../util/download.mjs';
import {Hasher, Counter, Void} from '../util/stream.mjs';
import {queue} from '../util/queue.mjs';
import {createPackageUrl} from '../util/ia.mjs';
import {Progress} from '../util/tui.mjs';

async function main() {
// eslint-disable-next-line no-process-env
Expand Down Expand Up @@ -91,35 +92,19 @@ async function main() {
};
};

{
const clear = process.stdout.isTTY
? '\x1B[F\x1B[2K'.repeat(resources.length)
: '';
const update = first => {
let output = first ? '' : clear;
for (const resource of resources) {
const {
info: {name},
progress,
hashes
} = resource;
const status = hashes
? 'DONE'
: `%${(progress * 100).toFixed(2)}`;
output += `${name}: ${status}\n`;
}
process.stdout.write(output);
};

update(true);
const interval = setInterval(update, 1000);
try {
await queue(resources, each, threads);
} finally {
clearInterval(interval);
const progress = new (class extends Progress {
line(resource) {
const status = resource.hashes
? 'DONE'
: `%${(resource.progress * 100).toFixed(2)}`;
return `${resource.info.name}: ${status}`;
}

update();
})(resources);
progress.start(1000);
try {
await queue(resources, each, threads);
} finally {
progress.end();
}

console.log('-'.repeat(80));
Expand Down
41 changes: 13 additions & 28 deletions bin/download-harman-air-sdk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {download} from '../util/download.mjs';
import {Hasher, Counter, Void} from '../util/stream.mjs';
import {queue} from '../util/queue.mjs';
import {createPackageUrl} from '../util/ia.mjs';
import {Progress} from '../util/tui.mjs';

async function main() {
// eslint-disable-next-line no-process-env
Expand Down Expand Up @@ -90,35 +91,19 @@ async function main() {
};
};

{
const clear = process.stdout.isTTY
? '\x1B[F\x1B[2K'.repeat(resources.length)
: '';
const update = first => {
let output = first ? '' : clear;
for (const resource of resources) {
const {
info: {name},
progress,
hashes
} = resource;
const status = hashes
? 'DONE'
: `%${(progress * 100).toFixed(2)}`;
output += `${name}: ${status}\n`;
}
process.stdout.write(output);
};

update(true);
const interval = setInterval(update, 1000);
try {
await queue(resources, each, threads);
} finally {
clearInterval(interval);
const progress = new (class extends Progress {
line(resource) {
const status = resource.hashes
? 'DONE'
: `%${(resource.progress * 100).toFixed(2)}`;
return `${resource.info.name}: ${status}`;
}

update();
})(resources);
progress.start(1000);
try {
await queue(resources, each, threads);
} finally {
progress.end();
}

console.log('-'.repeat(80));
Expand Down
53 changes: 53 additions & 0 deletions util/tui.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export class Progress {
constructor(items, stream = process.stdout) {
this.items = [...items];
this.stream = stream;
this.interval = null;
this._update = null;
}

// eslint-disable-next-line no-unused-vars
line(item) {
throw new Error('Must override in subclass');
}

start(interval) {
if (this.interval) {
throw new Error('Already started');
}

const {items, stream} = this;
const {isTTY} = stream;

let clear = '';
const update = (this._update = () => {
let output = clear;
for (const item of items) {
output += `${this.line(item)}\n`;
}
stream.write(output);
});

update();

clear = isTTY ? '\x1B[F\x1B[2K'.repeat(items.length) : '';
this.interval = setInterval(update, interval);
}

stop() {
const {interval} = this;
if (!interval) {
throw new Error('Not started');
}

clearInterval(interval);
this.interval = null;
this._update = null;
}

end() {
const {_update: update} = this;
this.stop();
update();
}
}

0 comments on commit 178474f

Please sign in to comment.