ShellState, SessionState implemented, all tested, and passing

This commit is contained in:
2026-07-25 03:37:13 +02:00
parent 0fce10895f
commit 994b93ae47
7 changed files with 275 additions and 6 deletions
+112 -2
View File
@@ -1,7 +1,117 @@
import type { FileSystem } from "./filesystem/filesystem";
import { addHistoryEntry } from "./history_state";
import { createInitialSessionState, getCurrentSession, type SessionState } from "./session";
import type { UserInfo } from "./user_info";
interface ShellState {
export interface ShellState {
readonly filesystem: FileSystem;
readonly users: ReadonlyMap<number, UserInfo>;
readonly sessionState: ReadonlyArray<SessionState>;
readonly sessionStack: ReadonlyArray<SessionState>;
}
export function createInitialShellState(
filesystem: FileSystem,
users: ReadonlyMap<number, UserInfo>,
user: UserInfo,
): ShellState {
return Object.freeze({
filesystem,
users,
sessionStack: Object.freeze([createInitialSessionState(user)]),
});
}
export function withCwd(state: ShellState, inode: number): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(session.history),
cwd: inode,
pwd: session.cwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withHistory(state: ShellState, input: string): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(addHistoryEntry(session.history, input)),
cwd: session.cwd,
pwd: session.pwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withEnv(state: ShellState, key: string, value: string): ShellState {
const session = getCurrentSession(state);
const envVars = new Map(session.envVars);
envVars.set(key, value);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(envVars),
history: Object.freeze(session.history),
cwd: session.cwd,
pwd: session.pwd,
exitCode: session.exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withExitCode(state: ShellState, exitCode: number): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(session.history),
cwd: session.cwd,
pwd: session.pwd,
exitCode: exitCode,
} satisfies SessionState);
const sessionStack = [...state.sessionStack];
sessionStack[sessionStack.length - 1] = newSession;
return Object.freeze({
filesystem: state.filesystem,
users: state.users,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}