summaryrefslogtreecommitdiff
path: root/Library/CrashHandler.js
blob: 86e14b4e728cf419b24a96d4c9b1653557957aef (plain)
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
global._STRAWBERRY_CURRENT_APP = "#SYSTEM#";

process.removeAllListeners("uncaughtException");
process.on('uncaughtException', (error) => {
    Strawberry.Tick.Events = [];
    Strawberry.ProcessKeyboard = false;

    let lines = [
        "Sorry, a system error occurred.",
        "\"" + global._STRAWBERRY_CURRENT_APP + "\"",
        "  error type " + parseInt(require('crypto').createHash('md5').update(error.name).digest('hex').substring(0, 2), 16)
    ];

    let longest = lines.reduce(function(a, b) {
        return a.length > b.length ? a : b
    }, '');

    let col = "red";
    let confirm = "Restart";
    longest = longest + 5;

    let v = Math.round((process.stdout.rows / 2 + 1) - 5 - (lines.length / 2));
    let horizontal = Math.round(process.stdout.columns / 2 - (1 + ((longest.length + 11) / 2)));

    process.stdout.cursorTo(horizontal, v);
    process.stdout.write(color.bgWhite[col]("┏━━━━━━" + "━".repeat(longest.length) + "━━━┓"));

    process.stdout.cursorTo(horizontal, v + 1);
    process.stdout.write(color.bgWhite[col]("┃      " + " ".repeat(longest.length) + "   ┃"));

    let index = 1;
    for (let line of lines) {
        process.stdout.cursorTo(horizontal, v + 1 + index);

        if (index === 1) {
            process.stdout.write(color.bgWhite.black(color[col]("┃") + "  " + chalk.bgRed.black("<!>") + " " + line + " ".repeat(longest.length - line.length) + "   " + color[col]("┃")));
        } else {
            process.stdout.write(color.bgWhite.black(color[col]("┃") + "      " + line + " ".repeat(longest.length - line.length) + "   " + color[col]("┃")));
        }

        index++;
    }

    process.stdout.cursorTo(horizontal, v + 1 + index);
    process.stdout.write(color.bgWhite[col]("┃      " + " ".repeat(longest.length) + "   ┃"));

    process.stdout.cursorTo(horizontal, v + 2 + index);
    process.stdout.write(color.bgWhite[col]("┃      " + " ".repeat(longest.length - (confirm.length + 2)) + chalk.bgBlack.white(" " + confirm + " ") + "   ┃"));

    process.stdout.cursorTo(horizontal, v + 3 + index);
    process.stdout.write(color.bgWhite[col]("┃      " + " ".repeat(longest.length) + "   ┃"));

    process.stdout.cursorTo(horizontal, v + 4 + index);
    process.stdout.write(color.bgWhite[col]("┗━━━━━━" + "━".repeat(longest.length) + "━━━┛"));

    process.stdin.on('keypress', (str, key) => {
        if (key.sequence === "\r") {
            console.clear();
            process.stdout.cursorTo(0, 0);
            setTimeout(() => {
                require('child_process').spawnSync(process.argv[0], process.argv.filter((_, i) => { return i > 0 }), { stdio: "inherit" });
            }, 1000)
        }
    });
})