-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
151 lines (118 loc) · 3.54 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
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
"use strict";
const cursor = require('ansi')(process.stdout);
const Reader = require('line-by-line');
const chalk = require('chalk');
// sleep will be loaded by the init function (if available)
let sleep = null;
const options = {
// To animate or not (only works if the sleep module is available)
animate: false,
// Duration of the animation
duration: 12,
// Seed of the rainbow, use the same for the same pattern
seed: 0,
// Animation speed
speed: 20,
// Spread of the rainbow
spread: 8.0,
// Frequency of the rainbow colors
freq: 0.3,
// Whether to display error messages or not
debug: false,
};
const rainbow = function(freq, i) {
const red = Math.round(Math.sin(freq * i + 0) * 127 + 128);
const green = Math.round(Math.sin(freq * i + 2 * Math.PI / 3) * 127 + 128);
const blue = Math.round(Math.sin(freq * i + 4 * Math.PI / 3) * 127 + 128);
return {
red: red,
green: green,
blue: blue
}
};
const colorize = function(char, colors) {
process.stdout.write(chalk.rgb(colors.red, colors.green, colors.blue)(char));
};
const printlnPlain = function(colorize, line) {
for (let i = 0; i < line.length; i++) {
colorize(line[i], rainbow(options.freq, options.seed + i / options.spread));
}
};
const printlnAnimated = function(colorize, line) {
if (sleep) {
// Backup the seed
const seed = options.seed;
for (let j = 1; j < options.duration; j++) {
process.stdout.cursorTo(0);
options.seed += options.spread;
if (j % 2 === 0) {
printlnPlain(colorize, line);
}
sleep.usleep(1/options.speed * 500000);
}
// Restore the original seed
options.seed = seed;
}
printlnPlain(colorize, line);
};
const println = function(line) {
cursor.show();
if (options.animate) {
cursor.hide();
printlnAnimated(colorize, line);
} else {
printlnPlain(colorize, line);
}
process.stdout.write('\n');
};
const fromPipe = function() {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
const lines = data.split('\n');
for (let line in lines) {
options.seed += 1;
println(lines[line]);
}
cursor.show();
});
return new Promise(resolve => process.stdin.on('end', resolve));
};
const fromFile = function(file) {
const fileReader = new Reader(file)
fileReader.on('line', function (line) {
options.seed += 1;
println(line);
cursor.show();
});
return new Promise(resolve => fileReader.on('end', resolve));
};
const fromString = function(string) {
string = string || '';
const lines = string.split('\n')
lines.forEach(function (line) {
options.seed += 1;
println(line);
cursor.show();
});
};
const init = function() {
// Because sleep is a native module, depending on the
// platform of the user, the compilation might fail,
// in this case fallback, and show no animations.
try {
sleep = require('sleep');
} catch (error) {
if (options.debug) {
console.error('The sleep module is not available, animations will be disabled.', error);
}
}
return null;
}
exports.options = options;
exports.println = println;
exports.rainbow = rainbow;
exports.fromPipe = fromPipe;
exports.fromFile = fromFile;
exports.fromString = fromString;
exports.init = init;