@acanthis-dec/cli 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -6
- package/dist/index.mjs +800 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -5,10 +5,11 @@ import fs from "node:fs/promises";
|
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import z from "zod";
|
|
8
|
-
import { cancel, confirm, intro, isCancel, log, multiselect, note, outro, password, select, spinner, text } from "@clack/prompts";
|
|
8
|
+
import { S_BAR, S_STEP_CANCEL, S_STEP_ERROR, S_STEP_SUBMIT, cancel, confirm, intro, isCI, isCancel, log, multiselect, note, outro, password, select, spinner, text, unicode, unicodeOr } from "@clack/prompts";
|
|
9
9
|
import { createRequire } from "module";
|
|
10
|
+
import { stripVTControlCharacters } from "node:util";
|
|
10
11
|
// package.json
|
|
11
|
-
var version = "0.
|
|
12
|
+
var version = "0.2.0";
|
|
12
13
|
// src/client.ts
|
|
13
14
|
var ApiClientError = class extends Error {
|
|
14
15
|
status;
|
|
@@ -5087,23 +5088,35 @@ async function runExport(client, options, callbacks) {
|
|
|
5087
5088
|
callbacks?.onWarning?.(message);
|
|
5088
5089
|
};
|
|
5089
5090
|
const novelId = parseNovelIdOrUrl(options.novelIdOrUrl);
|
|
5090
|
-
|
|
5091
|
-
|
|
5092
|
-
|
|
5093
|
-
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
|
|
5091
|
+
let novel;
|
|
5092
|
+
if (callbacks?.prefetchedNovel && callbacks.prefetchedNovel.id === novelId) novel = callbacks.prefetchedNovel;
|
|
5093
|
+
else {
|
|
5094
|
+
emitProgress({
|
|
5095
|
+
phase: "novel",
|
|
5096
|
+
current: 0,
|
|
5097
|
+
total: 1,
|
|
5098
|
+
message: `Fetching novel ${novelId}`
|
|
5099
|
+
});
|
|
5100
|
+
novel = await client.getNovel(options.source, novelId);
|
|
5101
|
+
}
|
|
5102
|
+
const selectedVolumes = parseVolumeSelection(options.volumes, novel.volumes);
|
|
5103
|
+
if (selectedVolumes.length === 0) throw new Error(`No volumes selected for novel "${novel.title}"`);
|
|
5104
|
+
const selectedAllVolumes = selectedVolumes.length === novel.volumes.length && novel.volumes.every((volume, index) => selectedVolumes[index]?.id === volume.id);
|
|
5105
|
+
const totalChapters = selectedVolumes.reduce((sum, volume) => sum + volume.chapters.length, 0);
|
|
5106
|
+
const volumePlans = selectedVolumes.map((volume, index) => ({
|
|
5107
|
+
index,
|
|
5108
|
+
id: volume.id,
|
|
5109
|
+
title: volume.title,
|
|
5110
|
+
chapterTotal: volume.chapters.length
|
|
5111
|
+
}));
|
|
5097
5112
|
emitProgress({
|
|
5098
5113
|
phase: "novel",
|
|
5099
5114
|
current: 1,
|
|
5100
5115
|
total: 1,
|
|
5101
|
-
|
|
5116
|
+
novelTitle: novel.title,
|
|
5117
|
+
volumes: volumePlans,
|
|
5118
|
+
message: `Loaded novel: ${novel.title} (${selectedVolumes.length} volume(s), ${totalChapters} chapter(s))`
|
|
5102
5119
|
});
|
|
5103
|
-
const selectedVolumes = parseVolumeSelection(options.volumes, novel.volumes);
|
|
5104
|
-
if (selectedVolumes.length === 0) throw new Error(`No volumes selected for novel "${novel.title}"`);
|
|
5105
|
-
const selectedAllVolumes = selectedVolumes.length === novel.volumes.length && novel.volumes.every((volume, index) => selectedVolumes[index]?.id === volume.id);
|
|
5106
|
-
const totalChapters = selectedVolumes.reduce((sum, volume) => sum + volume.chapters.length, 0);
|
|
5107
5120
|
const fetchedChaptersPerVolume = selectedVolumes.map(() => []);
|
|
5108
5121
|
const illustrationUrlsPerVolume = selectedVolumes.map(() => []);
|
|
5109
5122
|
let processedChapters = 0;
|
|
@@ -5112,20 +5125,45 @@ async function runExport(client, options, callbacks) {
|
|
|
5112
5125
|
const volumeChapters = fetchedChaptersPerVolume[vIdx];
|
|
5113
5126
|
const volumeIllustUrls = illustrationUrlsPerVolume[vIdx];
|
|
5114
5127
|
if (!volume || !volumeChapters || !volumeIllustUrls) continue;
|
|
5128
|
+
const volumeChapterTotal = volume.chapters.length;
|
|
5129
|
+
let volumeProcessedChapters = 0;
|
|
5115
5130
|
for (const chapter of volume.chapters) {
|
|
5116
|
-
processedChapters += 1;
|
|
5117
5131
|
if (isUnresolvedChapterId(chapter.id)) {
|
|
5118
5132
|
const msg = `Unresolved chapter "${chapter.title}" (${chapter.id}) in volume "${volume.title}"`;
|
|
5119
5133
|
if (options.strict) throw new Error(`Strict mode aborted: ${msg}`);
|
|
5134
|
+
processedChapters += 1;
|
|
5135
|
+
volumeProcessedChapters += 1;
|
|
5120
5136
|
emitWarning(`Skipped ${msg}`);
|
|
5121
5137
|
emitProgress({
|
|
5122
5138
|
phase: "chapters",
|
|
5123
5139
|
current: processedChapters,
|
|
5124
5140
|
total: totalChapters,
|
|
5125
|
-
|
|
5141
|
+
volumeIndex: vIdx,
|
|
5142
|
+
volumeCount: selectedVolumes.length,
|
|
5143
|
+
volumeId: volume.id,
|
|
5144
|
+
volumeTitle: volume.title,
|
|
5145
|
+
volumeCurrent: volumeProcessedChapters,
|
|
5146
|
+
volumeTotal: volumeChapterTotal,
|
|
5147
|
+
chapterTitle: chapter.title,
|
|
5148
|
+
chapterStatus: "skipped",
|
|
5149
|
+
message: `[${volume.title}] Skipped unresolved chapter: ${chapter.title}`
|
|
5126
5150
|
});
|
|
5127
5151
|
continue;
|
|
5128
5152
|
}
|
|
5153
|
+
emitProgress({
|
|
5154
|
+
phase: "chapters",
|
|
5155
|
+
current: processedChapters,
|
|
5156
|
+
total: totalChapters,
|
|
5157
|
+
volumeIndex: vIdx,
|
|
5158
|
+
volumeCount: selectedVolumes.length,
|
|
5159
|
+
volumeId: volume.id,
|
|
5160
|
+
volumeTitle: volume.title,
|
|
5161
|
+
volumeCurrent: volumeProcessedChapters,
|
|
5162
|
+
volumeTotal: volumeChapterTotal,
|
|
5163
|
+
chapterTitle: chapter.title,
|
|
5164
|
+
chapterStatus: "fetching",
|
|
5165
|
+
message: `[${volume.title}] Fetching (${volumeProcessedChapters + 1}/${volumeChapterTotal}): ${chapter.title}`
|
|
5166
|
+
});
|
|
5129
5167
|
try {
|
|
5130
5168
|
const content = await withExponentialBackoff(() => client.getChapterContent(options.source, chapter.id, novel.id));
|
|
5131
5169
|
volumeChapters.push({
|
|
@@ -5134,26 +5172,46 @@ async function runExport(client, options, callbacks) {
|
|
|
5134
5172
|
html: content.content
|
|
5135
5173
|
});
|
|
5136
5174
|
if (options.illustrations !== false) for (const imgUrl of extractImgSrcUrls(content.content)) volumeIllustUrls.push(imgUrl);
|
|
5175
|
+
processedChapters += 1;
|
|
5176
|
+
volumeProcessedChapters += 1;
|
|
5137
5177
|
emitProgress({
|
|
5138
5178
|
phase: "chapters",
|
|
5139
5179
|
current: processedChapters,
|
|
5140
5180
|
total: totalChapters,
|
|
5141
|
-
|
|
5181
|
+
volumeIndex: vIdx,
|
|
5182
|
+
volumeCount: selectedVolumes.length,
|
|
5183
|
+
volumeId: volume.id,
|
|
5184
|
+
volumeTitle: volume.title,
|
|
5185
|
+
volumeCurrent: volumeProcessedChapters,
|
|
5186
|
+
volumeTotal: volumeChapterTotal,
|
|
5187
|
+
chapterTitle: chapter.title,
|
|
5188
|
+
chapterStatus: "fetched",
|
|
5189
|
+
message: `[${volume.title}] Fetched (${volumeProcessedChapters}/${volumeChapterTotal}): ${chapter.title}`
|
|
5142
5190
|
});
|
|
5143
5191
|
} catch (error) {
|
|
5144
5192
|
if (isAuthError(error) || options.strict) throw error;
|
|
5193
|
+
processedChapters += 1;
|
|
5194
|
+
volumeProcessedChapters += 1;
|
|
5145
5195
|
emitWarning(`Skipped chapter "${chapter.title}" (${chapter.id}): ${formatErrorMessage(error)}`);
|
|
5146
5196
|
emitProgress({
|
|
5147
5197
|
phase: "chapters",
|
|
5148
5198
|
current: processedChapters,
|
|
5149
5199
|
total: totalChapters,
|
|
5150
|
-
|
|
5200
|
+
volumeIndex: vIdx,
|
|
5201
|
+
volumeCount: selectedVolumes.length,
|
|
5202
|
+
volumeId: volume.id,
|
|
5203
|
+
volumeTitle: volume.title,
|
|
5204
|
+
volumeCurrent: volumeProcessedChapters,
|
|
5205
|
+
volumeTotal: volumeChapterTotal,
|
|
5206
|
+
chapterTitle: chapter.title,
|
|
5207
|
+
chapterStatus: "failed",
|
|
5208
|
+
message: `[${volume.title}] Failed (${volumeProcessedChapters}/${volumeChapterTotal}): ${chapter.title}`
|
|
5151
5209
|
});
|
|
5152
5210
|
}
|
|
5153
5211
|
}
|
|
5154
5212
|
}
|
|
5155
5213
|
const mediaTaskMap = /* @__PURE__ */ new Map();
|
|
5156
|
-
const registerMediaTask = (rawUrl, role) => {
|
|
5214
|
+
const registerMediaTask = (rawUrl, role, volumeIndex) => {
|
|
5157
5215
|
const trimmed = rawUrl?.trim();
|
|
5158
5216
|
if (!trimmed) return;
|
|
5159
5217
|
let task = mediaTaskMap.get(trimmed);
|
|
@@ -5162,18 +5220,30 @@ async function runExport(client, options, callbacks) {
|
|
|
5162
5220
|
rawUrl: trimmed,
|
|
5163
5221
|
isNovelCover: false,
|
|
5164
5222
|
isVolumeCover: false,
|
|
5165
|
-
isIllustration: false
|
|
5223
|
+
isIllustration: false,
|
|
5224
|
+
volumeIndices: /* @__PURE__ */ new Set()
|
|
5166
5225
|
};
|
|
5167
5226
|
mediaTaskMap.set(trimmed, task);
|
|
5168
5227
|
}
|
|
5228
|
+
if (volumeIndex !== void 0) task.volumeIndices.add(volumeIndex);
|
|
5169
5229
|
if (role === "novelCover") task.isNovelCover = true;
|
|
5170
5230
|
else if (role === "volumeCover") task.isVolumeCover = true;
|
|
5171
5231
|
else task.isIllustration = true;
|
|
5172
5232
|
};
|
|
5173
5233
|
registerMediaTask(novel.coverUrl, "novelCover");
|
|
5174
|
-
for (
|
|
5175
|
-
|
|
5234
|
+
for (let vIdx = 0; vIdx < selectedVolumes.length; vIdx += 1) {
|
|
5235
|
+
const volume = selectedVolumes[vIdx];
|
|
5236
|
+
if (!volume) continue;
|
|
5237
|
+
registerMediaTask(volume.coverUrl, "volumeCover", vIdx);
|
|
5238
|
+
if (options.illustrations !== false) {
|
|
5239
|
+
const urls = illustrationUrlsPerVolume[vIdx];
|
|
5240
|
+
if (urls) for (const imgUrl of urls) registerMediaTask(imgUrl, "illustration", vIdx);
|
|
5241
|
+
}
|
|
5242
|
+
}
|
|
5176
5243
|
const mediaTasks = [...mediaTaskMap.values()];
|
|
5244
|
+
const volumeMediaTotals = selectedVolumes.map(() => 0);
|
|
5245
|
+
for (const task of mediaTasks) for (const idx of task.volumeIndices) volumeMediaTotals[idx] = (volumeMediaTotals[idx] ?? 0) + 1;
|
|
5246
|
+
const volumeMediaDone = selectedVolumes.map(() => 0);
|
|
5177
5247
|
const assetByRawUrl = /* @__PURE__ */ new Map();
|
|
5178
5248
|
let downloadedMediaCount = 0;
|
|
5179
5249
|
if (mediaTasks.length > 0) {
|
|
@@ -5181,7 +5251,8 @@ async function runExport(client, options, callbacks) {
|
|
|
5181
5251
|
phase: "images",
|
|
5182
5252
|
current: 0,
|
|
5183
5253
|
total: mediaTasks.length,
|
|
5184
|
-
|
|
5254
|
+
volumeMediaTotals,
|
|
5255
|
+
message: `Downloading ${mediaTasks.length} media asset(s)`
|
|
5185
5256
|
});
|
|
5186
5257
|
await withConcurrencyLimit(mediaTasks, MEDIA_DOWNLOAD_CONCURRENCY, async (task) => {
|
|
5187
5258
|
try {
|
|
@@ -5198,10 +5269,28 @@ async function runExport(client, options, callbacks) {
|
|
|
5198
5269
|
emitWarning(`Omitted media "${task.rawUrl}": ${formatErrorMessage(error)}`);
|
|
5199
5270
|
} finally {
|
|
5200
5271
|
downloadedMediaCount += 1;
|
|
5201
|
-
|
|
5272
|
+
const affected = [...task.volumeIndices];
|
|
5273
|
+
for (const idx of affected) volumeMediaDone[idx] = (volumeMediaDone[idx] ?? 0) + 1;
|
|
5274
|
+
const primaryVolIdx = affected[0];
|
|
5275
|
+
const primaryVol = primaryVolIdx !== void 0 ? selectedVolumes[primaryVolIdx] : void 0;
|
|
5276
|
+
if (primaryVol !== void 0 && primaryVolIdx !== void 0) emitProgress({
|
|
5202
5277
|
phase: "images",
|
|
5203
5278
|
current: downloadedMediaCount,
|
|
5204
5279
|
total: mediaTasks.length,
|
|
5280
|
+
volumeIndex: primaryVolIdx,
|
|
5281
|
+
volumeCount: selectedVolumes.length,
|
|
5282
|
+
volumeId: primaryVol.id,
|
|
5283
|
+
volumeTitle: primaryVol.title,
|
|
5284
|
+
volumeCurrent: volumeMediaDone[primaryVolIdx] ?? 0,
|
|
5285
|
+
volumeTotal: volumeMediaTotals[primaryVolIdx] ?? 0,
|
|
5286
|
+
affectedVolumeIndices: affected,
|
|
5287
|
+
message: `[${primaryVol.title}] Downloaded media (${downloadedMediaCount}/${mediaTasks.length})`
|
|
5288
|
+
});
|
|
5289
|
+
else emitProgress({
|
|
5290
|
+
phase: "images",
|
|
5291
|
+
current: downloadedMediaCount,
|
|
5292
|
+
total: mediaTasks.length,
|
|
5293
|
+
affectedVolumeIndices: affected,
|
|
5205
5294
|
message: `Downloaded media (${downloadedMediaCount}/${mediaTasks.length})`
|
|
5206
5295
|
});
|
|
5207
5296
|
}
|
|
@@ -5224,6 +5313,17 @@ async function runExport(client, options, callbacks) {
|
|
|
5224
5313
|
const displayTitle = `${novel.title} - ${volume.title}`;
|
|
5225
5314
|
if (!options.overwrite && await fileExists(targetPath)) {
|
|
5226
5315
|
emitWarning(`Skipped existing file: ${targetPath}`);
|
|
5316
|
+
emitProgress({
|
|
5317
|
+
phase: "write",
|
|
5318
|
+
current: vIdx + 1,
|
|
5319
|
+
total: totalSteps,
|
|
5320
|
+
volumeIndex: vIdx,
|
|
5321
|
+
volumeCount: totalSteps,
|
|
5322
|
+
volumeId: volume.id,
|
|
5323
|
+
volumeTitle: volume.title,
|
|
5324
|
+
skipped: true,
|
|
5325
|
+
message: `[${volume.title}] Skipped existing file`
|
|
5326
|
+
});
|
|
5227
5327
|
files.push({
|
|
5228
5328
|
path: targetPath,
|
|
5229
5329
|
title: displayTitle,
|
|
@@ -5237,7 +5337,11 @@ async function runExport(client, options, callbacks) {
|
|
|
5237
5337
|
phase: "pack",
|
|
5238
5338
|
current: vIdx + 1,
|
|
5239
5339
|
total: totalSteps,
|
|
5240
|
-
|
|
5340
|
+
volumeIndex: vIdx,
|
|
5341
|
+
volumeCount: totalSteps,
|
|
5342
|
+
volumeId: volume.id,
|
|
5343
|
+
volumeTitle: volume.title,
|
|
5344
|
+
message: `[${volume.title}] Packing EPUB`
|
|
5241
5345
|
});
|
|
5242
5346
|
const volumeCover = volume.coverUrl?.trim() ? assetByRawUrl.get(volume.coverUrl.trim()) : void 0;
|
|
5243
5347
|
const volumeIllustrations = [];
|
|
@@ -5263,7 +5367,11 @@ async function runExport(client, options, callbacks) {
|
|
|
5263
5367
|
phase: "write",
|
|
5264
5368
|
current: vIdx + 1,
|
|
5265
5369
|
total: totalSteps,
|
|
5266
|
-
|
|
5370
|
+
volumeIndex: vIdx,
|
|
5371
|
+
volumeCount: totalSteps,
|
|
5372
|
+
volumeId: volume.id,
|
|
5373
|
+
volumeTitle: volume.title,
|
|
5374
|
+
message: `[${volume.title}] Writing ${path.basename(targetPath)}`
|
|
5267
5375
|
});
|
|
5268
5376
|
await fs.writeFile(targetPath, epubBytes);
|
|
5269
5377
|
files.push({
|
|
@@ -5281,6 +5389,13 @@ async function runExport(client, options, callbacks) {
|
|
|
5281
5389
|
const totalChapterCount = fetchedChaptersPerVolume.reduce((sum, list) => sum + list.length, 0);
|
|
5282
5390
|
if (!options.overwrite && await fileExists(targetPath)) {
|
|
5283
5391
|
emitWarning(`Skipped existing file: ${targetPath}`);
|
|
5392
|
+
emitProgress({
|
|
5393
|
+
phase: "write",
|
|
5394
|
+
current: 1,
|
|
5395
|
+
total: 1,
|
|
5396
|
+
skipped: true,
|
|
5397
|
+
message: `Skipped existing file: ${path.basename(targetPath)}`
|
|
5398
|
+
});
|
|
5284
5399
|
files.push({
|
|
5285
5400
|
path: targetPath,
|
|
5286
5401
|
title: novel.title,
|
|
@@ -5350,6 +5465,635 @@ async function runExport(client, options, callbacks) {
|
|
|
5350
5465
|
warnings
|
|
5351
5466
|
};
|
|
5352
5467
|
}
|
|
5468
|
+
// src/progress.ts
|
|
5469
|
+
const ESC_CODE = 27;
|
|
5470
|
+
const CURSOR_HIDE = "\x1B[?25l";
|
|
5471
|
+
const CURSOR_SHOW = "\x1B[?25h";
|
|
5472
|
+
const RESET_STYLE = "\x1B[0m";
|
|
5473
|
+
const SPINNER_FRAMES = unicode ? [
|
|
5474
|
+
"◒",
|
|
5475
|
+
"◐",
|
|
5476
|
+
"◓",
|
|
5477
|
+
"◑"
|
|
5478
|
+
] : [
|
|
5479
|
+
"•",
|
|
5480
|
+
"o",
|
|
5481
|
+
"O",
|
|
5482
|
+
"0"
|
|
5483
|
+
];
|
|
5484
|
+
const SPINNER_DELAY_MS = unicode ? 80 : 120;
|
|
5485
|
+
const RENDER_THROTTLE_MS = 50;
|
|
5486
|
+
const BAR_FILLED = unicodeOr("█", "#");
|
|
5487
|
+
const BAR_EMPTY = unicodeOr("░", "-");
|
|
5488
|
+
const ELLIPSIS = unicodeOr("…", "...");
|
|
5489
|
+
const TRUNCATE_ELLIPSIS = unicodeOr("…", ".");
|
|
5490
|
+
const CHEVRON = unicodeOr("›", ">");
|
|
5491
|
+
const ICON_PENDING = unicodeOr("○", "o");
|
|
5492
|
+
const ICON_DONE = unicodeOr("✓", "+");
|
|
5493
|
+
const ICON_SKIPPED = unicodeOr("–", "-");
|
|
5494
|
+
/**
|
|
5495
|
+
* Returns the terminal display column width (0, 1, or 2) of a Unicode code point.
|
|
5496
|
+
*/
|
|
5497
|
+
function getCodePointWidth(cp) {
|
|
5498
|
+
if (cp <= 31 || cp >= 127 && cp <= 159) return 0;
|
|
5499
|
+
if (cp >= 8203 && cp <= 8207 || cp >= 65024 && cp <= 65039 || cp === 65279 || cp >= 917760 && cp <= 917999) return 0;
|
|
5500
|
+
if (cp >= 768 && cp <= 879 || cp >= 6832 && cp <= 6911 || cp >= 7616 && cp <= 7679 || cp >= 8400 && cp <= 8447 || cp >= 65056 && cp <= 65071) return 0;
|
|
5501
|
+
if (cp >= 4352 && cp <= 4447 || cp === 9001 || cp === 9002 || cp >= 11904 && cp <= 12350 || cp >= 12353 && cp <= 13311 || cp >= 13312 && cp <= 19903 || cp >= 19968 && cp <= 42191 || cp >= 43360 && cp <= 43391 || cp >= 44032 && cp <= 55203 || cp >= 63744 && cp <= 64255 || cp >= 65040 && cp <= 65049 || cp >= 65072 && cp <= 65135 || cp >= 65281 && cp <= 65376 || cp >= 65504 && cp <= 65510 || cp >= 127744 && cp <= 129791 || cp >= 131072 && cp <= 262143) return 2;
|
|
5502
|
+
return 1;
|
|
5503
|
+
}
|
|
5504
|
+
/**
|
|
5505
|
+
* Computes the terminal display column width of a string, ignoring ANSI escape sequences.
|
|
5506
|
+
*/
|
|
5507
|
+
function getDisplayWidth(text) {
|
|
5508
|
+
const plain = stripVTControlCharacters(text);
|
|
5509
|
+
let width = 0;
|
|
5510
|
+
for (const ch of plain) {
|
|
5511
|
+
const cp = ch.codePointAt(0);
|
|
5512
|
+
if (cp !== void 0) width += getCodePointWidth(cp);
|
|
5513
|
+
}
|
|
5514
|
+
return width;
|
|
5515
|
+
}
|
|
5516
|
+
/**
|
|
5517
|
+
* Truncates a plain text string so its display width fits within `maxWidth`.
|
|
5518
|
+
*/
|
|
5519
|
+
function truncatePlain(text, maxWidth) {
|
|
5520
|
+
if (maxWidth <= 0) return "";
|
|
5521
|
+
if (getDisplayWidth(text) <= maxWidth) return text;
|
|
5522
|
+
const ellipsisWidth = getDisplayWidth(TRUNCATE_ELLIPSIS);
|
|
5523
|
+
if (maxWidth <= ellipsisWidth) {
|
|
5524
|
+
let out = "";
|
|
5525
|
+
let width = 0;
|
|
5526
|
+
for (const ch of text) {
|
|
5527
|
+
const cp = ch.codePointAt(0);
|
|
5528
|
+
if (cp === void 0) continue;
|
|
5529
|
+
const w = getCodePointWidth(cp);
|
|
5530
|
+
if (width + w > maxWidth) break;
|
|
5531
|
+
out += ch;
|
|
5532
|
+
width += w;
|
|
5533
|
+
}
|
|
5534
|
+
return out;
|
|
5535
|
+
}
|
|
5536
|
+
const budget = maxWidth - ellipsisWidth;
|
|
5537
|
+
let out = "";
|
|
5538
|
+
let width = 0;
|
|
5539
|
+
for (const ch of text) {
|
|
5540
|
+
const cp = ch.codePointAt(0);
|
|
5541
|
+
if (cp === void 0) continue;
|
|
5542
|
+
const w = getCodePointWidth(cp);
|
|
5543
|
+
if (width + w > budget) break;
|
|
5544
|
+
out += ch;
|
|
5545
|
+
width += w;
|
|
5546
|
+
}
|
|
5547
|
+
return `${out}${TRUNCATE_ELLIPSIS}`;
|
|
5548
|
+
}
|
|
5549
|
+
/**
|
|
5550
|
+
* Truncates and right-pads a plain string with spaces to match `targetWidth` columns.
|
|
5551
|
+
*/
|
|
5552
|
+
function padRightPlain(text, targetWidth) {
|
|
5553
|
+
if (targetWidth <= 0) return "";
|
|
5554
|
+
const truncated = truncatePlain(text, targetWidth);
|
|
5555
|
+
const currentWidth = getDisplayWidth(truncated);
|
|
5556
|
+
if (currentWidth >= targetWidth) return truncated;
|
|
5557
|
+
return `${truncated}${" ".repeat(targetWidth - currentWidth)}`;
|
|
5558
|
+
}
|
|
5559
|
+
/**
|
|
5560
|
+
* Truncates an ANSI-styled line to `maxWidth` display columns while preserving CSI sequences.
|
|
5561
|
+
*/
|
|
5562
|
+
function truncateAnsi(line, maxWidth) {
|
|
5563
|
+
if (maxWidth <= 0) return "";
|
|
5564
|
+
let out = "";
|
|
5565
|
+
let visibleWidth = 0;
|
|
5566
|
+
let hasAnsi = false;
|
|
5567
|
+
let truncated = false;
|
|
5568
|
+
let i = 0;
|
|
5569
|
+
while (i < line.length) {
|
|
5570
|
+
if (line.charCodeAt(i) === ESC_CODE) {
|
|
5571
|
+
if (line[i + 1] === "[") {
|
|
5572
|
+
let j = i + 2;
|
|
5573
|
+
while (j < line.length) {
|
|
5574
|
+
const code = line.charCodeAt(j);
|
|
5575
|
+
if (code >= 64 && code <= 126) {
|
|
5576
|
+
j += 1;
|
|
5577
|
+
break;
|
|
5578
|
+
}
|
|
5579
|
+
j += 1;
|
|
5580
|
+
}
|
|
5581
|
+
out += line.slice(i, j);
|
|
5582
|
+
hasAnsi = true;
|
|
5583
|
+
i = j;
|
|
5584
|
+
continue;
|
|
5585
|
+
}
|
|
5586
|
+
i += 1;
|
|
5587
|
+
continue;
|
|
5588
|
+
}
|
|
5589
|
+
const cp = line.codePointAt(i);
|
|
5590
|
+
if (cp === void 0) break;
|
|
5591
|
+
const charLen = cp > 65535 ? 2 : 1;
|
|
5592
|
+
const w = getCodePointWidth(cp);
|
|
5593
|
+
if (visibleWidth + w > maxWidth) {
|
|
5594
|
+
truncated = true;
|
|
5595
|
+
break;
|
|
5596
|
+
}
|
|
5597
|
+
out += line.slice(i, i + charLen);
|
|
5598
|
+
visibleWidth += w;
|
|
5599
|
+
i += charLen;
|
|
5600
|
+
}
|
|
5601
|
+
if (truncated && hasAnsi) return `${out}${RESET_STYLE}`;
|
|
5602
|
+
return out;
|
|
5603
|
+
}
|
|
5604
|
+
/**
|
|
5605
|
+
* Formats an `ExportProgressEvent` as a single plain-text log line for non-TTY or JSON runs.
|
|
5606
|
+
* Returns `undefined` for transient `fetching` events to avoid flooding logs.
|
|
5607
|
+
*/
|
|
5608
|
+
function formatProgressLogLine(event) {
|
|
5609
|
+
if (event.phase === "chapters") {
|
|
5610
|
+
if (event.chapterStatus === "fetching") return;
|
|
5611
|
+
const statusLabel = event.chapterStatus === "skipped" ? "Skipped" : event.chapterStatus === "failed" ? "Failed" : "Fetched";
|
|
5612
|
+
const volPrefix = event.volumeTitle ? `[${event.volumeTitle}] (${event.volumeCurrent ?? 0}/${event.volumeTotal ?? 0}) ` : "";
|
|
5613
|
+
return `[chapters] (${event.current}/${event.total}) ${volPrefix}${statusLabel}: ${event.chapterTitle ?? event.message}`;
|
|
5614
|
+
}
|
|
5615
|
+
if (event.phase === "images") {
|
|
5616
|
+
const volPrefix = event.volumeTitle ? `[${event.volumeTitle}] ` : "";
|
|
5617
|
+
const detail = event.current === 0 ? event.message : "Downloaded media";
|
|
5618
|
+
return `[images] (${event.current}/${event.total}) ${volPrefix}${detail}`;
|
|
5619
|
+
}
|
|
5620
|
+
if (event.phase === "pack" || event.phase === "write") return `[${event.phase}] (${event.current}/${event.total}) ${event.message}`;
|
|
5621
|
+
return `[novel] ${event.message}`;
|
|
5622
|
+
}
|
|
5623
|
+
function formatElapsed(startTimeMs) {
|
|
5624
|
+
const seconds = Math.max(0, Math.floor((performance.now() - startTimeMs) / 1e3));
|
|
5625
|
+
const mins = Math.floor(seconds / 60);
|
|
5626
|
+
const secs = seconds % 60;
|
|
5627
|
+
return mins > 0 ? `[${mins}m ${secs}s]` : `[${secs}s]`;
|
|
5628
|
+
}
|
|
5629
|
+
function renderProgressBar(ratio, width, color) {
|
|
5630
|
+
if (width <= 0) return "";
|
|
5631
|
+
const filled = Math.round(Math.min(1, Math.max(0, ratio)) * width);
|
|
5632
|
+
const empty = Math.max(0, width - filled);
|
|
5633
|
+
const filledStr = BAR_FILLED.repeat(filled);
|
|
5634
|
+
const emptyStr = BAR_EMPTY.repeat(empty);
|
|
5635
|
+
return `${color === "green" ? pc.green(filledStr) : color === "yellow" ? pc.yellow(filledStr) : pc.cyan(filledStr)}${pc.dim(emptyStr)}`;
|
|
5636
|
+
}
|
|
5637
|
+
function stripVolumePrefix(message, volumeTitle) {
|
|
5638
|
+
if (!volumeTitle) return message;
|
|
5639
|
+
const prefix = `[${volumeTitle}] `;
|
|
5640
|
+
return message.startsWith(prefix) ? message.slice(prefix.length) : message;
|
|
5641
|
+
}
|
|
5642
|
+
function getVolumeIcon(status, spinnerFrame) {
|
|
5643
|
+
switch (status) {
|
|
5644
|
+
case "active": return pc.cyan(spinnerFrame);
|
|
5645
|
+
case "done": return pc.green(ICON_DONE);
|
|
5646
|
+
case "doneWithIssues": return pc.yellow(ICON_DONE);
|
|
5647
|
+
case "skipped": return pc.yellow(ICON_SKIPPED);
|
|
5648
|
+
default: return pc.dim(ICON_PENDING);
|
|
5649
|
+
}
|
|
5650
|
+
}
|
|
5651
|
+
/**
|
|
5652
|
+
* Creates a real-time multi-bar terminal progress renderer displaying both overall
|
|
5653
|
+
* and per-volume progress, active volume/chapter context, and safe TTY cleanup.
|
|
5654
|
+
*/
|
|
5655
|
+
function createExportProgressRenderer(options) {
|
|
5656
|
+
const output = options.output ?? process.stdout;
|
|
5657
|
+
const withGuide = options.withGuide ?? false;
|
|
5658
|
+
const isTty = Boolean(output.isTTY) && !isCI();
|
|
5659
|
+
const weightChapters = options.illustrations ? .7 : .85;
|
|
5660
|
+
const weightImages = options.illustrations ? .25 : .1;
|
|
5661
|
+
const weightFinalize = .05;
|
|
5662
|
+
let novelTitle = options.initialNovelTitle ?? "";
|
|
5663
|
+
let statusMessage = "Starting EPUB export...";
|
|
5664
|
+
let currentPhase = "novel";
|
|
5665
|
+
let phaseCurrent = 0;
|
|
5666
|
+
let phaseTotal = 1;
|
|
5667
|
+
let overallRatio = 0;
|
|
5668
|
+
let volumes = [];
|
|
5669
|
+
let maxVolumeTitleWidth = 0;
|
|
5670
|
+
let activeVolumeIndex;
|
|
5671
|
+
let activeVolumeTitle;
|
|
5672
|
+
let activeDetail;
|
|
5673
|
+
let started = false;
|
|
5674
|
+
let finished = false;
|
|
5675
|
+
let cursorHidden = false;
|
|
5676
|
+
let startTime = performance.now();
|
|
5677
|
+
let frameIndex = 0;
|
|
5678
|
+
let lastRenderedLines = 0;
|
|
5679
|
+
let lastRenderAt = 0;
|
|
5680
|
+
let dirty = false;
|
|
5681
|
+
let timer;
|
|
5682
|
+
const guidePrefix = withGuide ? `${pc.gray(S_BAR)} ` : " ";
|
|
5683
|
+
const guideSpacer = withGuide ? pc.gray(S_BAR) : "";
|
|
5684
|
+
const isVolumeCompleteInPhase = (vol) => {
|
|
5685
|
+
if (currentPhase === "novel" || currentPhase === "chapters") {
|
|
5686
|
+
if (vol.chapterTotal === 0) return activeVolumeIndex !== void 0 && vol.index < activeVolumeIndex;
|
|
5687
|
+
return vol.chaptersDone >= vol.chapterTotal;
|
|
5688
|
+
}
|
|
5689
|
+
if (currentPhase === "images") return vol.mediaTotal === 0 || vol.mediaDone >= vol.mediaTotal;
|
|
5690
|
+
return vol.packState === "written" || vol.packState === "skipped";
|
|
5691
|
+
};
|
|
5692
|
+
const computeOverallRatio = (event) => {
|
|
5693
|
+
if (event.phase === "novel") return 0;
|
|
5694
|
+
if (event.phase === "chapters") return (event.total > 0 ? event.current / event.total : 1) * weightChapters;
|
|
5695
|
+
if (event.phase === "images") {
|
|
5696
|
+
const frac = event.total > 0 ? event.current / event.total : 1;
|
|
5697
|
+
return weightChapters + frac * weightImages;
|
|
5698
|
+
}
|
|
5699
|
+
if (options.mode === "split" && volumes.length > 0) {
|
|
5700
|
+
let finalizeSum = 0;
|
|
5701
|
+
for (const vol of volumes) if (vol.packState === "packing") finalizeSum += .5;
|
|
5702
|
+
else if (vol.packState === "writing") finalizeSum += .85;
|
|
5703
|
+
else if (vol.packState === "written" || vol.packState === "skipped") finalizeSum += 1;
|
|
5704
|
+
return weightChapters + weightImages + finalizeSum / volumes.length * weightFinalize;
|
|
5705
|
+
}
|
|
5706
|
+
const mergedStep = event.phase === "pack" ? .5 : 1;
|
|
5707
|
+
return weightChapters + weightImages + mergedStep * weightFinalize;
|
|
5708
|
+
};
|
|
5709
|
+
const formatPhaseSummary = () => {
|
|
5710
|
+
switch (currentPhase) {
|
|
5711
|
+
case "novel": return "Preparing";
|
|
5712
|
+
case "chapters": return `Chapters ${phaseCurrent}/${phaseTotal}`;
|
|
5713
|
+
case "images": return `Images ${phaseCurrent}/${phaseTotal}`;
|
|
5714
|
+
case "pack": return `Packing ${phaseCurrent}/${phaseTotal}`;
|
|
5715
|
+
case "write": return `Writing ${phaseCurrent}/${phaseTotal}`;
|
|
5716
|
+
}
|
|
5717
|
+
};
|
|
5718
|
+
const formatActiveLine = (budget) => {
|
|
5719
|
+
if (activeVolumeTitle && activeDetail) {
|
|
5720
|
+
const sepWidth = getDisplayWidth(` ${CHEVRON} `);
|
|
5721
|
+
const innerBudget = Math.max(4, budget - sepWidth);
|
|
5722
|
+
const volRaw = getDisplayWidth(activeVolumeTitle);
|
|
5723
|
+
const detailRaw = getDisplayWidth(activeDetail);
|
|
5724
|
+
let volLimit;
|
|
5725
|
+
let detailLimit;
|
|
5726
|
+
if (volRaw + detailRaw <= innerBudget) {
|
|
5727
|
+
volLimit = volRaw;
|
|
5728
|
+
detailLimit = detailRaw;
|
|
5729
|
+
} else {
|
|
5730
|
+
const baseVol = Math.floor(innerBudget * .45);
|
|
5731
|
+
const baseDetail = innerBudget - baseVol;
|
|
5732
|
+
if (volRaw < baseVol) {
|
|
5733
|
+
volLimit = volRaw;
|
|
5734
|
+
detailLimit = innerBudget - volLimit;
|
|
5735
|
+
} else if (detailRaw < baseDetail) {
|
|
5736
|
+
detailLimit = detailRaw;
|
|
5737
|
+
volLimit = innerBudget - detailLimit;
|
|
5738
|
+
} else {
|
|
5739
|
+
volLimit = baseVol;
|
|
5740
|
+
detailLimit = baseDetail;
|
|
5741
|
+
}
|
|
5742
|
+
}
|
|
5743
|
+
const volPart = truncatePlain(activeVolumeTitle, volLimit);
|
|
5744
|
+
const detailPart = truncatePlain(activeDetail, detailLimit);
|
|
5745
|
+
return `${pc.cyan(volPart)} ${pc.dim(CHEVRON)} ${detailPart}`;
|
|
5746
|
+
}
|
|
5747
|
+
if (activeVolumeTitle) return pc.cyan(truncatePlain(activeVolumeTitle, budget));
|
|
5748
|
+
if (activeDetail) return truncatePlain(activeDetail, budget);
|
|
5749
|
+
return pc.dim(truncatePlain(statusMessage, budget));
|
|
5750
|
+
};
|
|
5751
|
+
const buildVolumeRow = (vol, spinnerFrame, columns, contentBudget) => {
|
|
5752
|
+
let ratio = 0;
|
|
5753
|
+
let status = "pending";
|
|
5754
|
+
let detailStyled = "";
|
|
5755
|
+
if (currentPhase === "novel" || currentPhase === "chapters") {
|
|
5756
|
+
if (vol.chapterTotal === 0) {
|
|
5757
|
+
const passed = activeVolumeIndex !== void 0 && vol.index < activeVolumeIndex;
|
|
5758
|
+
ratio = passed ? 1 : 0;
|
|
5759
|
+
status = passed ? vol.chaptersIssues > 0 ? "doneWithIssues" : "done" : vol.index === activeVolumeIndex ? "active" : "pending";
|
|
5760
|
+
} else {
|
|
5761
|
+
ratio = vol.chaptersDone / vol.chapterTotal;
|
|
5762
|
+
if (vol.chaptersDone >= vol.chapterTotal) status = vol.chaptersIssues > 0 ? "doneWithIssues" : "done";
|
|
5763
|
+
else if (vol.index === activeVolumeIndex || vol.chaptersDone > 0) status = "active";
|
|
5764
|
+
}
|
|
5765
|
+
const countDigits = String(vol.chapterTotal).length;
|
|
5766
|
+
const doneStr = String(vol.chaptersDone).padStart(countDigits, " ");
|
|
5767
|
+
const issueSuffix = vol.chaptersIssues > 0 ? ` ${pc.yellow(`(!${vol.chaptersIssues})`)}` : "";
|
|
5768
|
+
detailStyled = `${pc.dim(`${doneStr}/${vol.chapterTotal} ch`)}${issueSuffix}`;
|
|
5769
|
+
} else if (currentPhase === "images") {
|
|
5770
|
+
ratio = vol.mediaTotal > 0 ? vol.mediaDone / vol.mediaTotal : 1;
|
|
5771
|
+
if (vol.mediaTotal === 0 || vol.mediaDone >= vol.mediaTotal) status = vol.chaptersIssues > 0 ? "doneWithIssues" : "done";
|
|
5772
|
+
else if (vol.index === activeVolumeIndex || vol.mediaDone > 0) status = "active";
|
|
5773
|
+
if (vol.mediaTotal > 0) {
|
|
5774
|
+
const countDigits = String(vol.mediaTotal).length;
|
|
5775
|
+
const doneStr = String(vol.mediaDone).padStart(countDigits, " ");
|
|
5776
|
+
detailStyled = pc.dim(`${doneStr}/${vol.mediaTotal} img`);
|
|
5777
|
+
} else detailStyled = pc.dim("0 img");
|
|
5778
|
+
} else switch (vol.packState) {
|
|
5779
|
+
case "packing":
|
|
5780
|
+
ratio = .5;
|
|
5781
|
+
status = "active";
|
|
5782
|
+
detailStyled = pc.cyan("packing");
|
|
5783
|
+
break;
|
|
5784
|
+
case "writing":
|
|
5785
|
+
ratio = .85;
|
|
5786
|
+
status = "active";
|
|
5787
|
+
detailStyled = pc.cyan("writing");
|
|
5788
|
+
break;
|
|
5789
|
+
case "written":
|
|
5790
|
+
ratio = 1;
|
|
5791
|
+
status = vol.chaptersIssues > 0 ? "doneWithIssues" : "done";
|
|
5792
|
+
detailStyled = pc.green("written");
|
|
5793
|
+
break;
|
|
5794
|
+
case "skipped":
|
|
5795
|
+
ratio = 1;
|
|
5796
|
+
status = "skipped";
|
|
5797
|
+
detailStyled = pc.yellow("skipped");
|
|
5798
|
+
break;
|
|
5799
|
+
default:
|
|
5800
|
+
ratio = 0;
|
|
5801
|
+
status = "pending";
|
|
5802
|
+
detailStyled = pc.dim("pending");
|
|
5803
|
+
}
|
|
5804
|
+
const icon = getVolumeIcon(status, spinnerFrame);
|
|
5805
|
+
const pctStr = `${String(Math.round(Math.min(1, Math.max(0, ratio)) * 100)).padStart(3, " ")}%`;
|
|
5806
|
+
const detailWidth = getDisplayWidth(detailStyled);
|
|
5807
|
+
if (columns < 52) {
|
|
5808
|
+
const titleWidth = Math.max(6, contentBudget - 2 - 2 - 4 - 2 - detailWidth);
|
|
5809
|
+
const paddedTitle = padRightPlain(vol.title, titleWidth);
|
|
5810
|
+
return `${guidePrefix}${icon} ${paddedTitle} ${pctStr} ${detailStyled}`;
|
|
5811
|
+
}
|
|
5812
|
+
let barWidth = Math.min(18, Math.max(8, Math.floor(contentBudget * .22)));
|
|
5813
|
+
let titleWidth = Math.min(Math.max(maxVolumeTitleWidth, 10), Math.max(10, Math.floor(contentBudget * .36)));
|
|
5814
|
+
const fixedOverhead = 11 + detailWidth;
|
|
5815
|
+
const availableForTitleAndBar = Math.max(14, contentBudget - fixedOverhead);
|
|
5816
|
+
if (titleWidth + barWidth > availableForTitleAndBar) {
|
|
5817
|
+
titleWidth = Math.max(8, availableForTitleAndBar - barWidth);
|
|
5818
|
+
if (titleWidth + barWidth > availableForTitleAndBar) barWidth = Math.max(6, availableForTitleAndBar - titleWidth);
|
|
5819
|
+
}
|
|
5820
|
+
const paddedTitle = padRightPlain(vol.title, titleWidth);
|
|
5821
|
+
const bar = renderProgressBar(ratio, barWidth, status === "done" ? "green" : status === "doneWithIssues" || status === "skipped" ? "yellow" : "cyan");
|
|
5822
|
+
return `${guidePrefix}${icon} ${paddedTitle} ${bar} ${pctStr} ${detailStyled}`;
|
|
5823
|
+
};
|
|
5824
|
+
const buildFrame = () => {
|
|
5825
|
+
const columns = Math.max(20, output.columns || 80);
|
|
5826
|
+
const rows = Math.max(6, output.rows || 24);
|
|
5827
|
+
const maxLineWidth = Math.max(19, columns - 1);
|
|
5828
|
+
const spinnerFrame = SPINNER_FRAMES[frameIndex % SPINNER_FRAMES.length] ?? SPINNER_FRAMES[0] ?? "*";
|
|
5829
|
+
const elapsed = formatElapsed(startTime);
|
|
5830
|
+
if (volumes.length === 0) return [truncateAnsi(`${pc.magenta(spinnerFrame)} ${statusMessage} ${pc.dim(elapsed)}`, maxLineWidth)];
|
|
5831
|
+
const lines = [];
|
|
5832
|
+
const headerPrefix = `${pc.magenta(spinnerFrame)} Exporting `;
|
|
5833
|
+
const headerSuffix = ` ${pc.dim(elapsed)}`;
|
|
5834
|
+
const headerBudget = Math.max(8, maxLineWidth - getDisplayWidth(headerPrefix) - getDisplayWidth(headerSuffix));
|
|
5835
|
+
const shownNovelTitle = truncatePlain(novelTitle || "novel", headerBudget);
|
|
5836
|
+
lines.push(`${headerPrefix}${pc.bold(shownNovelTitle)}${headerSuffix}`);
|
|
5837
|
+
const contentBudget = Math.max(16, maxLineWidth - 3);
|
|
5838
|
+
const pctStr = `${String(Math.round(overallRatio * 100)).padStart(3, " ")}%`;
|
|
5839
|
+
const phaseSummary = formatPhaseSummary();
|
|
5840
|
+
const overallBarBudget = contentBudget - (16 + getDisplayWidth(phaseSummary));
|
|
5841
|
+
if (overallBarBudget >= 8) {
|
|
5842
|
+
const overallBar = renderProgressBar(overallRatio, Math.min(28, overallBarBudget), "cyan");
|
|
5843
|
+
lines.push(`${guidePrefix}${pc.bold("Overall")} ${overallBar} ${pctStr} ${pc.dim(phaseSummary)}`);
|
|
5844
|
+
} else if (contentBudget >= 24) {
|
|
5845
|
+
const barWidth = Math.max(6, Math.min(16, contentBudget - 16));
|
|
5846
|
+
const overallBar = renderProgressBar(overallRatio, barWidth, "cyan");
|
|
5847
|
+
lines.push(`${guidePrefix}${pc.bold("Overall")} ${overallBar} ${pctStr}`);
|
|
5848
|
+
} else lines.push(`${guidePrefix}${pc.bold("Overall")} ${pctStr}`);
|
|
5849
|
+
const activeBudget = Math.max(8, contentBudget - 9);
|
|
5850
|
+
lines.push(`${guidePrefix}${pc.bold("Active ")} ${formatActiveLine(activeBudget)}`);
|
|
5851
|
+
if (rows >= 12) lines.push(guideSpacer);
|
|
5852
|
+
const availableVolRows = Math.max(1, rows - 2 - lines.length);
|
|
5853
|
+
const desiredMaxVolumes = Math.max(3, Math.min(10, rows - 10));
|
|
5854
|
+
const totalVolumes = volumes.length;
|
|
5855
|
+
let start = 0;
|
|
5856
|
+
let end = totalVolumes;
|
|
5857
|
+
if (totalVolumes > availableVolRows || totalVolumes > desiredMaxVolumes) {
|
|
5858
|
+
const windowCap = Math.max(1, Math.min(desiredMaxVolumes, Math.max(1, availableVolRows - 2)));
|
|
5859
|
+
if (totalVolumes > windowCap) {
|
|
5860
|
+
const focus = Math.max(0, Math.min(totalVolumes - 1, activeVolumeIndex ?? 0));
|
|
5861
|
+
const leadContext = Math.min(2, Math.floor((windowCap - 1) / 3));
|
|
5862
|
+
start = Math.max(0, focus - leadContext);
|
|
5863
|
+
if (start + windowCap > totalVolumes) start = Math.max(0, totalVolumes - windowCap);
|
|
5864
|
+
end = Math.min(totalVolumes, start + windowCap);
|
|
5865
|
+
}
|
|
5866
|
+
}
|
|
5867
|
+
if (start > 0) {
|
|
5868
|
+
let doneBefore = 0;
|
|
5869
|
+
for (let i = 0; i < start; i += 1) {
|
|
5870
|
+
const vol = volumes[i];
|
|
5871
|
+
if (vol && isVolumeCompleteInPhase(vol)) doneBefore += 1;
|
|
5872
|
+
}
|
|
5873
|
+
lines.push(`${guidePrefix}${pc.dim(`${ELLIPSIS} ${start} earlier volume(s) (${doneBefore}/${start} done)`)}`);
|
|
5874
|
+
}
|
|
5875
|
+
for (let i = start; i < end; i += 1) {
|
|
5876
|
+
const vol = volumes[i];
|
|
5877
|
+
if (!vol) continue;
|
|
5878
|
+
lines.push(buildVolumeRow(vol, spinnerFrame, columns, contentBudget));
|
|
5879
|
+
}
|
|
5880
|
+
if (end < totalVolumes) lines.push(`${guidePrefix}${pc.dim(`${ELLIPSIS} ${totalVolumes - end} more volume(s)`)}`);
|
|
5881
|
+
return lines.map((line) => truncateAnsi(line, maxLineWidth));
|
|
5882
|
+
};
|
|
5883
|
+
const renderFrame = () => {
|
|
5884
|
+
if (!isTty || finished) return;
|
|
5885
|
+
lastRenderAt = performance.now();
|
|
5886
|
+
dirty = false;
|
|
5887
|
+
const lines = buildFrame();
|
|
5888
|
+
let buffer = "";
|
|
5889
|
+
if (lastRenderedLines > 1) buffer += `\x1b[${lastRenderedLines - 1}A`;
|
|
5890
|
+
if (lastRenderedLines > 0) buffer += "\r\x1B[0J";
|
|
5891
|
+
buffer += lines.join("\n");
|
|
5892
|
+
output.write(buffer);
|
|
5893
|
+
lastRenderedLines = lines.length;
|
|
5894
|
+
};
|
|
5895
|
+
const clearRenderedBlock = () => {
|
|
5896
|
+
if (lastRenderedLines > 1) output.write(`\x1b[${lastRenderedLines - 1}A\r\x1b[0J`);
|
|
5897
|
+
else if (lastRenderedLines === 1) output.write("\r\x1B[0J");
|
|
5898
|
+
lastRenderedLines = 0;
|
|
5899
|
+
};
|
|
5900
|
+
const restoreCursorIfHidden = () => {
|
|
5901
|
+
if (cursorHidden) {
|
|
5902
|
+
output.write(CURSOR_SHOW);
|
|
5903
|
+
cursorHidden = false;
|
|
5904
|
+
}
|
|
5905
|
+
};
|
|
5906
|
+
let onResize;
|
|
5907
|
+
let onExit;
|
|
5908
|
+
let onUncaught;
|
|
5909
|
+
let onSigint;
|
|
5910
|
+
let onSigterm;
|
|
5911
|
+
const cleanupListeners = () => {
|
|
5912
|
+
if (onResize) {
|
|
5913
|
+
output.off("resize", onResize);
|
|
5914
|
+
onResize = void 0;
|
|
5915
|
+
}
|
|
5916
|
+
if (onExit) {
|
|
5917
|
+
process.off("exit", onExit);
|
|
5918
|
+
onExit = void 0;
|
|
5919
|
+
}
|
|
5920
|
+
if (onUncaught) {
|
|
5921
|
+
process.off("uncaughtExceptionMonitor", onUncaught);
|
|
5922
|
+
onUncaught = void 0;
|
|
5923
|
+
}
|
|
5924
|
+
if (onSigint) {
|
|
5925
|
+
process.off("SIGINT", onSigint);
|
|
5926
|
+
onSigint = void 0;
|
|
5927
|
+
}
|
|
5928
|
+
if (onSigterm) {
|
|
5929
|
+
process.off("SIGTERM", onSigterm);
|
|
5930
|
+
onSigterm = void 0;
|
|
5931
|
+
}
|
|
5932
|
+
};
|
|
5933
|
+
const stopTimer = () => {
|
|
5934
|
+
if (timer !== void 0) {
|
|
5935
|
+
clearInterval(timer);
|
|
5936
|
+
timer = void 0;
|
|
5937
|
+
}
|
|
5938
|
+
};
|
|
5939
|
+
const finish = (message, kind) => {
|
|
5940
|
+
if (finished) return;
|
|
5941
|
+
finished = true;
|
|
5942
|
+
stopTimer();
|
|
5943
|
+
cleanupListeners();
|
|
5944
|
+
if (!isTty) {
|
|
5945
|
+
if (message) output.write(`${message}\n`);
|
|
5946
|
+
return;
|
|
5947
|
+
}
|
|
5948
|
+
clearRenderedBlock();
|
|
5949
|
+
const icon = kind === "submit" ? pc.green(S_STEP_SUBMIT) : kind === "error" ? pc.red(S_STEP_ERROR) : pc.red(S_STEP_CANCEL);
|
|
5950
|
+
const elapsed = formatElapsed(startTime);
|
|
5951
|
+
output.write(`${icon} ${message} ${pc.dim(elapsed)}\n`);
|
|
5952
|
+
restoreCursorIfHidden();
|
|
5953
|
+
};
|
|
5954
|
+
return {
|
|
5955
|
+
start(initialMessage) {
|
|
5956
|
+
if (started || finished) return;
|
|
5957
|
+
started = true;
|
|
5958
|
+
startTime = performance.now();
|
|
5959
|
+
if (initialMessage) statusMessage = initialMessage;
|
|
5960
|
+
if (!isTty) {
|
|
5961
|
+
if (initialMessage) output.write(`${initialMessage}\n`);
|
|
5962
|
+
return;
|
|
5963
|
+
}
|
|
5964
|
+
if (withGuide) output.write(`${pc.gray(S_BAR)}\n`);
|
|
5965
|
+
output.write(CURSOR_HIDE);
|
|
5966
|
+
cursorHidden = true;
|
|
5967
|
+
onResize = () => {
|
|
5968
|
+
renderFrame();
|
|
5969
|
+
};
|
|
5970
|
+
onExit = () => {
|
|
5971
|
+
stopTimer();
|
|
5972
|
+
cleanupListeners();
|
|
5973
|
+
restoreCursorIfHidden();
|
|
5974
|
+
};
|
|
5975
|
+
onUncaught = () => {
|
|
5976
|
+
stopTimer();
|
|
5977
|
+
cleanupListeners();
|
|
5978
|
+
restoreCursorIfHidden();
|
|
5979
|
+
};
|
|
5980
|
+
onSigint = () => {
|
|
5981
|
+
finish("Export cancelled.", "cancel");
|
|
5982
|
+
process.exit(130);
|
|
5983
|
+
};
|
|
5984
|
+
onSigterm = () => {
|
|
5985
|
+
finish("Export terminated.", "cancel");
|
|
5986
|
+
process.exit(143);
|
|
5987
|
+
};
|
|
5988
|
+
output.on("resize", onResize);
|
|
5989
|
+
process.on("exit", onExit);
|
|
5990
|
+
process.on("uncaughtExceptionMonitor", onUncaught);
|
|
5991
|
+
process.on("SIGINT", onSigint);
|
|
5992
|
+
process.on("SIGTERM", onSigterm);
|
|
5993
|
+
renderFrame();
|
|
5994
|
+
timer = setInterval(() => {
|
|
5995
|
+
frameIndex = (frameIndex + 1) % SPINNER_FRAMES.length;
|
|
5996
|
+
if (dirty || performance.now() - lastRenderAt >= SPINNER_DELAY_MS) renderFrame();
|
|
5997
|
+
}, SPINNER_DELAY_MS);
|
|
5998
|
+
},
|
|
5999
|
+
update(event) {
|
|
6000
|
+
if (finished) return;
|
|
6001
|
+
if (!isTty) {
|
|
6002
|
+
const line = formatProgressLogLine(event);
|
|
6003
|
+
if (line !== void 0) output.write(`${line}\n`);
|
|
6004
|
+
return;
|
|
6005
|
+
}
|
|
6006
|
+
statusMessage = event.message;
|
|
6007
|
+
currentPhase = event.phase;
|
|
6008
|
+
phaseCurrent = event.current;
|
|
6009
|
+
phaseTotal = event.total;
|
|
6010
|
+
if (event.novelTitle) novelTitle = event.novelTitle;
|
|
6011
|
+
if (event.volumes) {
|
|
6012
|
+
volumes = event.volumes.map((item) => ({
|
|
6013
|
+
index: item.index,
|
|
6014
|
+
id: item.id,
|
|
6015
|
+
title: item.title,
|
|
6016
|
+
chapterTotal: item.chapterTotal,
|
|
6017
|
+
chaptersDone: 0,
|
|
6018
|
+
chaptersIssues: 0,
|
|
6019
|
+
mediaTotal: 0,
|
|
6020
|
+
mediaDone: 0,
|
|
6021
|
+
packState: "pending"
|
|
6022
|
+
}));
|
|
6023
|
+
maxVolumeTitleWidth = volumes.reduce((max, item) => Math.max(max, getDisplayWidth(item.title)), 0);
|
|
6024
|
+
activeVolumeIndex = volumes.length > 0 ? 0 : void 0;
|
|
6025
|
+
activeVolumeTitle = void 0;
|
|
6026
|
+
activeDetail = void 0;
|
|
6027
|
+
}
|
|
6028
|
+
if (event.phase === "chapters") {
|
|
6029
|
+
if (event.volumeIndex !== void 0) activeVolumeIndex = event.volumeIndex;
|
|
6030
|
+
activeVolumeTitle = event.volumeTitle;
|
|
6031
|
+
activeDetail = event.chapterTitle ?? event.message;
|
|
6032
|
+
if (event.chapterStatus !== "fetching" && event.volumeIndex !== void 0) {
|
|
6033
|
+
const vol = volumes[event.volumeIndex];
|
|
6034
|
+
if (vol) {
|
|
6035
|
+
vol.chaptersDone = event.volumeCurrent ?? vol.chaptersDone + 1;
|
|
6036
|
+
if (event.chapterStatus === "skipped" || event.chapterStatus === "failed") vol.chaptersIssues += 1;
|
|
6037
|
+
}
|
|
6038
|
+
}
|
|
6039
|
+
} else if (event.phase === "images") {
|
|
6040
|
+
if (event.volumeMediaTotals) for (let i = 0; i < event.volumeMediaTotals.length; i += 1) {
|
|
6041
|
+
const vol = volumes[i];
|
|
6042
|
+
const total = event.volumeMediaTotals[i];
|
|
6043
|
+
if (vol && total !== void 0) vol.mediaTotal = total;
|
|
6044
|
+
}
|
|
6045
|
+
if (event.affectedVolumeIndices) for (const idx of event.affectedVolumeIndices) {
|
|
6046
|
+
const vol = volumes[idx];
|
|
6047
|
+
if (vol) vol.mediaDone = Math.min(vol.mediaTotal, vol.mediaDone + 1);
|
|
6048
|
+
}
|
|
6049
|
+
activeVolumeTitle = event.volumeTitle;
|
|
6050
|
+
activeDetail = `Downloading media (${event.current}/${event.total})`;
|
|
6051
|
+
const nextPendingIdx = volumes.findIndex((vol) => vol.mediaDone < vol.mediaTotal);
|
|
6052
|
+
if (nextPendingIdx !== -1) activeVolumeIndex = nextPendingIdx;
|
|
6053
|
+
else if (volumes.length > 0) activeVolumeIndex = volumes.length - 1;
|
|
6054
|
+
} else if (event.phase === "pack" || event.phase === "write") {
|
|
6055
|
+
for (const vol of volumes) if (vol.packState === "writing") vol.packState = "written";
|
|
6056
|
+
if (options.mode === "split" && event.volumeIndex !== void 0) {
|
|
6057
|
+
activeVolumeIndex = event.volumeIndex;
|
|
6058
|
+
activeVolumeTitle = event.volumeTitle;
|
|
6059
|
+
const vol = volumes[event.volumeIndex];
|
|
6060
|
+
if (vol) {
|
|
6061
|
+
if (event.phase === "pack") {
|
|
6062
|
+
vol.packState = "packing";
|
|
6063
|
+
activeDetail = "Packing EPUB";
|
|
6064
|
+
} else if (event.skipped) {
|
|
6065
|
+
vol.packState = "skipped";
|
|
6066
|
+
activeDetail = "Skipped existing file";
|
|
6067
|
+
} else {
|
|
6068
|
+
vol.packState = "writing";
|
|
6069
|
+
activeDetail = stripVolumePrefix(event.message, event.volumeTitle);
|
|
6070
|
+
}
|
|
6071
|
+
}
|
|
6072
|
+
} else if (options.mode === "merged") {
|
|
6073
|
+
activeVolumeTitle = novelTitle || void 0;
|
|
6074
|
+
activeDetail = event.message;
|
|
6075
|
+
activeVolumeIndex = volumes.length > 0 ? volumes.length - 1 : void 0;
|
|
6076
|
+
const nextState = event.phase === "pack" ? "packing" : event.skipped ? "skipped" : "writing";
|
|
6077
|
+
for (const vol of volumes) vol.packState = nextState;
|
|
6078
|
+
}
|
|
6079
|
+
}
|
|
6080
|
+
overallRatio = Math.min(1, Math.max(overallRatio, computeOverallRatio(event)));
|
|
6081
|
+
if (Boolean(event.volumes) || event.phase === "images" && (event.current === 0 || event.current === event.total) || event.phase === "write" && event.current === event.total || performance.now() - lastRenderAt >= RENDER_THROTTLE_MS) renderFrame();
|
|
6082
|
+
else dirty = true;
|
|
6083
|
+
},
|
|
6084
|
+
stop(message) {
|
|
6085
|
+
for (const vol of volumes) if (vol.packState === "writing") vol.packState = "written";
|
|
6086
|
+
overallRatio = 1;
|
|
6087
|
+
finish(message, "submit");
|
|
6088
|
+
},
|
|
6089
|
+
error(message) {
|
|
6090
|
+
finish(message, "error");
|
|
6091
|
+
},
|
|
6092
|
+
cancel(message = "Export cancelled.") {
|
|
6093
|
+
finish(message, "cancel");
|
|
6094
|
+
}
|
|
6095
|
+
};
|
|
6096
|
+
}
|
|
5353
6097
|
// src/commands/export.ts
|
|
5354
6098
|
const exportCommand = defineCommand({
|
|
5355
6099
|
meta: {
|
|
@@ -5427,8 +6171,12 @@ const exportCommand = defineCommand({
|
|
|
5427
6171
|
output: args.output
|
|
5428
6172
|
});
|
|
5429
6173
|
const client = new AcanthisApiClient(config.serverUrl, config.token);
|
|
5430
|
-
const
|
|
5431
|
-
|
|
6174
|
+
const progressRenderer = !isJson && Boolean(process.stdout.isTTY) && Boolean(process.stderr.isTTY) && !isCI() ? createExportProgressRenderer({
|
|
6175
|
+
withGuide: false,
|
|
6176
|
+
illustrations: args.illustrations !== false,
|
|
6177
|
+
mode: effectiveMode
|
|
6178
|
+
}) : void 0;
|
|
6179
|
+
if (progressRenderer) progressRenderer.start(`Exporting novel from ${config.defaultSource}...`);
|
|
5432
6180
|
let summary;
|
|
5433
6181
|
try {
|
|
5434
6182
|
summary = await runExport(client, {
|
|
@@ -5445,16 +6193,19 @@ const exportCommand = defineCommand({
|
|
|
5445
6193
|
json: isJson
|
|
5446
6194
|
}, {
|
|
5447
6195
|
onProgress: (event) => {
|
|
5448
|
-
if (
|
|
5449
|
-
else
|
|
6196
|
+
if (progressRenderer) progressRenderer.update(event);
|
|
6197
|
+
else {
|
|
6198
|
+
const line = formatProgressLogLine(event);
|
|
6199
|
+
if (line !== void 0) writeProgressLine(line, isJson);
|
|
6200
|
+
}
|
|
5450
6201
|
},
|
|
5451
6202
|
onWarning: (message) => {
|
|
5452
|
-
if (!
|
|
6203
|
+
if (!progressRenderer) writeWarningLine(message, isJson);
|
|
5453
6204
|
}
|
|
5454
6205
|
});
|
|
5455
|
-
if (
|
|
6206
|
+
if (progressRenderer) progressRenderer.stop(`Exported "${summary.novelTitle}"`);
|
|
5456
6207
|
} catch (error) {
|
|
5457
|
-
if (
|
|
6208
|
+
if (progressRenderer) progressRenderer.error("Export failed");
|
|
5458
6209
|
throw error;
|
|
5459
6210
|
}
|
|
5460
6211
|
if (isJson) {
|
|
@@ -5466,7 +6217,7 @@ const exportCommand = defineCommand({
|
|
|
5466
6217
|
const statusLabel = file.skipped ? pc.yellow("SKIPPED") : pc.green("WRITTEN");
|
|
5467
6218
|
process.stdout.write(` ${statusLabel} ${file.path} ${pc.dim(`(${file.chapterCount} chapters)`)}\n`);
|
|
5468
6219
|
}
|
|
5469
|
-
if (
|
|
6220
|
+
if (progressRenderer && summary.warnings.length > 0) for (const warning of summary.warnings) writeWarningLine(warning, false);
|
|
5470
6221
|
}
|
|
5471
6222
|
});
|
|
5472
6223
|
// src/commands/info.ts
|
|
@@ -5980,8 +6731,13 @@ async function runInteractiveWizard() {
|
|
|
5980
6731
|
message: "Overwrite existing EPUB files if present?",
|
|
5981
6732
|
initialValue: false
|
|
5982
6733
|
}));
|
|
5983
|
-
const
|
|
5984
|
-
|
|
6734
|
+
const exportProgress = createExportProgressRenderer({
|
|
6735
|
+
withGuide: true,
|
|
6736
|
+
illustrations,
|
|
6737
|
+
mode,
|
|
6738
|
+
initialNovelTitle: novel.title
|
|
6739
|
+
});
|
|
6740
|
+
exportProgress.start("Starting EPUB export...");
|
|
5985
6741
|
const summary = await (async () => {
|
|
5986
6742
|
try {
|
|
5987
6743
|
const result = await runExport(client, {
|
|
@@ -5996,13 +6752,16 @@ async function runInteractiveWizard() {
|
|
|
5996
6752
|
strict: false,
|
|
5997
6753
|
overwrite,
|
|
5998
6754
|
json: false
|
|
5999
|
-
}, {
|
|
6000
|
-
|
|
6001
|
-
|
|
6002
|
-
|
|
6755
|
+
}, {
|
|
6756
|
+
prefetchedNovel: novel,
|
|
6757
|
+
onProgress: (event) => {
|
|
6758
|
+
exportProgress.update(event);
|
|
6759
|
+
}
|
|
6760
|
+
});
|
|
6761
|
+
exportProgress.stop(`Exported "${result.novelTitle}"`);
|
|
6003
6762
|
return result;
|
|
6004
6763
|
} catch (error) {
|
|
6005
|
-
|
|
6764
|
+
exportProgress.error("Export failed");
|
|
6006
6765
|
throw error;
|
|
6007
6766
|
}
|
|
6008
6767
|
})();
|