From 0fce10895f5fae9c516874a68ad0bd9524a3b8cb Mon Sep 17 00:00:00 2001 From: hyperion Date: Fri, 24 Jul 2026 17:46:09 +0200 Subject: [PATCH] implementing the shellState sessionState, historyState implemented with helpers - no tests --- src/core/history_state.ts | 27 +++++++++++++++++++++++++++ src/core/session.ts | 8 ++++++++ src/core/shell_state.ts | 5 +---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/core/history_state.ts create mode 100644 src/core/session.ts diff --git a/src/core/history_state.ts b/src/core/history_state.ts new file mode 100644 index 0000000..ce27b5a --- /dev/null +++ b/src/core/history_state.ts @@ -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; +} diff --git a/src/core/session.ts b/src/core/session.ts new file mode 100644 index 0000000..be30d0a --- /dev/null +++ b/src/core/session.ts @@ -0,0 +1,8 @@ +interface SessionState { + readonly user: UserInfo; + readonly envVars: ReadonlyMap; + readonly history: HistoryState; + readonly cwd: number; + readonly pwd: number; + readonly exitCode: number; +} diff --git a/src/core/shell_state.ts b/src/core/shell_state.ts index dc9141f..289f484 100644 --- a/src/core/shell_state.ts +++ b/src/core/shell_state.ts @@ -3,8 +3,5 @@ import type { FileSystem } from "./filesystem/filesystem"; interface ShellState { readonly filesystem: FileSystem; readonly users: ReadonlyMap; - readonly currentUid: number; - readonly envVars: ReadonlyMap; - readonly cwd: number; - readonly pwd: number; + readonly sessionState: ReadonlyArray; }