Skip to content

Commit 2a32d9d

Browse files
It nneekereddddd. Tho, Malled, Yupupup.
1 parent 63cee1d commit 2a32d9d

1 file changed

Lines changed: 334 additions & 0 deletions

File tree

coms/extra.js

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,340 @@ log(`OUT: ${out_path}`);
147147
// this.ok();
148148
}//»
149149
}//»
150+
151+
/*Put these commands in their own libraries«
152+
153+
const com_parse = class extends Com{//«
154+
async run(){
155+
if (this.pipeFrom) return;
156+
let f;
157+
while (f = this.args.shift()){
158+
let node = await f.toNode(this.env.cwd);
159+
if (!node) {
160+
this.err(`not found: ${f}`);
161+
this.numErrors++;
162+
continue;
163+
}
164+
this.tryParse(await node.text);
165+
}
166+
this.numErrors?this.no():this.ok();
167+
}
168+
tryParse(val){
169+
try{
170+
this.out(JSON.parse(val));
171+
return true;
172+
}
173+
catch(e){
174+
this.err(e.message);
175+
this.numErrors++;
176+
return false;
177+
}
178+
}
179+
// pipeDone(lines){
180+
// this.tryParse(lines.join(""));
181+
// this.ok();
182+
// }
183+
// pipeIn(val){//Commented out
184+
// if (!isEOF(val)) this.tryParse(val);
185+
// else this.ok();
186+
// }
187+
}//»
188+
const com_stringify = class extends Com{//«
189+
init(){
190+
if (!this.pipeFrom) return this.no("expecting piped input");
191+
}
192+
run(){
193+
}
194+
tryStringify(val){
195+
try{
196+
this.out(JSON.stringify(val));
197+
return true;
198+
}
199+
catch(e){
200+
this.err(e.message);
201+
this.numErrors++;
202+
return false;
203+
}
204+
}
205+
// pipeDone(lines){
206+
// this.tryStringify(lines.join(""));
207+
// this.numErrors?this.no():this.ok();
208+
// }
209+
// pipeIn(val){//Commented out
210+
// if (!isEOF(val)) this.tryStringify(val);
211+
// else {
212+
// this.numErrors?this.no():this.ok();
213+
// this.ok();
214+
// }
215+
// }
216+
}//»
217+
218+
const com_workman = class extends Com{//«
219+
init(){}
220+
run(){//«
221+
222+
const OK_COMS=["close","move","resize","minimize","none"];
223+
const OK_VALS=["1","0","true","false","yes","no","okay","nope"];
224+
const{args, no}=this;
225+
let type = args.shift();
226+
if (!type) return no("Need a 'type' arg!");
227+
if (!(type==="w"||type==="s")){
228+
return no("The 'type' arg must be [w]indow or work[s]pace!");
229+
}
230+
let num = args.shift();
231+
let say_num = num;
232+
if (!num){
233+
return no("Need a 'number' arg!");
234+
}
235+
if (!num.match(/^[0-9]+$/)){
236+
return no(`Invalid number arg`);
237+
}
238+
let com = args.shift();
239+
if (!com) return no("Need a 'com' arg!");
240+
if (!OK_COMS.includes(com)){
241+
return no(`${com}: invalid 'com' arg`);
242+
}
243+
let val = args.shift();
244+
if (!val) return no("Need a 'val' arg!");
245+
if (!OK_VALS.includes(val)){
246+
return no(`${val}: invalid 'val' arg`);
247+
}
248+
if (val==="1"||val==="true"||val==="yes"||val==="okay") val = true;
249+
else val = false;
250+
com = "allow"+(com[0].toUpperCase() + com.slice(1));
251+
let say_type;
252+
if (type==="w"){//«
253+
254+
let elem = document.getElementById(`win_${num}`);
255+
if (!elem) return no(`${num}: window not found`);
256+
let win = elem._winObj;
257+
win[com] = val;
258+
say_type="Window";
259+
}//»
260+
else{//«
261+
num = parseInt(num);
262+
let workspaces = Desk.workspaces;
263+
if (num < 1 || num > workspaces.length){
264+
return no(`Need a workspace number 1->${workspaces.length}`);
265+
}
266+
let workspace = workspaces[num-1];
267+
if (!workspace) return no(`COULD NOT GET WORKSPACES[${num-1}]`);
268+
workspace[com] = val;
269+
say_type="Workspace";
270+
}//»
271+
272+
this.ok(`${say_type}[${say_num}].${com} = ${val}`);
273+
274+
}//»
275+
}//»
276+
const com_bindwin = class extends Com{//«
277+
static getOpts(){
278+
return {s:{d:3},l:{desc: 3}};
279+
}
280+
run(){
281+
const{args}=this;
282+
let numstr = args.shift();
283+
if (!numstr) return this.no(`expected a window id arg`);
284+
if (!numstr.match(/[0-9]+/)) return this.no(`${numstr}: invalid numerical argument`);
285+
let num = parseInt(numstr);
286+
let elem = document.getElementById(`win_${num}`);
287+
if (!elem) return this.no(`${numstr}: window not found`);
288+
let win = elem._winObj;
289+
if (!win) return this.no(`${numstr}: the window doesn't have an associated object!?!?`);
290+
if (win.ownedBy) return this.no("Cannot bind 'owned' windows!");
291+
let use_key = args.shift();
292+
if (!(use_key && use_key.match(/^[1-9]$/))) return this.no(`expected a 'key' arg (1-9)`);
293+
let desc = this.opts.desc || this.opts.d || win.appName;
294+
globals.boundWins[use_key] = {win, desc};
295+
win.bindNum = use_key;
296+
this.ok(`Alt+Shift+${use_key} -> win_${numstr}`);
297+
}
298+
}//»
299+
300+
const com_wat2wasm = class extends Com{//«
301+
302+
async init(){//«
303+
if (!window.WabtModule) {
304+
if (!await util.makeScript("/mods/util/libwabt.js")) {
305+
return this.no("Could not load 'libwabt'");
306+
}
307+
if (!window.WabtModule) return this.no("window.WabtModule does not exist!");
308+
}
309+
this.wabt = await window.WabtModule();
310+
}//»
311+
compile(text){//«
312+
//log(typeof text);
313+
let features = {};
314+
let outputLog = '';
315+
let outputBase64 = 'Error occured, base64 output is not available';
316+
317+
let binaryOutput;
318+
let binaryBlobUrl, binaryBuffer;
319+
let module;
320+
try {
321+
module = this.wabt.parseWat('test.wast', text, features);
322+
module.resolveNames();
323+
module.validate(features);
324+
let binaryOutput = module.toBinary({log: true, write_debug_names:true});
325+
outputLog = binaryOutput.log;
326+
binaryBuffer = binaryOutput.buffer;
327+
// binaryBuffer is a Uint8Array, so we need to convert it to a string to use btoa
328+
// https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string
329+
330+
// outputBase64 = btoa(String.fromCharCode.apply(null, binaryBuffer));
331+
332+
// let blob = new Blob([binaryBuffer]);
333+
this.out(binaryBuffer);
334+
// log(blob);
335+
//if (binaryBlobUrl) {
336+
// URL.revokeObjectURL(binaryBlobUrl);
337+
//}
338+
//binaryBlobUrl = URL.createObjectURL(blob);
339+
//downloadLink.setAttribute('href', binaryBlobUrl);
340+
//downloadEl.classList.remove('disabled');
341+
} catch (e) {
342+
outputLog += e.toString();
343+
cerr(outputLog);
344+
//downloadEl.classList.add('disabled');
345+
}
346+
finally {
347+
if (module) module.destroy();
348+
// outputEl.textContent = outputShowBase64 ? outputBase64 : outputLog;
349+
}
350+
//log(typeof text);
351+
}//»
352+
async run(){//«
353+
const{args, term}=this;
354+
//log();
355+
//let rv = await term.readLine("? ", {passwordMode: true});
356+
//let wabt = await WabtModule();
357+
358+
let fname = args.shift();
359+
if (!fname) return this.no("No filename!");
360+
let val = await fname.toText({cwd: term.cwd});
361+
if (!val) return this.no(`${fname}: nothing returned`);
362+
this.compile(val);
363+
this.ok();
364+
}//»
365+
366+
}//»
367+
368+
const com_markdown = class extends Com{//«
369+
#mod;
370+
async init(){
371+
let modret = await util.getMod("util.showdown");
372+
if (!modret) return this.no("No showdown module");
373+
this.#mod = modret.getmod();
374+
}
375+
async run(){
376+
const{args, term} = this;
377+
let converter = new this.#mod.Converter();
378+
let fname = args.shift();
379+
if (!fname) return this.no("Need a file name!");
380+
let str = await fname.toText(term);
381+
if (!str) return this.no("File not found!");
382+
let html = converter.makeHtml(str);
383+
this.out(html);
384+
this.ok();
385+
}
386+
}//»
387+
const com_html = class extends Com{//«
388+
init(){
389+
}
390+
async openWin(val){
391+
let win = await Desk.api.openApp("util.HTML", {appArgs: {text: val}});
392+
if (!win) return this.no(`util.HTML: app not found`);
393+
}
394+
async run(){
395+
const {args, opts, stdin} = this;
396+
if (stdin){
397+
this.openWin(stdin);
398+
this.ok();
399+
return;
400+
}
401+
else if (this.args.length){
402+
this.no("Only supporting stdin methods!");
403+
return;
404+
}
405+
else{
406+
// Do this method (like in com_math) to wait for EOF before opening the window
407+
// this.lines=[];
408+
}
409+
}
410+
pipeDone(lines){
411+
this.openWin(lines.join("\n"));
412+
//log(lines);
413+
this.ok();
414+
}
415+
//pipeIn(val){//Commented out
416+
// if (!isEOF(val)) this.openWin(val);
417+
// else this.ok();
418+
//}
419+
420+
}//»
421+
422+
const com_wget = class extends Com{//«
423+
//How github names their files within repos, and how to fetch them from LOTW«
424+
//
425+
//If this is the github repo:
426+
//https://github.com/mrdoob/three.js/
427+
//
428+
//The 'playground/' subdir of that repo is here:
429+
//https://github.com/mrdoob/three.js/tree/dev/playground
430+
//
431+
//This is the 'playground/index.html' file:
432+
//https://github.com/mrdoob/three.js/blob/dev/playground/index.html
433+
//
434+
//This is that same file in its raw form, but it is an invalid fetch:
435+
//https://github.com/mrdoob/three.js/raw/refs/heads/dev/playground/index.html
436+
//
437+
//And this is the same file that is okay to fetch:
438+
//https://raw.githubusercontent.com/mrdoob/three.js/refs/heads/dev/playground/index.html
439+
//
440+
//This gets the entire directory structure (but the output is sent to the console as Uint8Array,
441+
//rather than text, since there is no indication in the URL that the returned value is JSON):
442+
//https://api.github.com/repos/mrdoob/three.js/git/trees/refs/heads/dev?recursive=true
443+
//
444+
//»
445+
static getOpts(){
446+
return {l: {dl: 1, local: 1}, s: {l: 1}};
447+
}
448+
async run(){
449+
const {args, opts} = this;
450+
let patharg = args.shift();
451+
if (!patharg) return this.no("missing URL");
452+
if (args.length) return this.no("too many arguments");
453+
let url;
454+
if (opts.local || opts.l) url = `/_get?path=${encodeURIComponent(patharg)}`;
455+
else url = patharg;
456+
// if (!nodejs_mode || opts["local"]) url = patharg
457+
// else url = `/_get?path=${encodeURIComponent(patharg)}`;
458+
let rv;
459+
try{
460+
rv = await fetch(url)
461+
}
462+
catch(e){
463+
cerr(e);
464+
this.no(`${e.message} (see console)`);
465+
return;
466+
}
467+
if (!rv.ok){
468+
this.no(`Response code: ${rv.status} (${rv.statusText})`);
469+
log(rv);
470+
return;
471+
}
472+
let buf = await rv.arrayBuffer();
473+
if (this.opts.dl){
474+
let fname = (new URL(patharg)).pathname.split("/").pop();
475+
if (!fname) fname = "WGET-OUT.bin"
476+
util.download(new Blob([buf]), fname);
477+
}
478+
else this.out(new Uint8Array(buf));
479+
this.ok();
480+
}
481+
482+
}//»
483+
»*/
150484
/*
151485
const com_walt = async (args, opts) => {//«
152486
let {term}=opts;

0 commit comments

Comments
 (0)