-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker-tape.js
41 lines (31 loc) · 967 Bytes
/
ticker-tape.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
const {
leftPad,
moveFirstNCharsToEnd,
repeatString
} = require(`./string-utils`);
function render (string, tickerProgress, tapeWidth, itemWidth) {
console.clear();
const lines = string.split(`\n`).filter((ln) => ln.length > 0);
const paddedLines = lines.map((line) => leftPad(line, itemWidth, ` `));
const repeatedLines = paddedLines.map((line) => repeatString(line, tapeWidth))
const mover = moveFirstNCharsToEnd.bind(null, tickerProgress);
const movedLines = repeatedLines.map(mover);
const output = movedLines.join(`\n`);
console.log(output);
}
module.exports = function TickerTape () {
const tapeWidth = 120;
const itemWidth = 30;
const tickRate = 100;
let value = ` `;
let tickerProgress = 0;
setInterval(() => {
render(value, tickerProgress, tapeWidth, itemWidth);
tickerProgress = (tickerProgress + 1) % tapeWidth;
}, tickRate);
return {
updateDisplay(newValue) {
value = newValue;
}
}
}