Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Add ls command
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed May 9, 2023
1 parent 1a86af5 commit 0151b84
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
27 changes: 26 additions & 1 deletion src/ansi-shell/XDocumentPuterShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,30 @@ export class XDocumentPuterShell extends EventTarget {
$: 'config'
}, this.internal_.source);
}
}

async command (cmdName, command) {
const contentWindow = this.internal_.window;
let result;
const p = new Promise(rslv => {
const lis = evt => {
if ( evt.source !== contentWindow ) return;
if ( ! evt.data.$ ) {
console.error('message without $ when waiting');
return;
}
if ( evt.data.$ !== 'command-done' ) return;
result = evt.data.result;
rslv();
window.removeEventListener(lis);
}
window.addEventListener('message', lis);
});
contentWindow.postMessage({
...command,
$: 'command',
$$: cmdName,
}, this.internal_.source);
await p;
return result;
}
}
3 changes: 2 additions & 1 deletion src/ansi-shell/main_shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export const main_shell = async () => {
externs: {
config,
readline,
ptt
ptt,
puterShell,
},
registries: {
commands: command_registry,
Expand Down
4 changes: 3 additions & 1 deletion src/puter-shell/PuterANSIShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export class PuterANSIShell {
externs: {
in: ptt.in,
out: ptt.out,
puterShell: this.ctx.externs.puterShell,
},
locals: {
pwd: this.variables.pwd,
command,
args: tokens,
valid: true,
Expand All @@ -76,7 +78,7 @@ export class PuterANSIShell {
argProcessor.process(ctx, spec);
}
if ( ! ctx.locals.valid ) return;
await command.invoke(ctx);
await command.execute(ctx);
}

tokenize (cmdString) {
Expand Down
18 changes: 12 additions & 6 deletions src/puter-shell/coreutils/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ export default {
}
}
},
invoke: ctx => {
execute: async ctx => {
// ctx.params to access processed args
// ctx.args to access raw args
const { positionals, values, pwd } = ctx.locals;
const { puterShell } = ctx.externs;

const text = JSON.stringify({
positionals: ctx.locals.positionals,
values: ctx.locals.values,
});
ctx.externs.out.write(text + '\n');
const paths = positionals.length < 1
? [pwd] : positionals ;

for ( const path of paths ) {
const result = await puterShell.command('list', { path });
for ( const item of result ) {
ctx.externs.out.write(item.name + '\n');
}
}
}
};

0 comments on commit 0151b84

Please sign in to comment.