Struct mortal::terminal::Terminal

source ·
pub struct Terminal(_);
Expand description

Provides concurrent read and write access to a terminal device

Concurrency

Access to read and write operations is controlled by two internal locks: One for reading and one for writing. Each lock may be held independently of the other.

If any one thread wishes to hold both locks, the read lock must be acquired first, in order to prevent deadlocks.

Implementations§

source§

impl Terminal

source

pub fn new() -> Result<Terminal>

Opens a new interface to the terminal on stdout.

source

pub fn stderr() -> Result<Terminal>

Opens a new interface to the terminal on stderr.

source

pub fn name(&self) -> &str

Returns the name of the terminal.

Notes

On Unix, this method returns the contents of the TERM environment variable.

On Windows, this method always returns the string "windows-console".

source

pub fn lock_read(&self) -> LockResult<TerminalReadGuard<'_>>

Attempts to acquire an exclusive lock on terminal read operations.

The current thread will block until the lock can be acquired.

source

pub fn lock_write(&self) -> LockResult<TerminalWriteGuard<'_>>

Attempts to acquire an exclusive lock on terminal write operations.

The current thread will block until the lock can be acquired.

source

pub fn try_lock_read(&self) -> TryLockResult<TerminalReadGuard<'_>>

Attempts to acquire an exclusive lock on terminal read operations.

If the lock cannot be acquired immediately, Err(_) is returned.

source

pub fn try_lock_write(&self) -> TryLockResult<TerminalWriteGuard<'_>>

Attempts to acquire an exclusive lock on terminal write operations.

If the lock cannot be acquired immediately, Err(_) is returned.

source§

impl Terminal

Locking

The following methods internally acquire both the read and write locks.

The locks are released before the method returns.

These methods are also implemented on TerminalReadGuard, which holds the Terminal read lock until the value is dropped.

source

pub fn prepare(&self, config: PrepareConfig) -> Result<PrepareState>

Prepares the terminal to read input.

When reading operations have concluded, restore should be called with the resulting PrepareState value to restore the terminal to its previous state.

This method may be called more than once before a corresponding restore call. However, each restore call must receive the most recently created PrepareState value.

See PrepareConfig for details.

source

pub fn restore(&self, state: PrepareState) -> Result<()>

Restores the terminal to its previous state.

source§

impl Terminal

Locking

The following methods internally acquire the read lock.

The lock is released before the method returns.

These methods are also implemented on TerminalReadGuard, which holds the Terminal read lock until the value is dropped.

source

pub fn wait_event(&self, timeout: Option<Duration>) -> Result<bool>

Waits for an event from the terminal.

Returns Ok(false) if timeout elapses without an event occurring.

If timeout is None, this method will wait indefinitely.

source

pub fn read_event(&self, timeout: Option<Duration>) -> Result<Option<Event>>

Waits for input and reads an event from the terminal.

Returns Ok(None) if timeout elapses without an event occurring.

If timeout is None, this method will wait indefinitely.

source§

impl Terminal

Locking

The following methods internally acquire the write lock.

The lock is released before the method returns.

These methods are also implemented on TerminalWriteGuard, which holds the Terminal write lock until the value is dropped.

source

pub fn size(&self) -> Result<Size>

Returns the size of the terminal.

source

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

Clears the terminal screen, placing the cursor at the first line and column.

If the terminal contains a scrolling window over a buffer, the window will be scrolled downward, preserving as much of the existing buffer as possible.

source

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

Clears the current line, starting at cursor position.

source

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

Clears the screen, starting at cursor position.

source

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

Moves the cursor up n lines.

source

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

Moves the cursor down n lines.

source

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

Moves the cursor left n columns.

source

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

Moves the cursor right n columns.

source

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

Moves the cursor to the first column of the current line

source

pub fn set_cursor_mode(&self, mode: CursorMode) -> Result<()>

Set the current cursor mode.

This setting is a visible hint to the user. It produces no change in behavior.

Notes

On some systems, this setting may have no effect.

source

pub fn add_style(&self, style: Style) -> Result<()>

Adds a set of Style flags to the current style setting.

source

pub fn remove_style(&self, style: Style) -> Result<()>

Removes a set of Style flags from the current style setting.

source

pub fn set_style<S>(&self, style: S) -> Result<()>where S: Into<Option<Style>>,

Sets the current style to the given set of flags.

source

pub fn set_theme(&self, theme: Theme) -> Result<()>

Sets all attributes for the terminal.

source

pub fn set_fg<C: Into<Option<Color>>>(&self, fg: C) -> Result<()>

Sets the foreground text color.

source

pub fn set_bg<C: Into<Option<Color>>>(&self, bg: C) -> Result<()>

Sets the background text color.

source

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

Removes color and style attributes.

source

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

Adds bold to the current style setting.

This is equivalent to self.add_style(Style::BOLD).

source

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

Adds italic to the current style setting.

This is equivalent to self.add_style(Style::ITALIC).

source

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

Adds underline to the current style setting.

This is equivalent to self.add_style(Style::UNDERLINE).

source

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

Adds reverse to the current style setting.

This is equivalent to self.add_style(Style::REVERSE).

source

pub fn write_styled<F, B, S>( &self, fg: F, bg: B, style: S, s: &str ) -> Result<()>where F: Into<Option<Color>>, B: Into<Option<Color>>, S: Into<Option<Style>>,

Writes output to the terminal with the given color and style.

All attributes are removed after the given text is written.

source

pub fn write_char(&self, ch: char) -> Result<()>

Writes a single character to the terminal using the current style and color settings.

source

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

Writes a string to the terminal using the current style and color settings.

source

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

Writes formatted text to the terminal using the current style and color settings.

This method enables Terminal to be used as the receiver to the write! and writeln! macros.

Examples
let term = Terminal::new()?;

writeln!(term, "Hello, world!")?;

Trait Implementations§

source§

impl OpenTerminalExt for Terminal

source§

fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>

Opens a terminal interface on the device at the given path. Read more
source§

impl TerminalExt for Terminal

source§

fn read_raw( &mut self, buf: &mut [u8], timeout: Option<Duration> ) -> Result<Option<Event>>

Reads raw data from the terminal. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.