Skip to main content

Terminal

Struct Terminal 

Source
pub struct Terminal<I, O> { /* private fields */ }
Expand description

Owned terminal handle pairing input, output, environment, and raw-mode state.

Terminal implements Read from I and Write to O. The handle is generic so callers can use inherited stdio, the controlling terminal, or test doubles, while sharing one raw-mode and window-size API on supported platforms.

Implementations§

Source§

impl Terminal<Stdin, Stdout>

Source

pub fn stdio() -> Self

Create a terminal over inherited standard input and output.

The returned handle uses stdin for input, stdout for output, and Env::from_process for its environment snapshot. Use this when the process is expected to be connected directly to the terminal.

§Returns

A Terminal<Stdin, Stdout> with no saved raw-mode state.

§Errors and panics

This constructor does not fail or intentionally panic.

§Usage note

If stdin or stdout may be redirected, prefer Terminal::open to open the controlling terminal directly.

Source§

impl Terminal<TtyInput, TtyOutput>

Source

pub fn open() -> Result<Self>

Open the controlling terminal directly.

On Unix this opens /dev/tty for both input and output. On Windows it opens CONIN$ for input and CONOUT$ for output. The returned Terminal snapshots the process environment with Env::from_process.

§Returns

A Terminal<TtyInput, TtyOutput> backed by the controlling terminal.

§Errors

Returns the error from open_tty if the process has no controlling terminal or if the platform device cannot be opened.

§Panics

This function does not intentionally panic.

Source§

impl<I, O> Terminal<I, O>

Source

pub fn env(&self) -> &Env

Return the captured environment snapshot.

The snapshot is taken by the constructor and is not updated if the process environment later changes.

§Returns

A shared reference to the terminal’s Env.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn get_env(&self, key: &str) -> Option<String>

Look up an environment variable in the captured snapshot.

§Parameters
  • key — environment variable name.
§Returns

The last captured value for key, or None if it is absent.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn has_env(&self, key: &str) -> bool

Return whether an environment variable is present and non-empty.

§Parameters
  • key — environment variable name.
§Returns

true when the captured snapshot contains key with a non-empty value.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn input(&self) -> I
where I: Copy,

Return a copy of the input half.

This is available only when I: Copy, which is true for the standard and controlling-terminal handle types provided by this module. Use it to pass input to EventSource::new while retaining the Terminal for raw-mode restoration.

§Returns

A copy of the input handle.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn output(&self) -> O
where O: Copy,

Return a copy of the output half.

This is available only when O: Copy, which is true for the standard and controlling-terminal output types provided by this module. Use it to pass output to a renderer such as TextBuffer while retaining the Terminal for raw-mode restoration.

§Returns

A copy of the output handle.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn into_halves(self) -> (I, O)

Consume the terminal and return its input and output halves.

Any cached raw-mode state is dropped without being applied. Restore before calling this method if the terminal is in raw mode.

§Returns

(input, output).

§Errors and panics

This method does not fail or intentionally panic.

Source§

impl<I: AsFd, O: AsFd> Terminal<I, O>

Source

pub fn new(input: I, output: O, env: Env) -> Self

Build a terminal from input and output descriptors plus an Env.

The environment is stored exactly as provided and is not required to describe the given descriptors. Use Terminal::stdio or Terminal::open to snapshot the process environment automatically.

§Parameters
  • input — readable terminal descriptor.
  • output — writable terminal descriptor.
  • env — environment snapshot associated with this terminal.
§Returns

A Terminal with no saved raw-mode state.

§Errors and panics

This constructor does not fail or intentionally panic.

Source

pub fn make_raw(&mut self) -> Result<State>

Put the terminal into raw mode and save the previous state.

This calls make_raw_mode with the terminal’s input and output descriptors. The returned pre-raw State is cloned into the terminal so restore can later apply it without an argument.

§Returns

The state that was active before raw mode was applied.

§Errors

Returns any error from reading the current state or applying the raw state.

§Panics

This method does not intentionally panic.

Source

pub fn restore(&mut self) -> Result<()>

Restore the state cached by the most recent make_raw.

If a state is cached, it is applied with set_state and then cleared. If no state is cached, this is a no-op.

§Returns

Ok(()) when no state was cached or restoration succeeded.

§Errors

Returns any error from applying the cached state.

§Panics

This method does not intentionally panic.

Source

pub fn get_state(&self) -> Result<State>

Snapshot the current terminal mode.

This reads the terminal state without modifying the cached state used by restore.

§Returns

The current State.

§Errors

Returns any error from get_state.

§Panics

This method does not intentionally panic.

Source

pub fn set_state(&self, state: &State) -> Result<()>

Apply a previously snapshotted terminal mode.

This does not update or clear the state cached by make_raw. Use it for manual state management; use restore for the terminal-owned raw-mode lifecycle.

§Parameters
  • state — state to apply to the terminal.
§Returns

Ok(()) when the state was applied.

§Errors

Returns any error from set_state.

§Panics

This method does not intentionally panic.

Source

pub fn is_terminal(&self) -> (bool, bool)

Report whether the input and output halves are terminals.

§Returns

(input_is_terminal, output_is_terminal).

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn get_window_size(&self) -> Result<Winsize>

Query the current terminal window size.

On Unix this tries the output descriptor first and falls back to the input descriptor if the output query fails. If both fail, the output descriptor’s error is returned.

§Returns

The current window size in cells and, when reported by the platform, pixels.

§Errors

Returns an OS error if the size cannot be queried from either half.

§Panics

This method does not intentionally panic.

Trait Implementations§

Source§

impl<I: AsFd, O> AsFd for Terminal<I, O>

Available on Unix only.
Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrow the input descriptor.

This exposes the input half, not the output half. Use output and borrow that handle when an output descriptor is required.

Source§

impl<I: Read, O> Read for Terminal<I, O>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

impl<I, O: Write> Write for Terminal<I, O>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<I, O> Freeze for Terminal<I, O>
where I: Freeze, O: Freeze,

§

impl<I, O> RefUnwindSafe for Terminal<I, O>

§

impl<I, O> Send for Terminal<I, O>
where I: Send, O: Send,

§

impl<I, O> Sync for Terminal<I, O>
where I: Sync, O: Sync,

§

impl<I, O> Unpin for Terminal<I, O>
where I: Unpin, O: Unpin,

§

impl<I, O> UnsafeUnpin for Terminal<I, O>
where I: UnsafeUnpin, O: UnsafeUnpin,

§

impl<I, O> UnwindSafe for Terminal<I, O>
where I: UnwindSafe, O: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Input for T
where T: Read + AsFd + Send,