diff --git a/src/ansi-shell/XDocumentPuterShell.js b/src/ansi-shell/XDocumentPuterShell.js index b68fa3d..2b852b7 100644 --- a/src/ansi-shell/XDocumentPuterShell.js +++ b/src/ansi-shell/XDocumentPuterShell.js @@ -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; + } +} diff --git a/src/ansi-shell/main_shell.js b/src/ansi-shell/main_shell.js index d7c432b..412b13a 100644 --- a/src/ansi-shell/main_shell.js +++ b/src/ansi-shell/main_shell.js @@ -86,7 +86,8 @@ export const main_shell = async () => { externs: { config, readline, - ptt + ptt, + puterShell, }, registries: { commands: command_registry, diff --git a/src/puter-shell/PuterANSIShell.js b/src/puter-shell/PuterANSIShell.js index 3952f01..a598081 100644 --- a/src/puter-shell/PuterANSIShell.js +++ b/src/puter-shell/PuterANSIShell.js @@ -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, @@ -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) { diff --git a/src/puter-shell/coreutils/ls.js b/src/puter-shell/coreutils/ls.js index 1f85835..c2e3138 100644 --- a/src/puter-shell/coreutils/ls.js +++ b/src/puter-shell/coreutils/ls.js @@ -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'); + } + } } };