Skip to content

Commit

Permalink
Factor out the progress display utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Jan 2, 2025
1 parent b234a9a commit 039289f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
16 changes: 4 additions & 12 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require("path");
const fs = require("fs").promises;
const workerpool = require("workerpool");
const cliProgress = require("cli-progress");
const progressUtils = require("./progress-utils.js");

module.exports = async (
cachePath,
Expand All @@ -20,14 +20,7 @@ module.exports = async (
const flattenedChapters = chapterData.flatMap(arc => arc.chapters);

console.log("Converting raw downloaded HTML to EPUB chapters");
const progress = new cliProgress.SingleBar({
stopOnComplete: true,
clearOnComplete: true,
format: " {bar} {percentage}% | {time} | {value}/{total}"
}, cliProgress.Presets.shades_classic);

const start = performance.now();
progress.start(flattenedChapters.length, 0, { time: " " });
const progress = progressUtils.start(flattenedChapters.length);

const poolOptions = {};
if (concurrentJobs !== undefined) {
Expand All @@ -42,8 +35,7 @@ module.exports = async (

warnings.push(...await pool.exec("convertChapter", [chapter, bookData.title, inputPath, outputPath]));

const seconds = String(Math.round((performance.now() - start) / 1000)).padStart(3);
progress.increment({ time: `${seconds} s` });
progressUtils.increment(progress);
}));

pool.terminate();
Expand All @@ -52,7 +44,7 @@ module.exports = async (
console.warn(warning);
}

console.log(`All chapters converted in ${Math.round((performance.now() - start) / 100) / 10} seconds`);
console.log(`All chapters converted in ${progressUtils.getTotalSeconds(progress)} seconds`);
};

function getChapterData(arcs, manifest, chapterTitleStyle) {
Expand Down
15 changes: 4 additions & 11 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require("path");
const fs = require("fs").promises;
const { JSDOM } = require("jsdom");
const cliProgress = require("cli-progress");
const progressUtils = require("./progress-utils.js");

const FILENAME_PREFIX = "chapter";

Expand Down Expand Up @@ -58,13 +58,7 @@ async function downloadAllChapters(manifest, bookData, cachePath, manifestPath)

console.log(`Downloading ${toDownloadManifestIndicesByURL.size} remaining HTML pages`);

const progress = new cliProgress.SingleBar({
stopOnComplete: true,
clearOnComplete: true,
format: " {bar} {percentage}% | {time} | {value}/{total}"
}, cliProgress.Presets.shades_classic);
const start = performance.now();
progress.start(toDownloadManifestIndicesByURL.size, 0, { time: " " });
const progress = progressUtils.start(toDownloadManifestIndicesByURL.size);

const promises = [];
let manifestUpdatePromise = null;
Expand Down Expand Up @@ -93,13 +87,12 @@ async function downloadAllChapters(manifest, bookData, cachePath, manifestPath)
});
}

const seconds = String(Math.round((performance.now() - start) / 1000)).padStart(3);
progress.increment({ time: `${seconds} s` });
progressUtils.increment(progress);
})());
}

await Promise.all(promises);
console.log(`All HTML pages downloaded in ${Math.round((performance.now() - start) / 100) / 10} seconds`);
console.log(`All HTML pages downloaded in ${progressUtils.getTotalSeconds(progress)} seconds`);
}

function getChapterTitle(rawChapterDoc) {
Expand Down
28 changes: 28 additions & 0 deletions lib/progress-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";
const cliProgress = require("cli-progress");

// This file contains wrappers for cli-progress to fit the style we want to consistently use.

exports.start = total => {
const progress = new cliProgress.SingleBar({
stopOnComplete: true,
clearOnComplete: true,
format: " {bar} {percentage}% | {time} | {value}/{total}"
}, cliProgress.Presets.shades_classic);

const start = performance.now();
progress.start(total, 0, { time: " " });

progress.startTime = start;

return progress;
};

exports.increment = progress => {
const seconds = String(Math.round((performance.now() - progress.startTime) / 1000)).padStart(3);
progress.increment({ time: `${seconds} s` });
};

exports.getTotalSeconds = progress => {
return (Math.round((performance.now() - progress.startTime) / 100) / 10).toFixed(1);
};

0 comments on commit 039289f

Please sign in to comment.