-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom-student.js
More file actions
223 lines (200 loc) · 5.48 KB
/
random-student.js
File metadata and controls
223 lines (200 loc) · 5.48 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const fs = require('fs');
const gradient = require('gradient-string');
const CFonts = require('cfonts');
const argv = require('minimist')(process.argv.slice(2));
// print extra console.logs
const isVerbose = (argv.v || argv.verbose);
const isHelpEnabled = (argv.help);
const isResetEnabled = (argv.clearCache);
const isInitialEnabled = (argv.initial || !argv.fullname);
const isConsoleClearEnabled = (argv.clearOnCall || argv.c);
const isPlainTextEnabled = (argv.plain || argv.p);
const logFile = __dirname +
((argv.logFile) ? argv.logFile : '/random-student.log');
// confirm log file location
if (isVerbose) console.log(logFile);
// cfonts configuration arrays:
const fonts = [
"block",
"shade",
"chrome",
"simple",
"simpleBlock",
"3d",
"simple3d",
"huge"//,
// "console"
];
const clrs = [
"system",
// "black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"gray",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright"
];
const bkgClrs = [
"transparent",
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"blackBright",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright"
];
/**
* Help - displays available options and exit script.
*/
if (isHelpEnabled) {
console.log('Available Options:')
console.log('--font random|<font>')
console.table(fonts)
console.log('--colors random|<color>')
console.table(clrs)
console.log('--background random|<bkg_color>')
console.table(bkgClrs)
console.log('--align <left|center|right>')
console.log('--clearOnCall | -c <true| >', 'clear terminal screen on call');
console.log(`--logFile='. / test.log' configure custom log file useful for testing`)
console.log('--plain | -p', 'plain text output')
console.log('--verbose | -v', 'output extra console logs for debugging')
return;
}
// students gets re-assigned to studentsAll on reset.
let students = (argv.studentsFile) ?
require(argv.studentsFile) :
require('./students.js');
const studentsAll = [...students];
/**
* Read history log file, remove those students from avail students list.
* Then call, or reset history first.
*/
function readHistoryAndCall() {
// Read history log on each run.
fs.readFile(logFile, 'utf-8', (err, history) => {
if (err) throw err;
const called = history.trim().split('\n');
// Removed called students from available students list.
for (let i = 0; i < called.length; i++) {
let calledStudent = called[i];
students.forEach((stu, index) => {
if (calledStudent === stu) {
students.splice(index, 1);
}
});
}
if (isVerbose) console.table(students);
// After all students have been called OR reset argument specified.
if (students.length === 0 || isResetEnabled) {
reset(callRandom);
} else {
callRandom();
}
});
}
/**
* Call random student from available students list.
* Using CFonts to output fancy text.
*/
function callRandom() {
let student = students[Math.floor(Math.random() * students.length)];
fs.appendFile(logFile, '\n' + student, (err) => {
if (err) throw err;
if (isVerbose) console.log('appendFile: ' + student);
});
if (isInitialEnabled) {
// Match firstname + last initial
student = student.match(/\w+\s+[A-Z]/).toString();
}
// TODO: plain logic
if (!isPlainTextEnabled) {
/**
* cFonts Options defaults | arguments
*/
// get color here so we can make it the default.
let randomColor = clrs[Math.floor(Math.random() * clrs.length)];
// Options for the cfonts module.
let cfontsOpts = {
align: 'center',
colors: [randomColor]
};
if (argv.colors) {
cfontsOpts.colors = (argv.colors === 'random') ?
[randomColor] :
argv.colors.split(',');
}
if (argv.font) {// default font is 'block'
cfontsOpts.font = (argv.font === 'random') ?
fonts[Math.floor(Math.random() * fonts.length)] :
argv.font;
}
if (argv.align) {
cfontsOpts.align = argv.align;
}
if (argv.background) {
// Random backgrounds could clash so...
if (argv.background === 'random') {
// Filter out the backgrounds that are the same as the selected foreground.
bkgClrs.forEach(function(color, index) {
if (new RegExp(cfontsOpts.colors.join('|')).test(color)) {
if (isVerbose) {
console.log('filtering out color ' + color + ' because font is ' + cfontsOpts.colors.toString())
}
bkgClrs.splice(index, 1);
}
})
}
// Background either to random
cfontsOpts.background = (argv.background === 'random') ?
bkgClrs[Math.floor(Math.random() * bkgClrs.length)] :
argv.background;
}
if (isConsoleClearEnabled) {
console.reset();
}
}
if (isPlainTextEnabled) {
console.log(student)
} else {
CFonts.say(student, cfontsOpts);
// debug
if (isVerbose) console.table(cfontsOpts);
}
}
/**
* Reset students array to original list
*/
function reset(cb) {
fs.writeFile(logFile, '', (err) => {
if (err) throw err;
console.log(gradient.instagram('All students called! Clearing called log!'));
students = studentsAll;
if (cb) cb();
});
}
console.reset = function () {
return process.stdout.write('\033c');
}
//-------------------------------------------------
readHistoryAndCall();