first commit
This commit is contained in:
46
xterminal/source/history/index.ts
Normal file
46
xterminal/source/history/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { isArray } from "../helpers";
|
||||
import type { IHistory } from "./interface";
|
||||
|
||||
/**
|
||||
* History stack
|
||||
*/
|
||||
export default class XHistory implements IHistory {
|
||||
private store;
|
||||
private ptr;
|
||||
|
||||
constructor(initialState: string[] = []) {
|
||||
this.store = isArray(initialState) ? initialState : [];
|
||||
this.ptr = -1;
|
||||
}
|
||||
|
||||
private get size(): number {
|
||||
return this.store.length;
|
||||
}
|
||||
|
||||
public get list(): string[] {
|
||||
return [].slice.call(this.store).reverse();
|
||||
}
|
||||
|
||||
add(input: string): void {
|
||||
if (input && input !== this.store[0]) {
|
||||
this.store.unshift(input);
|
||||
}
|
||||
this.ptr = -1;
|
||||
}
|
||||
|
||||
previous(): string {
|
||||
this.ptr++;
|
||||
if (this.ptr >= this.size) this.ptr = this.size - 1;
|
||||
return this.store[this.ptr] || "";
|
||||
}
|
||||
|
||||
next(): string {
|
||||
this.ptr--;
|
||||
if (this.ptr <= -1) this.ptr = -1;
|
||||
return this.store[this.ptr] || "";
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.store.splice(0);
|
||||
}
|
||||
}
|
||||
33
xterminal/source/history/interface.ts
Normal file
33
xterminal/source/history/interface.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Interface: History
|
||||
*/
|
||||
export interface IHistory {
|
||||
/**
|
||||
* Array containing a copy of entries
|
||||
*/
|
||||
list: string[];
|
||||
|
||||
/**
|
||||
* Getter: access one entry at a time (forward)
|
||||
*/
|
||||
next(): string;
|
||||
|
||||
/**
|
||||
* Getter: access one entry at a time (backwards)
|
||||
*/
|
||||
previous(): string;
|
||||
|
||||
/**
|
||||
* Insert an input string to the stack
|
||||
*
|
||||
* Returns `false` if the `input` is the same as the previous entry
|
||||
*
|
||||
* @returns boolean
|
||||
*/
|
||||
add(input: string): void;
|
||||
|
||||
/**
|
||||
* Empty the stack of entries
|
||||
*/
|
||||
clear(): void;
|
||||
}
|
||||
Reference in New Issue
Block a user