pub struct Terminal { /* private fields */ }
Expand description
A handle to the terminal associated with the current process.
Once opened, you can use Self::prompt()
to read non-sensitive data from the terminal,
and Self::prompt_sensitive()
to read sensitive data like passwords.
Alternatively, you can manually call Self::enable_echo()
and Self::disable_echo()
, and read/write from the terminal directly.
The terminal handle implements the standard Read
, Write
and BufRead
traits,
and it has a Self::read_line()
convenience function that returns a new string.
§Terminal modes
When opened, the terminal will be put in line editing mode. When dropped, the original mode of the terminal will be restored. Note that the terminal is inherently a global resource, so creating multiple terminal objects and dropping them in a different order can cause the terminal to be left in a different mode.
Implementations§
Source§impl Terminal
impl Terminal
Sourcepub fn open() -> Result<Self>
pub fn open() -> Result<Self>
Open the terminal associated with the current process.
The exact behavior is platform dependent.
On Unix platforms, if one of standard I/O streams is a terminal, that terminal is used.
First standard error is tried, then standard input and finally standard output.
If none of those work, the function tries to open /dev/tty
.
This means that on Unix platforms, the terminal prompt can still work, even when both standard input and standard output are connected to pipes instead of the terminal.
On Windows, if both standard input and standard error are connected to a terminal, those streams are used.
In all cases, if the function fails to find a terminal for the process, an error is returned.
Sourcepub fn is_echo_enabled(&self) -> Result<bool>
pub fn is_echo_enabled(&self) -> Result<bool>
Check if the terminal is echoing input.
If enabled, any text typed on the terminal will be visible.
Sourcepub fn disable_echo(&self) -> Result<()>
pub fn disable_echo(&self) -> Result<()>
Disable echoing of terminal input.
This will prevent text typed on the terminal from being visible. This can be used to hide passwords while they are being typed.
Sourcepub fn enable_echo(&mut self) -> Result<()>
pub fn enable_echo(&mut self) -> Result<()>
Enable echoing of terminal input.
This will cause any text typed on the terminal to be visible.
Sourcepub fn read_input_line(&mut self) -> Result<String>
pub fn read_input_line(&mut self) -> Result<String>
Read a line of input from the terminal.
If echoing is disabled, this will also print a newline character to visually indicate to the user.
If this is not desired, use the BufRead::read_line()
function instead.
Sourcepub fn prompt(&mut self, prompt: impl Display) -> Result<String>
pub fn prompt(&mut self, prompt: impl Display) -> Result<String>
Prompt the user on the terminal.
This function does not enable or disable echoing and should not normally be used for reading sensitive data like passwords.
Consider Self::prompt_sensitive()
instead.
Sourcepub fn prompt_sensitive(&mut self, prompt: impl Display) -> Result<String>
pub fn prompt_sensitive(&mut self, prompt: impl Display) -> Result<String>
Prompt the user for sensitive data (like passwords) on the terminal.
This function makes sure that echoing is disabled before the prompt is shown. If echoing was enabled, it is re-enabled after the response is read.
Use Self::prompt()
to read non-sensitive data.
Trait Implementations§
Source§impl BufRead for Terminal
impl BufRead for Terminal
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte
or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moreSource§impl Read for Terminal
impl Read for Terminal
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for Terminal
impl Write for Terminal
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)