implementing the shellState sessionState, historyState implemented with helpers - no tests
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
const HISTORY_MAX_CAPACITY = 255;
|
||||||
|
|
||||||
|
export type HistoryState = readonly string[];
|
||||||
|
|
||||||
|
export function addHistoryEntry(state: HistoryState, input: string): HistoryState {
|
||||||
|
const entries: string[] = [...state];
|
||||||
|
|
||||||
|
if (entries.length === HISTORY_MAX_CAPACITY) {
|
||||||
|
entries.shift();
|
||||||
|
entries.push(input);
|
||||||
|
} else {
|
||||||
|
entries.push(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: readonly string[] = entries;
|
||||||
|
return Object.freeze(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getHistoryEntry(state: HistoryState, index: number): string {
|
||||||
|
const entry = state[index];
|
||||||
|
if (entry === undefined) {
|
||||||
|
// Add a custom error object later
|
||||||
|
throw Error(`HistoryState at index ${index} is undefined`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
interface SessionState {
|
||||||
|
readonly user: UserInfo;
|
||||||
|
readonly envVars: ReadonlyMap<string, string>;
|
||||||
|
readonly history: HistoryState;
|
||||||
|
readonly cwd: number;
|
||||||
|
readonly pwd: number;
|
||||||
|
readonly exitCode: number;
|
||||||
|
}
|
||||||
@@ -3,8 +3,5 @@ import type { FileSystem } from "./filesystem/filesystem";
|
|||||||
interface ShellState {
|
interface ShellState {
|
||||||
readonly filesystem: FileSystem;
|
readonly filesystem: FileSystem;
|
||||||
readonly users: ReadonlyMap<number, UserInfo>;
|
readonly users: ReadonlyMap<number, UserInfo>;
|
||||||
readonly currentUid: number;
|
readonly sessionState: ReadonlyArray<SessionState>;
|
||||||
readonly envVars: ReadonlyMap<string, string>;
|
|
||||||
readonly cwd: number;
|
|
||||||
readonly pwd: number;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user