x-lang

← Index

x/sys/stream

Output redirection as first-class Streams: to-fd/to-file/stdout/stderr, (s with thunk)/write/display/close, plus the with-fd / with-file helpers.

Redirection is pure X – push/pop the base’s fileout fd (an atom in the io files group); no C primitive. File targets need the radon dialect.

Class Stream

A first-class output stream over a file descriptor: write to it, or redirect display/write output through it for the duration of a thunk.

Pure X: display/write target the base’s fileout fd (an atom in the io files group, whose rows the layout contract names); a Stream just pushes/pops that fd.

(Stream to-fd fd) wraps an existing fd (not owned). (Stream to-file path) opens path for writing and OWNS it – (close) closes it. (Stream stdout)/(Stream stderr) are conveniences.

File-backed streams (to-file, write, with-file) need the radon dialect (File open/close/write -> syscall/make-str).

fd

Member: data carried by a Stream instance.

owned?

Member: data carried by a Stream instance.

(Stream to-fd fd)

Wrap an already-open fd in a stream. Not owned – (close) is a no-op.

Parameters:

Returns: Stream — A stream writing to FD

(Stream to-file path)

Open PATH for writing (wronly creat trunc) and wrap its fd in an OWNED stream – (close) closes the file. Needs the radon dialect.

A newly created file gets mode 0644 (File open’s default perm); open via File directly if you need other permission bits.

Parameters:

Returns: Stream — An owned stream writing to PATH

(Stream stdout)

A stream onto stdout (fd 1).

Returns: Stream — stdout stream

(Stream stderr)

A stream onto stderr (fd 2).

Returns: Stream — stderr stream

(Stream output-fd)

The file descriptor display/write currently emit to.

Returns: INT — the current output fd

(Stream with-fd fd thunk)

Run THUNK with display/write redirected to FD, restoring the previous target afterward (even if THUNK errors).

Parameters:

Returns: ANY — THUNK’s result

(Stream with-file path thunk)

Open PATH for writing, run THUNK with all output going to it, then restore the previous output and close PATH (even if THUNK errors). The one-call way to save streamed output to a file.

Parameters:

Returns: ANY — THUNK’s result

(fd)

The file descriptor this stream writes to.

Instance method: called on a Stream instance.

Returns: INT — the fd

(with thunk)

Run THUNK with the current display/write output redirected to this stream, restoring afterward (even if THUNK errors).

Instance method: called on a Stream instance.

Parameters:

Returns: ANY — THUNK’s result

(write data size)

Write SIZE bytes of DATA directly to the stream’s fd. Needs the radon dialect.

Instance method: called on a Stream instance.

Parameters:

Returns: INT — Bytes written, or negative on error

(display v)

Render V (display form) to this stream.

Instance method: called on a Stream instance.

Parameters:

Returns: ANY — nil

(close)

Close the stream’s fd if it owns it (opened via to-file); a no-op for wrapped fds (to-fd/stdout/stderr).

Instance method: called on a Stream instance.

Returns: ANY — File close result, or nil when not owned