From 37dffb0ad61646c78298a97a134cfcd694c55f99 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Mon, 31 Aug 2020 09:26:37 -0500 Subject: [PATCH] get rid of empty toolbars; electron: detect binary files --- electron-preload.js | 41 +++++++++++++++++++++++++++++++++++------ electron.js | 4 ++++ src/common/emu.ts | 4 ++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/electron-preload.js b/electron-preload.js index 0b5c82be..bfd6a435 100644 --- a/electron-preload.js +++ b/electron-preload.js @@ -3,6 +3,36 @@ const { ipcRenderer } = require('electron'); const fs = require('fs'); const modpath = require('path'); +function isProbablyBinary(path, data) { + var score = 0; + // decode as UTF-8 + for (var i = 0; i < (data?data.length:0);) { + let c = data[i++]; + if ((c & 0x80) == 0) { + // more likely binary if we see a NUL or obscure control character + if (c < 9 || (c >= 14 && c < 26) || c == 0x7f) { + score++; + break; + } + } else { + // look for invalid unicode sequences + var nextra = 0; + if ((c & 0xe0) == 0xc0) nextra = 1; + else if ((c & 0xf0) == 0xe0) nextra = 2; + else if ((c & 0xf8) == 0xf0) nextra = 3; + else if (c < 0xa0) score++; + else if (c == 0xff) score++; + while (nextra--) { + if (i >= data.length || (data[i++] & 0xc0) != 0x80) { + score++; + break; + } + } + } + } + return score > 0; +} + process.once('loaded', () => { // workspace root path // reload() clears this, so we have to set it every time @@ -11,13 +41,12 @@ process.once('loaded', () => { window.getWorkspaceFile = function(path, filetype) { if (wsroot == null) throw Error("no workspace root set"); try { - // TODO: detect text or binary, or decide based on incbin vs. source file var fullpath = modpath.join(wsroot, modpath.normalize(path)); - var encoding = filetype == 'text' ? 'utf8' : null; - var data = fs.readFileSync(fullpath, {encoding:encoding}); - if (encoding == null) data = new Uint8Array(data); - console.log("getWorkspaceFile", path, filetype, data.length); - // TODO: add to watched files + var data = fs.readFileSync(fullpath); // read binary + var buf = new Uint8Array(data); // convert to array + var isBinary = filetype != 'text' || isProbablyBinary(path, buf); + data = isBinary ? buf : data.toString('utf-8'); + console.log("getWorkspaceFile", path, isBinary, data.length); return data; } catch (e) { console.log(e); diff --git a/electron.js b/electron.js index ed4f23e1..c2aace2c 100644 --- a/electron.js +++ b/electron.js @@ -349,6 +349,10 @@ function buildMenu() { label: 'Follow @8bitworkshop on Twitter', click: openURL('https://twitter.com/8bitworkshop') }, + { + label: 'Browse Books on Amazon', + click: openURL('https://www.amazon.com/s?k=8bitworkshop&i=stripbooks&dc&qid=1598884483&tag=pzp-20') + }, { label: 'Become a Patreon', click: openURL('https://www.patreon.com/8bitworkshop') diff --git a/src/common/emu.ts b/src/common/emu.ts index 5288e951..19c63f20 100644 --- a/src/common/emu.ts +++ b/src/common/emu.ts @@ -681,7 +681,7 @@ export class Toolbar { } } newGroup() { - return this.grp = $(document.createElement("span")).addClass("btn_group").appendTo(this.span); + return this.grp = $(document.createElement("span")).addClass("btn_group").appendTo(this.span).hide(); } add(key:string, alttext:string, icon:string, fn:(e,combo) => void) { var btn = null; @@ -693,7 +693,7 @@ export class Toolbar { btn.html(icon); btn.prop("title", key ? (alttext+" ("+key+")") : alttext); btn.click(fn); - this.grp.append(btn); + this.grp.append(btn).show(); } if (key) { this.mousetrap.bind(key, fn);