Bash.Context (Bash v0.6.0)

Copy Markdown View Source

Unified execution context for defbash functions in both builtins and interop.

Manages process-dictionary-based I/O context with support for nesting (e.g., eval calling builtins). Provides stdout/stderr writing, stdin reading, state access, and delta-based state updates.

I/O Functions

Stdin

  • gets/0 - Read a line from stdin
  • read/1 - Read from stdin (:all, :line, or byte count)
  • stream/1 - Get stdin as a lazy stream

Output Streams

  • stream/2 - Stream an enumerable to stdout or stderr

State

Summary

Functions

Delete the current context and restore previous (for interop cleanup).

Write message to stderr with trailing newline.

Get the current session state.

Get accumulated state updates from the current context.

Read a line from stdin.

Write message to stdout with trailing newline.

Write to stdout or stderr.

Read from stdin.

Get stdin as a lazy stream.

Stream an enumerable to stdout or stderr.

Request state updates to be applied after execution completes.

Write raw data to stdout (no newline added).

Write raw data to stderr (no newline added).

Types

t()

@type t() :: %Bash.Context{state: map(), state_updates: map(), stdin: term()}

Functions

delete_context()

@spec delete_context() :: :ok

Delete the current context and restore previous (for interop cleanup).

error(message)

@spec error(String.t()) :: :ok

Write message to stderr with trailing newline.

get_state()

@spec get_state() :: map()

Get the current session state.

get_state_updates()

@spec get_state_updates() :: map()

Get accumulated state updates from the current context.

Used by interop's execute_with_context to retrieve deltas without going through finalize/1.

gets()

@spec gets() :: {:ok, String.t()} | :eof | {:error, term()}

Read a line from stdin.

Returns {:ok, line}, :eof, or {:error, reason}.

puts(message)

@spec puts(String.t()) :: :ok

Write message to stdout with trailing newline.

puts(atom, message)

@spec puts(:stdout | :stderr, String.t()) :: :ok

Write to stdout or stderr.

When called with a stream target and message:

  • puts(:stdout, message) - Write to stdout
  • puts(:stderr, message) - Write to stderr

read(n)

@spec read(:all | :line | non_neg_integer()) ::
  {:ok, String.t()} | :eof | {:error, term()}

Read from stdin.

Modes:

  • :all - Read all remaining input
  • :line - Read a single line (same as gets/0)
  • n (integer) - Read n bytes

stream(atom)

@spec stream(:stdin) :: Enumerable.t()

Get stdin as a lazy stream.

Returns an empty stream if no stdin is available.

stream(atom, enumerable)

@spec stream(:stdout | :stderr, Enumerable.t()) :: :ok

Stream an enumerable to stdout or stderr.

Each element is converted to a string and written immediately.

update_state(updates)

@spec update_state(keyword() | map()) :: :ok

Request state updates to be applied after execution completes.

Accumulates a delta map of update keys.

For more details on settings variables, see Bash.Variable. By default, a string key and string value will default to an exported variable.

Example

update_state(
  working_dir: "/new/path",
  variables: %{"PWD" => "/new/path"}
)

write(data)

@spec write(iodata()) :: :ok

Write raw data to stdout (no newline added).

write_stderr(data)

@spec write_stderr(iodata()) :: :ok

Write raw data to stderr (no newline added).