-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadliner.js
147 lines (129 loc) · 3.23 KB
/
headliner.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const {generateNewsTitles} = require('./wikinews');
const KEY_C_C = 3;
const KEY_BACKSPACE = 8;
const ASCII_MIN = 32; // space
const ASCII_MAX = 126; // tilde
class Headliner {
constructor({enableErrorBell = true} = {}) {
this.processStdin = this.processStdin.bind(this);
this.newsTitles = generateNewsTitles();
this.enableErrorBell = enableErrorBell;
this.currentPhrase = null;
this.phraseIndex = 0;
this.correctCharacters = 0;
this.missedCharacters = 0;
this.startTime = 0;
this.totalTime = 0;
}
setupStdin() {
process.stdin.setRawMode(true);
process.stdin.on('data', this.processStdin);
}
async newTitle() {
this.currentPhrase = null;
this.phraseIndex = 0;
this.currentPhrase = await this.newsTitles.next().value;
console.log(this.currentPhrase);
}
processStdin(data) {
const code = data.readInt8();
if (code === KEY_C_C) {
process.exit(0);
return;
}
if (!this.currentPhrase) {
return;
}
this.startTime = this.startTime || Date.now();
const currentCharCode = this.currentPhrase.charCodeAt(this.phraseIndex);
if (code === currentCharCode || currentCharCode >= ASCII_MAX) {
this.recordCorrectCharacter();
} else {
this.missedCharacters++;
if (this.enableErrorBell === true) {
soundBell();
}
}
}
recordCorrectCharacter() {
process.stdout.write(this.currentPhrase[this.phraseIndex]);
this.correctCharacters++;
if (++this.phraseIndex >= this.currentPhrase.length) {
process.stdout.write('\n');
this.endTitle();
this.newTitle();
}
}
endTitle() {
this.currentPhrase = null;
this.totalTime += Date.now() - this.startTime;
this.startTime = null;
const words = this.correctCharacters / 5;
const minutes = this.totalTime / 1000 / 60;
const wpm = (words / minutes).toFixed(1);
const characters = this.correctCharacters + this.missedCharacters;
const accuracy = ((this.correctCharacters / characters) * 100).toFixed(1);
console.log(`WPM: ${wpm}. Accuracy: ${accuracy}%`);
}
}
class FreeformHeadliner extends Headliner {
constructor() {
super(...arguments);
this.missDebt = 0;
}
newTitle() {
this.missDebt = 0;
return super.newTitle();
}
processStdin(data) {
const code = data.readInt8();
if (code === KEY_C_C) {
process.exit(0);
return;
}
if (!this.currentPhrase) {
return;
}
this.startTime = this.startTime || Date.now();
const currentCharCode = this.currentPhrase.charCodeAt(this.phraseIndex);
switch (code) {
case currentCharCode:
if (this.missDebt === 0) {
this.recordCorrectCharacter();
}
return;
case KEY_BACKSPACE:
if (this.missDebt > 0) {
this.missDebt--;
writeBackspace();
} else if (this.phraseIndex > 0) {
this.phraseIndex--;
writeBackspace();
}
return;
default:
if (currentCharCode > ASCII_MAX && this.missDebt === 0) {
this.recordCorrectCharacter(); // e for effort
} else {
this.missedCharacters++;
if (this.enableErrorBell === true) {
soundBell();
}
if (code >= ASCII_MIN) {
this.missDebt++;
process.stdout.write(data);
}
}
}
}
}
function writeBackspace() {
process.stdout.write('\b \b');
}
function soundBell() {
process.stderr.write('\u0007');
}
module.exports = {
Headliner,
FreeformHeadliner
};