Files
termos/src/core/shell_state.ts
T
ChaosMaker 41739d7cf9 - Reworked internal signals
- Separated input passing from signal calling
- Exposed command registry to the broader codebase
- Moved command execution call to internal shell logic
- TTY is now expected to watch for stdout escape sequences to perform tasks
2026-11-23 13:31:22 +01:00

143 lines
4.3 KiB
TypeScript

import type { FileSystem } from "./filesystem/filesystem";
import type { NodePointer } from "./filesystem/node";
import { addHistoryEntry } from "./history_state";
import type { GroupInfo, UserInfo } from "./session_manager";
import { createInitialSessionState, getCurrentSession, type SessionState } from "./session_manager";
export interface ShellState {
readonly filesystem: FileSystem;
readonly users: ReadonlyMap<number, UserInfo>;
readonly groups: ReadonlyMap<number, GroupInfo>;
readonly sessionStack: ReadonlyArray<SessionState>;
//readonly commmandRegistry: CommandRegistry;
}
export function createInitialShellState(
filesystem: FileSystem,
users: ReadonlyMap<number, UserInfo>,
groups: ReadonlyMap<number, GroupInfo>,
user: UserInfo,
//commandRegistry: CommandRegistry,
): ShellState {
return Object.freeze({
filesystem,
users,
groups,
sessionStack: Object.freeze([createInitialSessionState(user)]),
//commandRegistry,
});
}
export function createShellState(
filesystem: FileSystem,
users: ReadonlyMap<number, UserInfo>,
groups: ReadonlyMap<number, GroupInfo>,
sessionStack: ReadonlyArray<SessionState>,
): ShellState {
return Object.freeze({
filesystem,
users,
groups,
sessionStack: Object.freeze(sessionStack),
});
}
export function withCwd(state: ShellState, ptr: NodePointer): ShellState {
const session = getCurrentSession(state);
const newSession = Object.freeze({
user: session.user,
envVars: Object.freeze(session.envVars),
history: Object.freeze(session.history),
cwd: ptr,
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,
groups: state.groups,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}
export function withHistory(state: ShellState, input: Uint8Array): 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,
groups: state.groups,
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,
groups: state.groups,
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,
groups: state.groups,
sessionStack: Object.freeze(sessionStack),
} satisfies ShellState);
}