Struct console::Term

source ·
pub struct Term { /* private fields */ }
Expand description

Abstraction around a terminal.

A terminal can be cloned. If a buffer is used it’s shared across all clones which means it largely acts as a handle.

Implementations§

source§

impl Term

source

pub fn stdout() -> Term

Return a new unbuffered terminal.

source

pub fn stderr() -> Term

Return a new unbuffered terminal to stderr.

source

pub fn buffered_stdout() -> Term

Return a new buffered terminal.

source

pub fn buffered_stderr() -> Term

Return a new buffered terminal to stderr.

source

pub fn read_write_pair<R, W>(read: R, write: W) -> Term
where R: Read + Debug + AsRawFd + Send + 'static, W: Write + Debug + AsRawFd + Send + 'static,

Return a terminal for the given Read/Write pair styled like stderr.

source

pub fn read_write_pair_with_style<R, W>(read: R, write: W, style: Style) -> Term
where R: Read + Debug + AsRawFd + Send + 'static, W: Write + Debug + AsRawFd + Send + 'static,

Return a terminal for the given Read/Write pair.

source

pub fn style(&self) -> Style

Return the style for this terminal.

source

pub fn target(&self) -> TermTarget

Return the target of this terminal.

source

pub fn write_line(&self, s: &str) -> Result<()>

Write a string to the terminal and add a newline.

source

pub fn read_char(&self) -> Result<char>

Read a single character from the terminal.

This does not echo the character and blocks until a single character or complete key chord is entered. If the terminal is not user attended the return value will be an error.

source

pub fn read_key(&self) -> Result<Key>

Read a single key form the terminal.

This does not echo anything. If the terminal is not user attended the return value will always be the unknown key.

source

pub fn read_key_raw(&self) -> Result<Key>

source

pub fn read_line(&self) -> Result<String>

Read one line of input.

This does not include the trailing newline. If the terminal is not user attended the return value will always be an empty string.

source

pub fn read_line_initial_text(&self, initial: &str) -> Result<String>

Read one line of input with initial text.

This method blocks until no other thread is waiting for this read_line before reading a line from the terminal. This does not include the trailing newline. If the terminal is not user attended the return value will always be an empty string.

source

pub fn read_secure_line(&self) -> Result<String>

Read a line of input securely.

This is similar to read_line but will not echo the output. This also switches the terminal into a different mode where not all characters might be accepted.

source

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

Flush internal buffers.

This forces the contents of the internal buffer to be written to the terminal. This is unnecessary for unbuffered terminals which will automatically flush.

source

pub fn is_term(&self) -> bool

Check if the terminal is indeed a terminal.

source

pub fn features(&self) -> TermFeatures<'_>

Check for common terminal features.

source

pub fn size(&self) -> (u16, u16)

Return the terminal size in rows and columns or gets sensible defaults.

source

pub fn size_checked(&self) -> Option<(u16, u16)>

Return the terminal size in rows and columns.

If the size cannot be reliably determined None is returned.

source

pub fn move_cursor_to(&self, x: usize, y: usize) -> Result<()>

Move the cursor to row x and column y. Values are 0-based.

source

pub fn move_cursor_up(&self, n: usize) -> Result<()>

Move the cursor up by n lines, if possible.

If there are less than n lines above the current cursor position, the cursor is moved to the top line of the terminal (i.e., as far up as possible).

source

pub fn move_cursor_down(&self, n: usize) -> Result<()>

Move the cursor down by n lines, if possible.

If there are less than n lines below the current cursor position, the cursor is moved to the bottom line of the terminal (i.e., as far down as possible).

source

pub fn move_cursor_left(&self, n: usize) -> Result<()>

Move the cursor n characters to the left, if possible.

If there are fewer than n characters to the left of the current cursor position, the cursor is moved to the beginning of the line (i.e., as far to the left as possible).

source

pub fn move_cursor_right(&self, n: usize) -> Result<()>

Move the cursor n characters to the right.

If there are fewer than n characters to the right of the current cursor position, the cursor is moved to the end of the current line (i.e., as far to the right as possible).

source

pub fn clear_line(&self) -> Result<()>

Clear the current line.

Position the cursor at the beginning of the current line.

source

pub fn clear_last_lines(&self, n: usize) -> Result<()>

Clear the last n lines before the current line.

Position the cursor at the beginning of the first line that was cleared.

source

pub fn clear_screen(&self) -> Result<()>

Clear the entire screen.

Move the cursor to the upper left corner of the screen.

source

pub fn clear_to_end_of_screen(&self) -> Result<()>

Clear everything from the current cursor position to the end of the screen. The cursor stays in its position.

source

pub fn clear_chars(&self, n: usize) -> Result<()>

Clear the last n characters of the current line.

source

pub fn set_title<T: Display>(&self, title: T)

Set the terminal title.

source

pub fn show_cursor(&self) -> Result<()>

Make the cursor visible again.

source

pub fn hide_cursor(&self) -> Result<()>

Hide the cursor.

Trait Implementations§

source§

impl AsRawFd for Term

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Clone for Term

source§

fn clone(&self) -> Term

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Term

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Read for &'a Term

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>

Read 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>

Read 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>

Read 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)
Read 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” adaptor 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§

impl Read for Term

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>

Read 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>

Read 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>

Read 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)
Read 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” adaptor 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§

impl<'a> Write for &'a Term

source§

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

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

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

Flush 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, fmt: 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
source§

impl Write for Term

source§

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

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

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

Flush 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, fmt: 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 Freeze for Term

§

impl RefUnwindSafe for Term

§

impl Send for Term

§

impl Sync for Term

§

impl Unpin for Term

§

impl UnwindSafe for Term

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.