1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-08 08:33:32 +00:00

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 fs = require('fs');
const modpath = require('path'); 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', () => { process.once('loaded', () => {
// workspace root path // workspace root path
// reload() clears this, so we have to set it every time // reload() clears this, so we have to set it every time
@ -11,13 +41,12 @@ process.once('loaded', () => {
window.getWorkspaceFile = function(path, filetype) { window.getWorkspaceFile = function(path, filetype) {
if (wsroot == null) throw Error("no workspace root set"); if (wsroot == null) throw Error("no workspace root set");
try { try {
// TODO: detect text or binary, or decide based on incbin vs. source file
var fullpath = modpath.join(wsroot, modpath.normalize(path)); var fullpath = modpath.join(wsroot, modpath.normalize(path));
var encoding = filetype == 'text' ? 'utf8' : null; var data = fs.readFileSync(fullpath); // read binary
var data = fs.readFileSync(fullpath, {encoding:encoding}); var buf = new Uint8Array(data); // convert to array
if (encoding == null) data = new Uint8Array(data); var isBinary = filetype != 'text' || isProbablyBinary(path, buf);
console.log("getWorkspaceFile", path, filetype, data.length); data = isBinary ? buf : data.toString('utf-8');
// TODO: add to watched files console.log("getWorkspaceFile", path, isBinary, data.length);
return data; return data;
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View File

@ -349,6 +349,10 @@ function buildMenu() {
label: 'Follow @8bitworkshop on Twitter', label: 'Follow @8bitworkshop on Twitter',
click: openURL('https://twitter.com/8bitworkshop') 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', label: 'Become a Patreon',
click: openURL('https://www.patreon.com/8bitworkshop') click: openURL('https://www.patreon.com/8bitworkshop')

View File

@ -681,7 +681,7 @@ export class Toolbar {
} }
} }
newGroup() { 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) { add(key:string, alttext:string, icon:string, fn:(e,combo) => void) {
var btn = null; var btn = null;
@ -693,7 +693,7 @@ export class Toolbar {
btn.html(icon); btn.html(icon);
btn.prop("title", key ? (alttext+" ("+key+")") : alttext); btn.prop("title", key ? (alttext+" ("+key+")") : alttext);
btn.click(fn); btn.click(fn);
this.grp.append(btn); this.grp.append(btn).show();
} }
if (key) { if (key) {
this.mousetrap.bind(key, fn); this.mousetrap.bind(key, fn);