Skip to content

Class HeadlessShell ​

Source: shell.ts

A headless Posh shell.

Example ​

ts
const shell = new HeadlessShell();
const input = process.argv.slice(2).map(Shell.quote).join(" ");

const exitCode = await shell.start(input);
process.exit(exitCode);

Extends ​

Constructors ​

Constructor ​

new HeadlessShell(): HeadlessShell

Returns ​

HeadlessShell

Overrides ​

Shell.constructor

Properties ​

state ​

state: ShellState

The reactive state of this shell.

Inherited from ​

Shell.state


config ​

config: ShellConfig

The configuration used to initialize this shell.

Inherited from ​

Shell.config


env ​

env: ShellEnvironment

The environment variable manager for this shell.

Inherited from ​

Shell.env


interpreter ​

interpreter: ShellInterpreter

The logic handler for parsing and executing command strings.

Inherited from ​

Shell.interpreter


MISSING_ARGS_ERROR ​

readonly static MISSING_ARGS_ERROR: "requires at least 1 argument" = "requires at least 1 argument"

Inherited from ​

Shell.MISSING_ARGS_ERROR


MISSING_OPTIONS_ERROR ​

readonly static MISSING_OPTIONS_ERROR: "requires at least 1 option" = "requires at least 1 option"

Inherited from ​

Shell.MISSING_OPTIONS_ERROR


COMMAND_FAILED_ERROR ​

readonly static COMMAND_FAILED_ERROR: "Command failed" = "Command failed"

Inherited from ​

Shell.COMMAND_FAILED_ERROR


USAGE_ERROR ​

readonly static USAGE_ERROR: "Incorrect usage" = "Incorrect usage"

Inherited from ​

Shell.USAGE_ERROR


INVALID_PATH_ERROR ​

readonly static INVALID_PATH_ERROR: "No such file or directory" = "No such file or directory"

Inherited from ​

Shell.INVALID_PATH_ERROR


SUDO_COMMAND ​

readonly static SUDO_COMMAND: "sudo" = "sudo"

Inherited from ​

Shell.SUDO_COMMAND

Methods ​

start() ​

start(input?: string): Promise<number>

Executes the given command string or starts interactive mode if the command string is empty.

Parameters ​

input? ​

string

The input string.

Returns ​

Promise<number>

The exit code.


startCommand() ​

startCommand(input: string): Promise<number>

Executes the given command string.

Parameters ​

input ​

string

The input string.

Returns ​

Promise<number>

The exit code.


startInteractiveMode() ​

startInteractiveMode(): Promise<number>

Starts interactive mode.

Returns ​

Promise<number>

The exit code.


setCursorVisible() ​

setCursorVisible(visible: boolean): void

Sets the visible state of the cursor.

Parameters ​

visible ​

boolean

The state to set.

Returns ​

void


handleKeyDown() ​

handleKeyDown(event: Pick<KeyboardEvent<Element>, "key" | "preventDefault"> & Partial<Pick<KeyboardEvent<Element>, "ctrlKey" | "metaKey" | "shiftKey">>): Promise<void>

Parameters ​

event ​

Pick<KeyboardEvent<Element>, "key" | "preventDefault"> & Partial<Pick<KeyboardEvent<Element>, "ctrlKey" | "metaKey" | "shiftKey">>

Returns ​

Promise<void>

Inherited from ​

Shell.handleKeyDown


setRawMode() ​

setRawMode(rawMode: boolean): void

Toggles raw mode, which forwards all keystrokes directly to the active stream.

Parameters ​

rawMode ​

boolean

Whether to enable raw mode.

Returns ​

void

Inherited from ​

Shell.setRawMode


terminate() ​

terminate(signal?: StreamSignal): void

Sends a signal to the shell or the currently active foreground process.

Parameters ​

signal? ​

StreamSignal = "SIGTERM"

The signal to send. Defaults to "SIGTERM".

Returns ​

void

Inherited from ​

Shell.terminate


interrupt() ​

interrupt(): void

Convenience method to send the SIGINT (Interrupt) signal.

Returns ​

void

See ​

Shell.terminate

Inherited from ​

Shell.interrupt


kill() ​

kill(): void

Convenience method to send the SIGKILL (Kill) signal, closing the shell.

Returns ​

void

See ​

Shell.terminate

Inherited from ​

Shell.kill


updatePrompt() ​

updatePrompt(): Promise<void>

Refreshes the prompt string based on the current user, hostname, and working directory.

Returns ​

Promise<void>

Inherited from ​

Shell.updatePrompt


setLine() ​

setLine(value: string | ((prev: string) => string)): void

Updates the text in the current input line.

Parameters ​

value ​

string | ((prev: string) => string)

The new string value or a function that receives the previous value.

Returns ​

void

Inherited from ​

Shell.setLine


pushHistory() ​

pushHistory(entry: HistoryEntry): void

Appends a new entry to the terminal history.

Parameters ​

entry ​

HistoryEntry

The history entry to add.

Returns ​

void

Inherited from ​

Shell.pushHistory


write() ​

write(text: string): void

Writes raw text to the shell, handling ANSI escape codes for screen clearing and alt buffers.

Parameters ​

text ​

string

The string data to write to the TTY.

Returns ​

void

Inherited from ​

Shell.write


run() ​

run(input: string): Promise<number>

Commits the current input line and executes the given command string.

Parameters ​

input ​

string

The command string to execute.

Returns ​

Promise<number>

A promise that resolves with the final exit code of the execution.

Inherited from ​

Shell.run


commitLine() ​

commitLine(input?: string): void

Commits the current line or the given input string.

Parameters ​

input? ​

string = ...

The input string.

Returns ​

void

Inherited from ​

Shell.commitLine


clearLine() ​

clearLine(): void

Clears the current input line and resets the history search offset.

Returns ​

void

Inherited from ​

Shell.clearLine


historySearch() ​

historySearch(direction: number): void

Navigates through command history.

Parameters ​

direction ​

number

Positive to go back in time, negative to go forward.

Returns ​

void

Inherited from ​

Shell.historySearch


setWorkingDirectory() ​

setWorkingDirectory(directory: VirtualFolder): void

Changes the current working directory and updates PWD/OLDPWD environment variables.

Parameters ​

directory ​

VirtualFolder

The virtual folder to switch to.

Returns ​

void

Inherited from ​

Shell.setWorkingDirectory


getCompletions() ​

getCompletions(): string[]

Calculates possible completions for the current input based on commands and file paths.

Returns ​

string[]

An array of string suggestions.

Inherited from ​

Shell.getCompletions


autoComplete() ​

autoComplete(): void

Performs an auto-completion action. If one match is found, it completes the line. If multiple are found, it lists them in the history.

Returns ​

void

Inherited from ​

Shell.autoComplete


readRawInput() ​

readRawInput(stdin: Stream, onData: (data: string) => void | Promise<void>): Promise<void>

Sets the shell to raw mode, listens for input on stdin.

Parameters ​

stdin ​

Stream

The standard input stream.

onData ​

(data: string) => void | Promise<void>

The callback to handle incoming data chunks.

Returns ​

Promise<void>

Inherited from ​

Shell.readRawInput


readFiles() ​

readFiles(params: { paths: string[]; workingDirectory: VirtualFolder; commandName: string; onContent: (content: string) => void | Promise<void>; onStdinData: (data: string) => void | Promise<void>; } & Pick<ProcessIO, "stdin" | "stderr">): Promise<number>

Processes a list of paths, falling back to standard input if the list is empty or contains "-".

Parameters ​

params ​

{ paths: string[]; workingDirectory: VirtualFolder; commandName: string; onContent: (content: string) => void | Promise<void>; onStdinData: (data: string) => void | Promise<void>; } & Pick<ProcessIO, "stdin" | "stderr">

Returns ​

Promise<number>

Inherited from ​

Shell.readFiles


writeError() ​

static writeError(stream: Stream, commandName: string, error?: string | string[], exitCode?: number): Promise<number>

Utility to write a formatted error message to a stream and return an exit code.

Parameters ​

stream ​

Stream

The stream to receive the error output.

commandName ​

string

The name of the command reporting the error.

error? ​

string | string[]

A single string or array of strings representing the error message.

exitCode? ​

number = EXIT_CODE.generalError

The numerical exit code to return.

Returns ​

Promise<number>

exitCode.

Inherited from ​

Shell.writeError


loop() ​

static loop(__namedParameters: Pick<ProcessIO, "stdin" | "stdout"> & { task: () => string; delay?: number; maxIterations?: number; }): Promise<0>

Executes a task repeatedly and writes its output to stdout.

Parameters ​

__namedParameters ​

Pick<ProcessIO, "stdin" | "stdout"> & { task: () => string; delay?: number; maxIterations?: number; }

Returns ​

Promise<0>

A promise that resolves when the loop is finished.

Inherited from ​

Shell.loop


animate() ​

static animate(__namedParameters: Pick<ProcessIO, "stdin" | "stdout"> & { render: (frame: number) => string; delay: number; clear?: boolean; stopOnBlank?: boolean; useAltBuffer?: boolean; }): Promise<0>

Executes a frame-based animation in the terminal using the Alternate Screen Buffer.

Parameters ​

__namedParameters ​

Pick<ProcessIO, "stdin" | "stdout"> & { render: (frame: number) => string; delay: number; clear?: boolean; stopOnBlank?: boolean; useAltBuffer?: boolean; }

Returns ​

Promise<0>

A promise that resolves when the animation is stopped.

Inherited from ​

Shell.animate


readInput() ​

static readInput(rawLine: string, stdin: Stream, callback: (data: string) => CommandOutput | Promise<CommandOutput>): Promise<CommandOutput>

Reads input data. If rawLine is provided, it processes it immediately via the callback. Otherwise, it waits for data from the stdin stream.

Parameters ​

rawLine ​

string

The pre-provided input string.

stdin ​

Stream

The input stream to fall back on.

callback ​

(data: string) => CommandOutput | Promise<CommandOutput>

Function to process the collected input data.

Returns ​

Promise<CommandOutput>

Inherited from ​

Shell.readInput


printLn() ​

static printLn(stream: Stream, text?: string): Promise<void>

Utility function that writes text followed by a newline ("\n") to stream.

Parameters ​

stream ​

Stream

The stream to write to.

text? ​

string = ""

The text to write.

Returns ​

Promise<void>

Inherited from ​

Shell.printLn


quote() ​

static quote(text: string): string

Parameters ​

text ​

string

Returns ​

string

Inherited from ​

Shell.quote

Accessors ​

isInteractive ​

Get Signature ​

get isInteractive(): boolean

Checks if this shell is running in interactive mode.

Returns ​

boolean

true if this shell is running in interactive mode.


isCursorVisible ​

Get Signature ​

get isCursorVisible(): boolean

Checks if the cursor is currently visible.

Returns ​

boolean

true if the cursor is currently visible.

Built by Prozilla