1/*
2 *  Test using timers and intervals with curses.
3 */
4
5if (typeof Ncurses !== 'object') {
6    throw new Error('Ncurses required');
7}
8
9function fillScreen(ch) {
10    var size, w, h;
11    var i, j;
12
13    size = Ncurses.getmaxyx();
14    h = size[0];
15    w = size[1];
16
17    for (i = 0; i < h; i++) {
18        for (j = 0; j < w; j++) {
19            Ncurses.mvprintw(i, j, ch);
20        }
21    }
22    Ncurses.refresh();
23}
24
25function main() {
26    var i, j;
27    var counters = [];
28    var size, w, h;
29
30    Ncurses.initscr();
31    size = Ncurses.getmaxyx();
32    h = size[0];
33    w = size[1];
34
35    fillScreen('.');
36
37    setInterval(function () {
38        Ncurses.mvprintw(1, 4, new Date().toISOString());
39        Ncurses.refresh();
40    }, 1000);
41
42    function addCounter(row, index, interval) {
43        counters[index] = 0;
44        setInterval(function () {
45            counters[index]++;
46            Ncurses.mvprintw(row, 4, '' + Date.now() + ' ' + counters[index]);
47            Ncurses.refresh();
48        }, interval);
49    }
50
51    function addRandomChar(row, col, interval) {
52        setTimeout(function () {
53            Ncurses.mvprintw(row, col, String.fromCharCode(Math.random() * 64 + 0x20));
54            Ncurses.refresh();
55        }, interval);
56    }
57
58    for (i = 0; i < h - 5; i++) {
59        addCounter(3 + i, i, 363 * i + 400);
60    }
61
62    /* Here the inserts take a lot of time because the underlying timer manager
63     * data structure has O(n) insertion performance.
64     */
65    for (i = 0; i < h - 5; i++) {
66        for (j = 0; j < w - 50; j++) {
67            // Math.exp(0)...Math.exp(8) is an uneven distribution between 1...~2980.
68            addRandomChar(3 + i, 28 + j, 58000 - Math.exp(Math.random() * 8) * 20);
69        }
70    }
71
72    setTimeout(function () {
73        Ncurses.endwin();
74        Ncurses.delscreen();
75        requestEventLoopExit();
76    }, 120e3);
77}
78
79main();
80