Skip to content

Commit

Permalink
Support coloured output for ls and grep commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Jul 18, 2024
1 parent 0b79075 commit e4bc80a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
21 changes: 17 additions & 4 deletions src/commands/coreutils_command_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ import { WasmCommandRunner } from "./wasm_command_runner"
export class CoreutilsCommandRunner extends WasmCommandRunner {
names(): string[] {
return [
"basename", "cat", "cp", "cut", "date", "dir", "dircolors", "dirname", "echo", "env", "expr",
"head", "id", "join", "ln", "logname", "ls", "md5sum", "mkdir", "mv", "nl", "pwd", "realpath",
"rm", "rmdir", "seq", "sha1sum", "sha224sum", "sha256sum", "sha384sum", "sha512sum", "sleep",
"sort", "stat", "stty", "tail", "touch", "tr", "tty", "uname", "uniq", "vdir", "wc",
"basename",
"cat", "chmod", "cp", "cut",
"date", "dir", "dircolors", "dirname",
"echo", "env", "expr",
"head",
"id",
"join",
"ln", "logname", "ls",
"md5sum", "mkdir", "mv",
"nl",
"pwd",
"realpath", "rm", "rmdir",
"seq", "sha1sum", "sha224sum", "sha256sum", "sha384sum", "sha512sum", "sleep", "sort", "stat", "stty",
"tail", "touch", "tr", "tty",
"uname", "uniq",
"vdir",
"wc",
]
}

Expand Down
3 changes: 2 additions & 1 deletion src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
export class Environment extends Map<string, string> {
constructor() {
super()
this.set("PS1", "\x1b[1;31mjs-shell:$\x1b[1;0m ") // red color
this.set("PS1", "\x1b[1;32mjs-shell:$\x1b[1;0m ")
this.set("TERM", "xterm-256color")
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/io/file_output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FileOutput extends BufferedOutput {
}
}

FS.writeFile(this.path, content)
FS.writeFile(this.path, content, { mode: 0o664 })
this.clear()
}
}
4 changes: 2 additions & 2 deletions tests/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Shell", () => {
it("should echo input and run ls command after \\r", async () => {
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 ")
expect(output.text).toMatch(/^ls\r\ndirA file1 file2\r\n/)
})
})

Expand All @@ -78,7 +78,7 @@ describe("Shell", () => {
it("should show tab completion options", async () => {
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")
expect(output.text).toMatch(/^e\r\necho env expr\r\n/)
})

it("should do nothing on unknown command", async () => {
Expand Down
22 changes: 14 additions & 8 deletions tests/shell_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@ export interface IShellSetup {
FS: any
}

export async function shell_setup_empty(): Promise<IShellSetup> {
return await _shell_setup_common(0)
export async function shell_setup_empty(wantColor: boolean = false): Promise<IShellSetup> {
return await _shell_setup_common(wantColor, 0)
}

export async function shell_setup_simple(): Promise<IShellSetup> {
return await _shell_setup_common(1)
export async function shell_setup_simple(wantColor: boolean = false): Promise<IShellSetup> {
return await _shell_setup_common(wantColor, 1)
}

async function _shell_setup_common(level: number): Promise<IShellSetup> {
async function _shell_setup_common(wantColor: boolean, level: number): Promise<IShellSetup> {
const output = new MockTerminalOutput(false)
const shell = new Shell(output.callback)
const fileSystem = await shell.initFilesystem()
const { FS } = fileSystem

if (!wantColor) {
// TODO: disable color in the prompt.
const { environment } = shell
environment.delete("TERM")
}

await shell.start()
output.start()

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

return { shell, output, fileSystem, FS }
Expand Down

0 comments on commit e4bc80a

Please sign in to comment.