filesystem.class.js 26.2 KB
Newer Older
G
GitSquared 已提交
1 2 3 4
class FilesystemDisplay {
    constructor(opts) {
        if (!opts.parentId) throw "Missing options";

G
GitSquared 已提交
5 6 7
        const fs = require("fs");
        const path = require("path");
        this.cwd = [];
G
GitSquared 已提交
8 9
        this.iconcolor = `rgb(${window.theme.r}, ${window.theme.g}, ${window.theme.b})`;
        this.icons = {
10
            showDisks: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 15.9994,19.9981L 19.9994,19.9981L 19.9994,15.9981L 15.9994,15.9981M 15.9994,13.9981L 19.9994,13.9981L 19.9994,9.99807L 15.9994,9.99807M 9.99938,7.99807L 13.9994,7.99807L 13.9994,3.99807L 9.99938,3.99807M 15.9994,7.99807L 19.9994,7.99807L 19.9994,3.99807L 15.9994,3.99807M 9.99938,13.9981L 13.9994,13.9981L 13.9994,9.99807L 9.99938,9.99807M 3.99938,13.9981L 7.99938,13.9981L 7.99938,9.99807L 3.99938,9.99807M 3.99938,19.9981L 7.99938,19.9981L 7.99938,15.9981L 3.99938,15.9981M 9.99938,19.9981L 13.9994,19.9981L 13.9994,15.9981L 9.99938,15.9981M 3.99938,7.99807L 7.99938,7.99807L 7.99938,3.99807L 3.99938,3.99807L 3.99938,7.99807 Z "/>`,
G
GitSquared 已提交
11 12 13 14
            up: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 22,4L 14,4L 12,2L 6,2C 4.9,2 4.01,2.9 4.01,4L 4,16C 4,17.1 4.9,18 6,18L 22,18C 23.1,18 24,17.1 24,16L 24,6C 24,4.9 23.1,4 22,4 Z M 2,6L -2.98023e-008,6L -2.98023e-008,11L 0.0100021,11L -2.98023e-008,20C -2.98023e-008,21.1 0.900001,22 2,22L 20,22L 20,20L 2,20L 2,6 Z "/>`,
            dir: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 9.99936,3.99807L 3.99936,3.99807C 2.89436,3.99807 2.00936,4.89406 2.00936,5.99807L 1.99936,17.9981C 1.99936,19.1021 2.89436,19.9981 3.99936,19.9981L 19.9994,19.9981C 21.1029,19.9981 21.9994,19.1021 21.9994,17.9981L 21.9994,7.99807C 21.9994,6.89406 21.1029,5.99807 19.9994,5.99807L 11.9994,5.99807L 9.99936,3.99807 Z "/>`,
            symlink: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 15.9995,5.99817L 12.9995,5.99817L 12.9995,7.89813L 15.9995,7.89813C 18.2635,7.89813 20.0995,9.73413 20.0995,11.9982C 20.0995,14.2621 18.2635,16.0981 15.9995,16.0981L 12.9995,16.0981L 12.9995,17.9982L 15.9995,17.9982C 19.3145,17.9982 21.9995,15.3121 21.9995,11.9982C 21.9995,8.68414 19.3145,5.99817 15.9995,5.99817 Z M 3.89948,11.9982C 3.89948,9.73413 5.7355,7.89813 7.99948,7.89813L 10.9995,7.89813L 10.9995,5.99817L 7.99948,5.99817C 4.68649,5.99817 1.99948,8.68414 1.99948,11.9982C 1.99948,15.3121 4.68649,17.9982 7.99948,17.9982L 10.9995,17.9982L 10.9995,16.0981L 7.99948,16.0981C 5.7355,16.0981 3.89948,14.2621 3.89948,11.9982 Z M 7.99948,12.9982L 15.9995,12.9982L 15.9995,10.9982L 7.99948,10.9982L 7.99948,12.9982 Z "/>`,
            file: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 12.9994,8.99807L 12.9994,3.49807L 18.4994,8.99807M 5.99939,1.99807C 4.89438,1.99807 4.0094,2.89406 4.0094,3.99807L 3.99939,19.9981C 3.99939,21.1021 4.88538,21.9981 5.98938,21.9981L 17.9994,21.9981C 19.1034,21.9981 19.9994,21.1021 19.9994,19.9981L 19.9994,7.99807L 13.9994,1.99807L 5.99939,1.99807 Z "/>`,
15
            other: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 11,18L 13,18L 13,16L 11,16L 11,18 Z M 12,6C 9.79,6 8,7.79 8,10L 10,10C 10,8.9 10.9,8 12,8C 13.1,8 14,8.9 14,10C 14,12 11,11.75 11,15L 13,15C 13,12.75 16,12.5 16,10C 16,7.79 14.21,6 12,6 Z M 5,3L 19,3C 20.1046,3 21,3.89543 21,5L 21,19C 21,20.1046 20.1046,21 19,21L 5,21C 3.89543,21 3,20.1046 3,19L 3,5C 3,3.89543 3.89543,3 5,3 Z "/>`,
16 17 18
            disk: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-linejoin="round" d="M 6,2L 18,2C 19.1046,2 20,2.89543 20,4L 20,20C 20,21.1046 19.1046,22 18,22L 6,22C 4.89543,22 4,21.1046 4,20L 4,4C 4,2.89543 4.89543,2 6,2 Z M 12,4.00001C 8.68629,4.00001 5.99999,6.6863 5.99999,10C 5.99999,13.3137 8.68629,16 12.1022,15.9992L 11.2221,13.7674C 10.946,13.2891 11.1099,12.6775 11.5882,12.4013L 12.4542,11.9013C 12.9325,11.6252 13.5441,11.7891 13.8202,12.2674L 15.7446,14.6884C 17.1194,13.5889 18,11.8973 18,10C 18,6.6863 15.3137,4.00001 12,4.00001 Z M 12,9.00001C 12.5523,9.00001 13,9.44773 13,10C 13,10.5523 12.5523,11 12,11C 11.4477,11 11,10.5523 11,10C 11,9.44773 11.4477,9.00001 12,9.00001 Z M 7,18C 6.44771,18 6,18.4477 6,19C 6,19.5523 6.44771,20 7,20C 7.55228,20 8,19.5523 8,19C 8,18.4477 7.55228,18 7,18 Z M 12.0882,13.2674L 14.5757,19.5759L 17.1738,18.0759L 12.9542,12.7674L 12.0882,13.2674 Z "/>`,
            rom: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 11.9975,13.9987C 10.8938,13.9987 10,13.1 10,11.9975C 10,10.8938 10.8938,10 11.9975,10C 13.105,10 13.9987,10.8938 13.9987,11.9975C 13.9987,13.1 13.105,13.9987 11.9975,13.9987 Z M 11.9975,3.99875C 7.5825,3.99875 3.99875,7.5775 3.99875,11.9975C 3.99875,16.4162 7.5825,20 11.9975,20C 16.4163,20 20,16.4162 20,11.9975C 20,7.5775 16.4163,3.99875 11.9975,3.99875 Z "/>`,
            usb: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 14.9994,6.99807L 14.9994,10.9981L 15.9994,10.9981L 15.9994,12.9981L 12.9994,12.9981L 12.9994,4.99807L 14.9994,4.99807L 11.9994,0.998068L 8.9994,4.99807L 10.9994,4.99807L 10.9994,12.9981L 7.9994,12.9981L 7.9994,10.9281C 8.7034,10.5611 9.1994,9.84707 9.1994,8.99807C 9.1994,7.78307 8.2144,6.79807 6.9994,6.79807C 5.7844,6.79807 4.7994,7.78307 4.7994,8.99807C 4.7994,9.84707 5.2954,10.5611 5.9994,10.9281L 5.9994,12.9981C 5.9994,14.1031 6.8934,14.9981 7.9994,14.9981L 10.9994,14.9981L 10.9994,18.0491C 10.2894,18.4141 9.7994,19.1451 9.7994,19.9981C 9.7994,21.2131 10.7844,22.1981 11.9994,22.1981C 13.2144,22.1981 14.1994,21.2131 14.1994,19.9981C 14.1994,19.1451 13.7084,18.4141 12.9994,18.0491L 12.9994,14.9981L 15.9994,14.9981C 17.1044,14.9981 17.9994,14.1031 17.9994,12.9981L 17.9994,10.9981L 18.9994,10.9981L 18.9994,6.99807L 14.9994,6.99807 Z "/>`,
19
            edex: {
20
                theme: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 17.9994,3.99805L 17.9994,2.99805C 17.9994,2.44604 17.5514,1.99805 16.9994,1.99805L 4.9994,1.99805C 4.4474,1.99805 3.9994,2.44604 3.9994,2.99805L 3.9994,6.99805C 3.9994,7.55005 4.4474,7.99805 4.9994,7.99805L 16.9994,7.99805C 17.5514,7.99805 17.9994,7.55005 17.9994,6.99805L 17.9994,5.99805L 18.9994,5.99805L 18.9994,9.99805L 8.9994,9.99805L 8.9994,20.998C 8.9994,21.55 9.4474,21.998 9.9994,21.998L 11.9994,21.998C 12.5514,21.998 12.9994,21.55 12.9994,20.998L 12.9994,11.998L 20.9994,11.998L 20.9994,3.99805L 17.9994,3.99805 Z "/>`,
21 22 23 24
                themesDir: `<path fill="${this.iconcolor}" stroke-linejoin="round" d="m9.9994 3.9981h-6c-1.105 0-1.99 0.896-1.99 2l-0.01 12c0 1.104 0.895 2 2 2h16c1.104 0 2-0.896 2-2v-9.9999c0-1.104-0.896-2-2-2h-8l-1.9996-2z" stroke-width=".2"/><path stroke-linejoin="round" d="m18.8 9.3628v-0.43111c0-0.23797-0.19314-0.43111-0.43111-0.43111h-5.173c-0.23797 0-0.43111 0.19313-0.43111 0.43111v1.7244c0 0.23797 0.19314 0.43111 0.43111 0.43111h5.1733c0.23797 0 0.43111-0.19314 0.43111-0.43111v-0.43111h0.43111v1.7244h-4.3111v4.7422c0 0.23797 0.19314 0.43111 0.43111 0.43111h0.86221c0.23797 0 0.43111-0.19314 0.43111-0.43111v-3.879h3.449v-3.4492z" stroke-width=".086221" fill="${window.theme.colors.light_black}"/>`,
                kblayout: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 18.9994,9.99807L 16.9994,9.99807L 16.9994,7.99807L 18.9994,7.99807M 18.9994,12.9981L 16.9994,12.9981L 16.9994,10.9981L 18.9994,10.9981M 15.9994,9.99807L 13.9994,9.99807L 13.9994,7.99807L 15.9994,7.99807M 15.9994,12.9981L 13.9994,12.9981L 13.9994,10.9981L 15.9994,10.9981M 15.9994,16.9981L 7.99941,16.9981L 7.99941,14.9981L 15.9994,14.9981M 6.99941,9.99807L 4.99941,9.99807L 4.99941,7.99807L 6.99941,7.99807M 6.99941,12.9981L 4.99941,12.9981L 4.99941,10.9981L 6.99941,10.9981M 7.99941,10.9981L 9.99941,10.9981L 9.99941,12.9981L 7.99941,12.9981M 7.99941,7.99807L 9.99941,7.99807L 9.99941,9.99807L 7.99941,9.99807M 10.9994,10.9981L 12.9994,10.9981L 12.9994,12.9981L 10.9994,12.9981M 10.9994,7.99807L 12.9994,7.99807L 12.9994,9.99807L 10.9994,9.99807M 19.9994,4.99807L 3.99941,4.99807C 2.89441,4.99807 2.0094,5.89406 2.0094,6.99807L 1.99941,16.9981C 1.99941,18.1021 2.89441,18.9981 3.99941,18.9981L 19.9994,18.9981C 21.1034,18.9981 21.9994,18.1021 21.9994,16.9981L 21.9994,6.99807C 21.9994,5.89406 21.1034,4.99807 19.9994,4.99807 Z "/>`,
                kblayoutsDir: `<path fill="${this.iconcolor}" stroke-linejoin="round" d="m9.9994 3.9981h-6c-1.105 0-1.99 0.896-1.99 2l-0.01 12c0 1.104 0.895 2 2 2h16c1.104 0 2-0.896 2-2v-9.9999c0-1.104-0.896-2-2-2h-8l-1.9996-2z" stroke-width=".2"/><path stroke-linejoin="round" d="m17.48 11.949h-1.14v-1.14h1.14m0 2.8499h-1.14v-1.14h1.14m-1.7099-0.56999h-1.14v-1.14h1.14m0 2.8499h-1.14v-1.14h1.14m0 3.4199h-4.56v-1.14h4.56m-5.13-2.85h-1.1399v-1.14h1.14m0 2.8499h-1.1399v-1.14h1.14m0.56998 0h1.14v1.14h-1.14m0-2.8499h1.14v1.14h-1.14m1.7099 0.56999h1.14v1.14h-1.14m0-2.8499h1.14v1.14h-1.14m5.13-2.8494h-9.1199c-0.62982 0-1.1343 0.51069-1.1343 1.14l-0.0057 5.6998c0 0.62925 0.51013 1.14 1.14 1.14h9.1196c0.62925 0 1.14-0.5107 1.14-1.14v-5.6998c0-0.62926-0.5107-1.14-1.14-1.14z" stroke-width="0.114" fill="${window.theme.colors.light_black}"/>`,
                settings: `<path fill="${this.iconcolor}" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 11.9994,15.498C 10.0664,15.498 8.49939,13.931 8.49939,11.998C 8.49939,10.0651 10.0664,8.49805 11.9994,8.49805C 13.9324,8.49805 15.4994,10.0651 15.4994,11.998C 15.4994,13.931 13.9324,15.498 11.9994,15.498 Z M 19.4284,12.9741C 19.4704,12.6531 19.4984,12.329 19.4984,11.998C 19.4984,11.6671 19.4704,11.343 19.4284,11.022L 21.5414,9.36804C 21.7294,9.21606 21.7844,8.94604 21.6594,8.73004L 19.6594,5.26605C 19.5354,5.05005 19.2734,4.96204 19.0474,5.04907L 16.5584,6.05206C 16.0424,5.65607 15.4774,5.32104 14.8684,5.06903L 14.4934,2.41907C 14.4554,2.18103 14.2484,1.99805 13.9994,1.99805L 9.99939,1.99805C 9.74939,1.99805 9.5434,2.18103 9.5054,2.41907L 9.1304,5.06805C 8.52039,5.32104 7.95538,5.65607 7.43939,6.05206L 4.95139,5.04907C 4.7254,4.96204 4.46338,5.05005 4.33939,5.26605L 2.33939,8.73004C 2.21439,8.94604 2.26938,9.21606 2.4574,9.36804L 4.5694,11.022C 4.5274,11.342 4.49939,11.6671 4.49939,11.998C 4.49939,12.329 4.5274,12.6541 4.5694,12.9741L 2.4574,14.6271C 2.26938,14.78 2.21439,15.05 2.33939,15.2661L 4.33939,18.73C 4.46338,18.946 4.7254,19.0341 4.95139,18.947L 7.4404,17.944C 7.95639,18.34 8.52139,18.675 9.1304,18.9271L 9.5054,21.577C 9.5434,21.8151 9.74939,21.998 9.99939,21.998L 13.9994,21.998C 14.2484,21.998 14.4554,21.8151 14.4934,21.577L 14.8684,18.9271C 15.4764,18.6741 16.0414,18.34 16.5574,17.9431L 19.0474,18.947C 19.2734,19.0341 19.5354,18.946 19.6594,18.73L 21.6594,15.2661C 21.7844,15.05 21.7294,14.78 21.5414,14.6271L 19.4284,12.9741 Z "/>`
25
            }
G
GitSquared 已提交
26 27
        };

G
GitSquared 已提交
28
        const container = document.getElementById(opts.parentId);
G
GitSquared 已提交
29
        container.innerHTML = `
30
            <h3 class="title"><p>FILESYSTEM</p><p id="fs_disp_title_dir"></p></h3>
G
GitSquared 已提交
31
            <div id="fs_disp_container">
32 33
            </div>
            <div id="fs_space_bar">
34
                <h1>EXIT DISPLAY</h1>
35
                <h3>Calculating available space...</h3><progress value="100" max="100"></progress>
G
GitSquared 已提交
36 37
            </div>`;
        this.filesContainer = document.getElementById("fs_disp_container");
38 39 40 41 42
        this.space_bar = {
            text: document.querySelector("#fs_space_bar > h3"),
            bar: document.querySelector("#fs_space_bar > progress")
        };
        this.fsBlock = {};
43
        this.dirpath = "";
G
GitSquared 已提交
44
        this.failed = false;
45
        this._noTracking = false;
G
GitSquared 已提交
46
        this._runNextTick = false;
47

G
GitSquared 已提交
48 49 50
        this._timer = setInterval(() => {
            if (this._runNextTick === true) {
                this._runNextTick = false;
51
                this.readFS(this.dirpath);
G
GitSquared 已提交
52 53
            }
        }, 1000);
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        this._asyncFSwrapper = new Proxy(fs, {
            get: function(fs, prop) {
                if (prop in fs) {
                    return function(...args) {
                        return new Promise((resolve, reject) => {
                            fs[prop](...args, (err, d) => {
                                if (typeof err !== "undefined" && err !== null) reject(err);
                                if (typeof d !== "undefined") resolve(d);
                                if (typeof d === "undefined" && typeof err === "undefined") resolve();
                            });
                        });
                    }
                }
            },
            set: function() {
                return false;
            }
        });

G
GitSquared 已提交
74 75 76 77 78 79
        this.setFailedState = () => {
            this.failed = true;
            container.innerHTML = `
            <h3 class="title"><p>FILESYSTEM</p><p id="fs_disp_title_dir">EXECUTION FAILED</p></h3>
            <h2 id="fs_disp_error">CANNOT ACCESS CURRENT WORKING DIRECTORY</h2>`;
        };
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        this.followTab = () => {
            let num = window.currentTerm;

            window.term[num].oncwdchange = (cwd) => {
                if (cwd && window.currentTerm === num) {
                    if (this._fsWatcher) {
                        this._fsWatcher.close();
                    }
                    if (cwd.startsWith("FALLBACK |-- ")) {
                        this.readFS(cwd.slice(13));
                        this._noTracking = true;
                    } else {
                        this.readFS(cwd);
                        this.watchFS(cwd);
                    }
96
                }
97
            };
G
GitSquared 已提交
98
        };
99
        this.followTab();
G
GitSquared 已提交
100

101
        this.watchFS = (dir) => {
G
GitSquared 已提交
102 103 104
            if (this._fsWatcher) {
                this._fsWatcher.close();
            }
105
            this._fsWatcher = fs.watch(dir, () => {
G
GitSquared 已提交
106 107 108 109
                this._runNextTick = true;
            });
        };

110
        this.readFS = async dir => {
G
GitSquared 已提交
111
            if (this.failed === true) return false;
112
            let tcwd = dir;
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
            let content = await this._asyncFSwrapper.readdir(tcwd).catch(err => {
                console.warn(err);
                if (this._noTracking === true && this.dirpath) { // #262
                    this.setFailedState();
                    setTimeout(() => {
                        this.readFS(this.dirpath);
                    }, 1000);
                } else {
                    this.setFailedState();
                }
            });

            this.cwd = [];

            await new Promise((resolve, reject) => {
                if (content.length === 0) resolve();

                content.forEach(async (file, i) => {
                    let fstat = await this._asyncFSwrapper.lstat(path.join(tcwd, file)).catch(reject);

                    if (fstat.isDirectory()) {
                        if (tcwd === settingsDir && file === "themes") {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "edex-themesDir",
                                category: "dir"
                            });
                        } else if (tcwd === settingsDir && file === "keyboards") {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "edex-kblayoutsDir",
                                category: "dir"
                            });
                        } else {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "dir",
                                category: "dir"
                            });
                        }
                    } else if (fstat.isSymbolicLink()) {
                        this.cwd.push({
                            name: window._escapeHtml(file),
                            type: "symlink",
                            category: "symlink"
                        });
                    } else if (fstat.isFile()) {
                        if (tcwd === themesDir && file.endsWith(".json")) {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "edex-theme",
                                category: "file"
                            });
                        } else if (tcwd === keyboardsDir && file.endsWith(".json")) {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "edex-kblayout",
                                category: "file"
                            });
                        } else if (tcwd === settingsDir && file === "settings.json") {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "edex-settings",
                                category: "file"
                            });
                        } else {
                            this.cwd.push({
                                name: window._escapeHtml(file),
                                type: "file",
                                category: "file"
                            });
                        }
185
                    } else {
186 187 188 189 190
                        this.cwd.push({
                            name: window._escapeHtml(file),
                            type: "other",
                            category: "other"
                        });
191
                    }
G
GitSquared 已提交
192

193 194 195
                    if (i === content.length-1) resolve();
                });
            }).catch(() => { this.setFailedState() });
196

197
            if (this.failed) return false;
198

199 200 201 202 203 204
            let ordering = {
                dir: 0,
                symlink: 1,
                file: 2,
                other: 3
            };
G
GitSquared 已提交
205

206 207 208
            this.cwd.sort((a, b) => {
                return (ordering[a.category] - ordering[b.category] || a.name.localeCompare(b.name));
            });
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
            this.cwd.splice(0, 0, {
                name: "Show disks",
                type: "showDisks"
            });

            if (tcwd !== "/" && tcwd !== "\\") {
                this.cwd.splice(1, 0, {
                    name: "Go up",
                    type: "up"
                });
            }

            let d = await window.si.fsSize().catch(() => {
                this.setFailedState();
            });
            d.forEach(fsBlock => {
                if (tcwd.startsWith(fsBlock.mount)) {
                    this.fsBlock = fsBlock;
G
GitSquared 已提交
228
                }
229
            });
230 231 232

            this.dirpath = tcwd;
            this.render(this.cwd);
G
GitSquared 已提交
233 234
        };

235
        this.readDevices = async () => {
236 237
            if (this.failed === true) return false;

238 239 240 241 242 243 244
            let blocks = await window.si.blockDevices();
            let devices = [];
            blocks.forEach(block => {
                if (fs.existsSync(block.mount)) {
                    let type = (block.type === "rom") ? "rom" : "disk";
                    if (block.removable && block.type !== "rom") {
                        type = "usb";
245 246
                    }

247 248 249 250 251 252
                    devices.push({
                        name: (block.label !== "") ? `${block.label} (${block.name})` : `${block.mount} (${block.name})`,
                        type,
                        path: block.mount
                    });
                }
253
            });
254 255

            this.render(devices);
256 257
        };

258
        this.render = async blockList => {
G
GitSquared 已提交
259
            if (this.failed === true) return false;
260 261

            document.getElementById("fs_disp_title_dir").innerText = this.dirpath;
262
            this.filesContainer.setAttribute("class", "");
263 264 265 266
            if (this._noTracking) {
                document.querySelector("section#filesystem > h3.title > p:first-of-type").innerText = "FILESYSTEM - TRACKING FAILED, RUNNING DETACHED FROM TTY";
            }

G
GitSquared 已提交
267
            let filesDOM = ``;
268
            blockList.forEach(e => {
G
GitSquared 已提交
269 270 271 272 273
                let hidden = "";
                if (e.name.startsWith(".")) {
                    hidden = " hidden";
                }

G
GitSquared 已提交
274
                let cmd = `window.term[window.currentTerm].write('\\'${e.name}\\'')`;
275
                if (e.type === "dir" || e.type === "up" || e.type.endsWith("Dir")) {
G
GitSquared 已提交
276
                    cmd = `window.term[window.currentTerm].writelr('cd \\'${e.name.replace("\\", "\\\\")}\\'')`;
G
GitSquared 已提交
277
                }
278

279 280 281 282
                if (e.type === "up") {
                    cmd = `window.term[window.currentTerm].writelr('cd ..')`;
                }

283
                if (e.type === "up" && this._noTracking) {
G
Git² 已提交
284
                    cmd = `window.fsDisp.readFS('${path.resolve(this.dirpath, '..').replace(/\\/g, '\\\\')}')`;
285
                }
286
                if ((e.type === "dir" || e.type.endsWith("Dir")) && this._noTracking) {
G
Git² 已提交
287
                    cmd = `window.fsDisp.readFS('${path.resolve(this.dirpath, e.name).replace(/\\/g, '\\\\')}')`;
288 289
                }

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
                if (e.type === "showDisks") {
                    cmd = `window.fsDisp.readDevices()`;
                }

                if (e.type === "disk" || e.type === "rom" || e.type === "usb") {
                    let extraSwitch = (process.platform === "win32") ? " /D" : "";
                    cmd = `window.term[window.currentTerm].writelr('cd${extraSwitch} \\'${e.path.replace("\\", "\\\\")}\\'')`;

                    document.getElementById("fs_disp_title_dir").innerText = "Showing available block devices";
                    this.filesContainer.setAttribute("class", "disks");
                }
                if ((e.type === "disk" || e.type === "rom" || e.type === "usb") && this._noTracking) {
                    cmd = `window.fsDisp.readFS('${e.path.replace(/\\/g, '\\\\')}')`;
                }

305 306 307
                if (e.type === "edex-theme") {
                    cmd = `window.themeChanger('${e.name.slice(0, -5)}')`;
                }
308 309 310
                if (e.type === "edex-kblayout") {
                    cmd = `window.remakeKeyboard('${e.name.slice(0, -5)}')`;
                }
G
GitSquared 已提交
311 312
                if (e.type === "edex-settings") {
                    cmd = `window.openSettings()`;
313
                }
G
GitSquared 已提交
314

G
GitSquared 已提交
315 316
                let icon = "";
                switch(e.type) {
317 318 319
                    case "showDisks":
                        icon = this.icons.showDisks;
                        break;
G
GitSquared 已提交
320 321 322 323 324 325 326 327 328 329 330 331
                    case "up":
                        icon = this.icons.up;
                        break;
                    case "dir":
                        icon = this.icons.dir;
                        break;
                    case "symlink":
                        icon = this.icons.symlink;
                        break;
                    case "file":
                        icon = this.icons.file;
                        break;
332 333 334 335 336 337 338 339 340
                    case "disk":
                        icon = this.icons.disk;
                        break;
                    case "rom":
                        icon = this.icons.rom;
                        break;
                    case "usb":
                        icon = this.icons.usb;
                        break;
341 342 343
                    case "edex-theme":
                        icon = this.icons.edex.theme;
                        break;
344 345 346
                    case "edex-kblayout":
                        icon = this.icons.edex.kblayout;
                        break;
347 348 349 350 351 352 353 354 355
                    case "edex-settings":
                        icon = this.icons.edex.settings;
                        break;
                    case "edex-themesDir":
                        icon = this.icons.edex.themesDir;
                        break;
                    case "edex-kblayoutsDir":
                        icon = this.icons.edex.kblayoutsDir;
                        break;
G
GitSquared 已提交
356 357 358 359 360
                    default:
                        icon = this.icons.other;
                        break;
                }

361
                filesDOM += `<div class="fs_disp_${e.type}${hidden} animationWait" onclick="${cmd}">
G
GitSquared 已提交
362
                                <svg viewBox="0 0 24.00 24.00">${icon}</svg>
G
GitSquared 已提交
363 364
                                <h3>${e.name}</h3>
                            </div>`;
365
            });
G
GitSquared 已提交
366
            this.filesContainer.innerHTML = filesDOM;
367

368 369
            if (this.filesContainer.getAttribute("class").endsWith("disks")) {
                document.getElementById("fs_space_bar").setAttribute("onclick", "window.fsDisp.render(window.fsDisp.cwd)");
G
Git² 已提交
370
            } else {
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
                document.getElementById("fs_space_bar").setAttribute("onclick", "");

                let splitter = (process.platform === "win32") ? "\\" : "/";
                let displayMount = (this.fsBlock.mount.length < 18) ? this.fsBlock.mount : "..."+splitter+this.fsBlock.mount.split(splitter).pop();

                // See #226
                if (!isNaN(this.fsBlock.use)) {
                    this.space_bar.text.innerHTML = `Mount <strong>${displayMount}</strong> used <strong>${Math.round(this.fsBlock.use)}%</strong>`;
                    this.space_bar.bar.value = Math.round(this.fsBlock.use);
                } else if (!isNaN((this.fsBlock.size / this.fsBlock.used) * 100)) {
                    let usage = Math.round((this.fsBlock.size / this.fsBlock.used) * 100);

                    this.space_bar.text.innerHTML = `Mount <strong>${displayMount}</strong> used <strong>${usage}%</strong>`;
                    this.space_bar.bar.value = usage;
                } else {
                    this.space_bar.text.innerHTML = "Could not calculate mountpoint usage.";
                    this.space_bar.bar.value = 100;
                }
G
Git² 已提交
389
            }
390 391 392 393 394 395 396 397 398 399 400 401 402

            function delay(ms) {
                return new Promise((resolve, reject) => {
                    setTimeout(resolve, ms);
                });
            }
            // Render animation
            let id = 0;
            while (this.filesContainer.childNodes[id]) {
                this.filesContainer.childNodes[id].setAttribute("class", this.filesContainer.childNodes[id].getAttribute("class").replace(" animationWait", ""));
                await delay(50);
                id++;
            }
403
        };
404 405 406

        // Automatically start indexing supposed beginning CWD
        // See #365
G
GitSquared 已提交
407 408 409 410 411
        // ...except if we're hot-reloading, in which case this can mess up the rendering
        // See #392
        if (window.performance.navigation.type === 0) {
            this.readFS(window.term[window.currentTerm].cwd || window.settings.cwd);
        }
412
    }
G
GitSquared 已提交
413
}
414 415 416 417

module.exports = {
    FilesystemDisplay
};