-
Notifications
You must be signed in to change notification settings - Fork 0
/
userInput.js
84 lines (73 loc) · 2.31 KB
/
userInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
async function getOptionFromUser() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question('Choose an option ("1: Direct Url" or "2: search"): ', option => {
readline.close();
resolve(option);
});
});
}
async function getContentFromUser(prompt) {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question(prompt, content => {
readline.close();
resolve(content);
});
});
}
async function getSelectedLinkIndex(maxIndex) {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question(`Select a link (1-${maxIndex}): `, choice => {
readline.close();
const index = parseInt(choice, 10);
if (index >= 1 && index <= maxIndex) {
resolve(index - 1); // Convert to zero-based index
} else {
console.log("Invalid choice. Please select a valid link.");
resolve(getSelectedLinkIndex(maxIndex)); // Ask again
}
});
});
}
async function getCapChoice() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question(`Enter 'all' to fetch all chapters or specify a range (e.g., 10-29): `, choice => {
readline.close();
resolve(choice);
});
});
}
async function getPDFGenerationChoice() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
readline.question(`Do you want to create separate PDFs for each chapter (s) or merge all chapters into a single PDF (m)? `, (choice) => {
readline.close();
resolve(choice);
});
});
}
module.exports = {
getOptionFromUser,
getContentFromUser,
getSelectedLinkIndex,
getCapChoice,
getPDFGenerationChoice
};