-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (77 loc) · 1.57 KB
/
index.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
import { renderResult } from "./render.js";
import { getSubs, downloadSubs } from "./request.js";
let page = 1;
let pointer = 0;
const pages = [];
const onPress = (ch, key) => {
console.clear();
if (key && key.ctrl && key.name == "c") {
process.stdin.pause();
}
switch (key.name) {
case "down": {
if (pointer >= 6) {
pointer = pointer;
} else {
pointer++;
}
break;
}
case "up": {
if (pointer < 1) {
pointer = pointer;
} else {
pointer--;
}
break;
}
case "return": {
if (pointer == 5) {
page = prevPage(page);
} else if (pointer == 6) {
page = nextPage(page);
} else {
downloadSubs(pages[page - 1][pointer]);
}
break;
}
default: {
pointer = pointer;
break;
}
}
renderResult(pages[page - 1], page, pointer);
};
const nextPage = (page) => {
if (page >= pages.length) {
page = page;
} else {
page++;
}
return page;
};
const prevPage = (page) => {
if (page <= 1) {
page = page;
} else {
page--;
}
return page;
};
const pagenation = (listOfSubs) => {
for (let i = 0; i < Math.floor(listOfSubs.length / 5); i++) {
pages.push(listOfSubs.slice(i * 5, i * 5 + 5));
}
return pages;
};
const ctrl = async (movie) => {
try {
const listOfSubs = await getSubs(movie);
const pages = pagenation(listOfSubs);
renderResult(pages[page - 1], page, pointer);
process.stdin.on("keypress", onPress);
} catch (error) {
console.error(error);
}
};
ctrl(process.argv[2]);