Skip to content

Commit

Permalink
Merge pull request #7 from jupyterlite/grep
Browse files Browse the repository at this point in the history
Add grep command
  • Loading branch information
ianthomas23 authored Jul 8, 2024
2 parents ab0cee8 + 8c2952f commit e6b9b80
Show file tree
Hide file tree
Showing 8 changed files with 5,059 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/command_registry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ICommandRunner } from "./commands/command_runner"
import { BuiltinCommandRunner } from "./commands/builtin_command_runner"
import { CoreutilsCommandRunner } from "./commands/coreutils_command_runner"
import { GrepCommandRunner } from "./commands/grep_command_runner"

export class CommandRegistry {
private constructor() {
this._commandRunners = [
new BuiltinCommandRunner(),
new CoreutilsCommandRunner(),
new GrepCommandRunner(),
]

// Command name -> runner mapping
Expand Down
12 changes: 12 additions & 0 deletions src/commands/grep_command_runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as GrepModule from "../wasm/grep"
import { WasmCommandRunner } from "./wasm_command_runner"

export class GrepCommandRunner extends WasmCommandRunner {
names(): string[] {
return ["grep"]
}

protected _getWasmModule(): any {
return GrepModule.default
}
}
5,021 changes: 5,021 additions & 0 deletions src/wasm/grep.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/commands/cat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { shell_setup_simple } from "../shell_setup"

describe("echo command", () => {
it("should write to stdout", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell._runCommands("cat file1")
expect(output.text).toEqual("Contents of the file\r\n")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { shell_setup_empty } from "../shell_setup"

describe("echo command", () => {
it("should write to stdout", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell._runCommands("echo some text")
expect(output.text).toEqual("some text\r\n")
})
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/ls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { shell_setup_empty, shell_setup_simple } from "../shell_setup"

describe("ls command", () => {
it("should write to stdout", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell._runCommands("ls")
expect(output.text).toEqual("dirA file1 file2\r\n")

Expand All @@ -12,7 +12,7 @@ describe("ls command", () => {
})

it("should handle empty listing", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell._runCommands("ls")
expect(output.text).toEqual("")
})
Expand Down
18 changes: 9 additions & 9 deletions tests/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@ import { shell_setup_empty, shell_setup_simple } from "./shell_setup"
describe("Shell", () => {
describe("._runCommands", () => {
it("should run ls command", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell._runCommands("ls")
expect(output.text).toEqual("dirA file1 file2\r\n")
})

it("should run ls command with leading whitespace", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell._runCommands(" ls")
expect(output.text).toEqual("dirA file1 file2\r\n")
})
})

describe("input", () => {
it("should echo input up to \\r", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell.inputs(["l", "s", " ", "-", "a", "l"])
expect(output.text).toEqual("ls -al")
})

it("should echo input and run ls command after \\r", async () => {
const [shell, output] = await shell_setup_simple()
const { shell, output } = await shell_setup_simple()
await shell.inputs(["l", "s", "\r"])
expect(output.text).toEqual("ls\r\ndirA file1 file2\r\n\x1b[1;31mjs-shell:$\x1b[1;0m ")
})
})

describe("input tab complete", () => {
it("should complete ec", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell.inputs(["e", "c", "\t"])
expect(output.text).toEqual("echo ")
})

it("should ignore leading whitespace", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell.inputs([" ", "e", "c", "\t"])
expect(output.text).toEqual(" echo ")
})

it("should ignore leading whitespace x2", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell.inputs([" ", " ", "e", "c", "\t"])
expect(output.text).toEqual(" echo ")
})

it("should show tab completion options", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell.inputs(["e", "\t"])
expect(output.text).toEqual("e\r\necho env expr\r\n\x1b[1;31mjs-shell:$\x1b[1;0m e")
})

it("should do nothing on unknown command", async () => {
const [shell, output] = await shell_setup_empty()
const { shell, output } = await shell_setup_empty()
await shell.inputs(["u", "n", "k", "\t"])
expect(output.text).toEqual("unk")
})
Expand Down
16 changes: 11 additions & 5 deletions tests/shell_setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { MockTerminalOutput } from "./util"
import { Shell } from "../src/shell"

export async function shell_setup_empty(): Promise<[Shell, MockTerminalOutput]> {
export interface IShellSetup {
shell: Shell
output: MockTerminalOutput
FS: any
}

export async function shell_setup_empty(): Promise<IShellSetup> {
return await _shell_setup_common(0)
}

export async function shell_setup_simple(): Promise<[Shell, MockTerminalOutput]> {
export async function shell_setup_simple(): Promise<IShellSetup> {
return await _shell_setup_common(1)
}

async function _shell_setup_common(level: number): Promise<[Shell, MockTerminalOutput]> {
async function _shell_setup_common(level: number): Promise<IShellSetup> {
const output = new MockTerminalOutput(false)
const shell = new Shell(output.callback)
const { FS } = await shell.initFilesystem()
Expand All @@ -19,9 +25,9 @@ async function _shell_setup_common(level: number): Promise<[Shell, MockTerminalO

if (level > 0) {
FS.writeFile('file1', 'Contents of the file');
FS.writeFile('file2', 'Some other file');
FS.writeFile('file2', 'Some other file\nSecond line');
FS.mkdir('dirA')
}

return [shell, output]
return { shell, output, FS }
}

0 comments on commit e6b9b80

Please sign in to comment.