Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History maxSize and ignore duplicates and leading whitespace #20

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export class History {
add(command: string) {
this._current = null

if (!command.trim()) {
// Do nothing with command that is all whitespace.
if (!command.trim() ||
(this._ignoreInitialWhitespace && command.startsWith(" ")) ||
(this._ignoreDuplicates && command == this.at(-1))) {
// Do not store.
return
}

if (this._history.length >= this._maxSize) {
this._history.shift()
}
Expand All @@ -22,6 +25,11 @@ export class History {
return this._history.at(index) ?? null
}

clear() {
this._current = null
this._history = []
}

scrollCurrent(next: boolean): string | null {
if (next) {
this._current = (this._current === null) ? null : this._current+1
Expand All @@ -40,6 +48,14 @@ export class History {
return this._current === null ? null : this.at(this._current)
}

setMaxSize(maxSize: number) {
this._maxSize = Math.max(maxSize, 0)
if (this._history.length > this._maxSize) {
this._current = null
this._history = this._history.slice(this._maxSize)
}
}

async write(output: IOutput): Promise<void> {
for (let i = 0; i < this._history.length; i++) {
const index = String(i).padStart(5, ' ')
Expand All @@ -49,7 +65,7 @@ export class History {

private _history: string[] = []
private _current: number | null = null

// Smnall number for debug purposes. Should probably get this from an env var.
private _maxSize: number = 5
private _ignoreInitialWhitespace: boolean = true
private _ignoreDuplicates: boolean = true
private _maxSize: number = 1000
}
2 changes: 1 addition & 1 deletion src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class Shell {
commandNode: CommandNode,
input: IInput,
output: IOutput,
): Promise<void> {
): Promise<void> {
const name = commandNode.name.value
const runner = CommandRegistry.instance().get(name)
if (runner === null) {
Expand Down
99 changes: 93 additions & 6 deletions tests/history.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { shell_setup_empty } from "./shell_setup"

// Not accessing the history object directly, here using the Shell.

describe("history", () => {
it("should be stored", async () => {
const { shell, output } = await shell_setup_empty()
Expand All @@ -16,8 +14,9 @@ describe("history", () => {
})

it("should limit storage to max size", async () => {
// TODO: Max size initially hard-coded as 5, will change to use env var.
const { shell, output } = await shell_setup_empty()
const { history } = shell
history.setMaxSize(5)

await shell._runCommands("cat")
await shell._runCommands("echo")
Expand All @@ -31,19 +30,107 @@ describe("history", () => {
" 0 echo\r\n 1 ls\r\n 2 uname\r\n 3 uniq\r\n 4 history\r\n")
})

it("should rerun previous command using !index syntax", async () => {
it("should clip history when reduce max size", async () => {
const { shell, output } = await shell_setup_empty()
const { history } = shell
history.setMaxSize(5)

await shell._runCommands("cat")
await shell._runCommands("echo")
await shell._runCommands("ls")
await shell._runCommands("uname")
await shell._runCommands("uniq")
output.clear()

history.setMaxSize(3)

await shell._runCommands("history")
expect(output.text).toEqual(" 0 uname\r\n 1 uniq\r\n 2 history\r\n")
})

it("should ignore duplicates", async () => {
const { shell, output } = await shell_setup_empty()

await shell._runCommands("cat")
await shell._runCommands("cat")
output.clear()

await shell._runCommands("history")
expect(output.text).toEqual(
" 0 cat\r\n 1 history\r\n")
})

it("should ignore commands starting with whitespace", async () => {
const { shell, output } = await shell_setup_empty()

await shell._runCommands(" ls")
output.clear()

await shell._runCommands("history")
expect(output.text).toEqual(
" 0 history\r\n")
})

it("should rerun previous command using !index syntax, negative and positive", async () => {
const { shell, output } = await shell_setup_empty()

await shell._runCommands("cat")
await shell._runCommands("echo hello")
await shell._runCommands("ls")
output.clear()

await shell._runCommands("!-2")
expect(output.text).toEqual("hello\r\n")
output.clear()

await shell._runCommands("!1")
expect(output.text).toEqual("hello\r\n")
})

// Need ! out of bounds
it("should handle !index out of bounds", async () => {
const { shell, output } = await shell_setup_empty()
const { history } = shell

await shell._runCommands("ls")
output.clear()

await shell._runCommands("!1")
expect(output.text).toMatch(/!1: event not found/)
})

it("should scroll up and down", async () => {
const { shell, output } = await shell_setup_empty()

await shell._runCommands("cat")
await shell._runCommands("echo hello")
await shell._runCommands("ls")
output.clear()

const upArrow = "\x1B[A"
const downArrow = "\x1B[B"

await shell.input(upArrow)
await shell.input(upArrow)
expect(output.text).toMatch(/echo hello$/)
output.clear()

await shell.input(upArrow)
expect(output.text).toMatch(/cat$/)
output.clear()

// Need up and down to be tested.
await shell.input(upArrow)
expect(output.text).toMatch(/cat$/)
output.clear()

await shell.input(downArrow)
expect(output.text).toMatch(/echo hello$/)
output.clear()

await shell.input(downArrow)
expect(output.text).toMatch(/ls$/)
output.clear()

await shell.input(downArrow)
expect(output.text).toMatch(/ $/)
})
})
Loading