From 115e8e23d20aaaa694212ef996f05b57bb57bdbd Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 15 May 2023 21:53:04 -0400 Subject: [PATCH] Improve ls command --- src/puter-shell/coreutils/ls.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/puter-shell/coreutils/ls.js b/src/puter-shell/coreutils/ls.js index cb583e1..81d85df 100644 --- a/src/puter-shell/coreutils/ls.js +++ b/src/puter-shell/coreutils/ls.js @@ -1,3 +1,6 @@ +// INCONST: called 'path' instead of 'path_' elsewhere +import path_ from "path-browserify"; + export default { name: 'ls', args: { @@ -18,8 +21,25 @@ export default { const paths = positionals.length < 1 ? [pwd] : positionals ; + + + // DRY: also done in mkdir, cat, and mv + const resolve = relPath => { + if ( relPath.startsWith('/') ) { + return relPath; + } + return path_.resolve(ctx.vars.pwd, relPath); + } + + const showHeadings = paths.length > 1 ? async ({ i, path }) => { + if ( i !== 0 ) ctx.externs.out.write('\n'); + await ctx.externs.out.write(path + ':\n'); + } : () => {}; - for ( const path of paths ) { + for ( let i=0 ; i < paths.length ; i++ ) { + let path = paths[i]; + await showHeadings({ i, path }); + path = resolve(path); const result = await puterShell.command('list', { path }); for ( const item of result ) { await ctx.externs.out.write(item.name + '\n');