Skip to content

Commit

Permalink
fix(plugins/plugin-bash-like): ls | grep or ls | wc do not work
Browse files Browse the repository at this point in the history
Fixes #6984
  • Loading branch information
starpit committed Feb 8, 2021
1 parent 8b54aca commit 9a6a1e5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
31 changes: 27 additions & 4 deletions plugins/plugin-bash-like/fs/src/lib/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { UTC as speedDate } from 'speed-date'
import {
i18n,
split,
encodeComponent,
Arguments,
CodedError,
Expand Down Expand Up @@ -247,8 +248,15 @@ function opt(o: keyof TheLsOptions, opts: Arguments<LsOptions>) {
* ls command handler
*
*/
const doLs = (cmd: string) => async (opts: Arguments<LsOptions>): Promise<MixedResponse | Table> => {
if (/\|/.test(opts.command)) {
const doLs = (cmd: string) => async (opts: Arguments<LsOptions>): Promise<number | MixedResponse | Table> => {
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)
}
Expand All @@ -262,7 +270,7 @@ const doLs = (cmd: string) => async (opts: Arguments<LsOptions>): Promise<MixedR
//
// NOTE 2: The 2nd regexp assumes that `ls` takes only boolean options.
//
const srcs = opts.command.replace(/^\s*ls/, '').replace(/\s--?\S+/g, '')
const srcs = command.replace(/^\s*ls/, '').replace(/\s--?\S+/g, '')

const cmdline = 'vfs ls ' + (opts.parsedOptions.l || cmd === 'lls' ? '-l ' : '') + opt('d', opts) + srcs

Expand All @@ -288,7 +296,22 @@ const doLs = (cmd: string) => async (opts: Arguments<LsOptions>): Promise<MixedR
}
}

return toTable(entries, opts)
if (pipeToGrep || pipeToGrepToWordcount) {
const rest = pipeToGrepToWordcount ? pipeToGrepToWordcount[1] : pipeToGrep[1]
const grepFor = split(rest).filter(_ => !/^-/.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) => ({
Expand Down
30 changes: 30 additions & 0 deletions plugins/plugin-bash-like/src/test/bash-like/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9a6a1e5

Please sign in to comment.