diff --git a/plugins/plugin-bash-like/fs/src/lib/ls.ts b/plugins/plugin-bash-like/fs/src/lib/ls.ts index a49675a1534..0f10f03f11b 100644 --- a/plugins/plugin-bash-like/fs/src/lib/ls.ts +++ b/plugins/plugin-bash-like/fs/src/lib/ls.ts @@ -17,6 +17,7 @@ import { UTC as speedDate } from 'speed-date' import { i18n, + split, encodeComponent, Arguments, CodedError, @@ -247,8 +248,15 @@ function opt(o: keyof TheLsOptions, opts: Arguments) { * ls command handler * */ -const doLs = (cmd: string) => async (opts: Arguments): Promise => { - if (/\|/.test(opts.command)) { +const doLs = (cmd: string) => async (opts: Arguments): Promise => { + const pipeToGrep = opts.command.match(/\|\s*grep\s+([^|]+)$/) + const pipeToWordcount = /\|\s*wc\s+[^|]*$/.test(opts.command) + const pipeToGrepToWordcount = opts.command.match(/\|\s*grep\s+([\w*.]+)\s*\|\s*wc\s+[^|]*$/) + const pipeTo = !!pipeToGrepToWordcount || !!pipeToWordcount || !!pipeToGrep + let command = opts.command + if (pipeTo) { + command = command.slice(0, command.indexOf('|')) + } else if (/\|/.test(opts.command)) { // conservatively send possibly piped output to the PTY return opts.REPL.qexec(`sendtopty ${opts.command}`, opts.block) } @@ -262,7 +270,7 @@ const doLs = (cmd: string) => async (opts: Arguments): Promise async (opts: Arguments): Promise !/^-/.test(_))[0] + const pattern = new RegExp(grepFor.replace(/\*/g, '.*')) + const filteredEntries = entries.filter(_ => pattern.test(_.name)) + console.error('!!!!!!', filteredEntries, rest) + if (pipeToGrep) { + return toTable(filteredEntries, opts) + } else { + return filteredEntries.length + } + } else if (pipeToWordcount) { + return entries.length + } else { + return toTable(entries, opts) + } } const usage = (command: string) => ({ diff --git a/plugins/plugin-bash-like/src/test/bash-like/ls.ts b/plugins/plugin-bash-like/src/test/bash-like/ls.ts index b695a351344..99fffd839dd 100644 --- a/plugins/plugin-bash-like/src/test/bash-like/ls.ts +++ b/plugins/plugin-bash-like/src/test/bash-like/ls.ts @@ -43,6 +43,36 @@ describe(`directory listing ${process.env.MOCHA_RUN_TARGET || ''}`, function(thi .then(ReplExpect.okWith('package.json')) .catch(Common.oops(this))) + // pipe to wc + it('should use ls ../../package.json | wc -l', () => + CLI.command(`ls ../../package.json | wc -l`, this.app) + .then(ReplExpect.okWithString('1')) + .catch(Common.oops(this))) + + // pipe to grep to wc + it('should use ls ../../ | grep package.json | wc -l', () => + CLI.command(`ls ../../ | grep package.json | wc -l`, this.app) + .then(ReplExpect.okWithString('1')) + .catch(Common.oops(this))) + + // pipe to grep + it('should use ls ../../ | grep package.json', () => + CLI.command(`ls ../../ | grep package.json`, this.app) + .then(ReplExpect.okWithString('package.json')) + .catch(Common.oops(this))) + it('should use ls ../../ | grep p*', () => + CLI.command(`ls ../../ | grep p*`, this.app) + .then(ReplExpect.okWithString('package.json')) + .catch(Common.oops(this))) + it('should use ls -l ../../ | grep package.json', () => + CLI.command(`ls -l ../../ | grep package.json`, this.app) + .then(ReplExpect.okWith('package.json')) + .catch(Common.oops(this))) + it('should use ls -l ../../ | grep p*son', () => + CLI.command(`ls -l ../../ | grep p*son`, this.app) + .then(ReplExpect.okWith('package.json')) + .catch(Common.oops(this))) + const doListAndMetaClick = async () => { const holdDown = Keys.holdDownKey.bind(this) const release = Keys.releaseKey.bind(this)