refactor: split downloader into modules
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const { QUALITY_LEVELS, QUALITY_LABELS } = require('./config');
|
||||
|
||||
function parseLevel(input) {
|
||||
const value = (input || '').trim().toLowerCase();
|
||||
if (!value) return null;
|
||||
if (/^\d+$/.test(value)) {
|
||||
const index = parseInt(value, 10) - 1;
|
||||
return index >= 0 && index < QUALITY_LEVELS.length ? QUALITY_LEVELS[index] : null;
|
||||
}
|
||||
return QUALITY_LEVELS.includes(value) ? value : null;
|
||||
}
|
||||
|
||||
function printQualityMenu(currentLevel) {
|
||||
QUALITY_LEVELS.forEach((level, index) => {
|
||||
const mark = level === currentLevel ? ' ←当前' : '';
|
||||
console.log(` ${index + 1}. ${level.padEnd(10)} ${(QUALITY_LABELS[level] || '').padEnd(8)}${mark}`);
|
||||
});
|
||||
}
|
||||
|
||||
async function askLevel(ask, currentLevel) {
|
||||
const label = QUALITY_LABELS[currentLevel] ? ` (${QUALITY_LABELS[currentLevel]})` : '';
|
||||
console.log(`\n 当前音质: ${currentLevel}${label}`);
|
||||
console.log(' 可选音质:');
|
||||
printQualityMenu(currentLevel);
|
||||
const input = await ask(' 选择音质 (输入序号或名称, 回车使用当前): ');
|
||||
const parsed = parseLevel(input);
|
||||
if (parsed) return parsed;
|
||||
if (input.trim()) console.log(' ⚠️ 无效选择,使用当前设置');
|
||||
return currentLevel;
|
||||
}
|
||||
|
||||
module.exports = { parseLevel, printQualityMenu, askLevel };
|
||||
Reference in New Issue
Block a user