-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·49 lines (43 loc) · 1.24 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
#!/usr/bin/env node
var fs = require('fs')
var RAINBOW_COLORS = [124, 196, 202, 208, 214, 220, 226, 154, 82, 46, 42, 45, 33, 21, 19]
function rainbow (text, blur) {
if (text === '') return ''
var color = Math.floor(Math.random() * 10 % RAINBOW_COLORS.length)
//var color = 0
var result = ''
var lines = text.split('\n')
var segment = blur
for (var line in lines) {
if (line % segment === 0) {
color = (color + 1) % RAINBOW_COLORS.length
}
result += rainbowLine(lines[line], blur, segment, color) + '\n'
blur = blur - 1 <= 0 ? segment : blur - 1
}
return result.slice(0, result.length-1)
}
function colorify (num, text) {
return '\u001b[38;5;' + num + 'm' + text + '\u001b[39m'
}
function rainbowLine (line, startLength, segmentLength, startColor) {
if (line === '') return ''
var result = ''
var chars = line.split('')
var checkLength = startLength
var oldLength = 0
for (var c = 0; c < chars.length; c += 1) {
if (c - oldLength === checkLength) {
startColor = (startColor + 1) % RAINBOW_COLORS.length
oldLength = c
checkLength = segmentLength
}
result += colorify(RAINBOW_COLORS[startColor], chars[c])
}
return result
}
module.exports = {
rainbow,
colorify,
rainbowLine
}