get rid of empty toolbars; electron: detect binary files

This commit is contained in:
Steven Hugg 2020-08-31 09:26:37 -05:00
parent ba73c5f569
commit 37dffb0ad6
3 changed files with 41 additions and 8 deletions

View File

@ -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);

View File

@ -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')

View File

@ -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);