-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·109 lines (97 loc) · 3.03 KB
/
Copy pathindex.js
File metadata and controls
executable file
·109 lines (97 loc) · 3.03 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", function(data){
processInput(data);
});
let maskChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_=+{}[]:;|\"'<>,.?/".split("");
class Char {
constructor(initial, animationInterval){
this.initial = initial;
this.decrypted = false;
this.animationInterval = animationInterval;
this.crypted = this.generateCrypted();
}
generateCrypted(){
return Char.doNotModifyChars.includes(this.initial) ? this.initial : maskChars[Math.floor(Math.random()*maskChars.length)]
}
initDecrypt(){
const gen = this.decrypt();
return new Promise((resolve)=>{
setInterval(()=>{
const {done} = gen.next();
if(done) {
resolve();
}
}, 300)
});
}
getValue(){
return this.decrypted ? this.initial : this.crypted;
}
* decrypt(){
for(let i=0;i<this.animationInterval;i++){
yield this.crypted = this.generateCrypted();
}
this.decrypted = true;
yield this.initial;
}
}
Char.doNotModifyChars = ["\n"," "];
class DataProcessor {
constructor(){
this.chars = null;//It supposed to contain all chars
this.rows = [];//It has all rows with text
this.decrypted = false;
}
getTextFromCryptedChars(chars){
return [].concat.apply([], chars).reduce((res, char)=>{
return res.push(char.getValue()), res;
},[]).join("");
}
getCryptedCharsFromText(text){
this.rows = text.split("\n");
this.chars = this.rows.reduce((res, row)=>{//[new Char, new Char, new Char,new Char, new Char, new Char...]
row = row.split("").map((char)=>{
return new Char(char, (Math.random()*20|0));
});
row.unshift(new Char("\n"));
return [...res, ...row];
}, []);
return this.chars;
}
getCryptedText(text){
return this.getTextFromCryptedChars(this.getCryptedCharsFromText(text))
}
initDecrypt(){
var pr = this.chars.map((char)=>{
return char.initDecrypt();
});
Promise.all(pr).then(()=>{
this.decrypted = true;
})
}
getSnapshot(){
let value = this.getTextFromCryptedChars(this.chars);
return {
done: this.decrypted,
value
}
}
}
let processInput = (data) => {
const dataProcessor = new DataProcessor();
process.stdout.write(dataProcessor.getCryptedText(data));
let interval = setInterval(()=>{
const l = dataProcessor.rows.length;
let {done, value} = dataProcessor.getSnapshot();
process.stdout.write('\033['+l+'A');
process.stdout.clearScreenDown();
process.stdout.write(value);
if(done){
clearInterval(interval);
process.exit();
}
}, 75);
dataProcessor.initDecrypt();
};