Rollup merge of #124992 - foresterre:example/is-terminal, r=ChrisDenton
Add example to IsTerminal::is_terminal
This commit is contained in:
commit
c5b8c7c3b0
@ -1161,7 +1161,41 @@ pub trait IsTerminal: crate::sealed::Sealed {
|
|||||||
/// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
|
/// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
|
||||||
/// Note that this [may change in the future][changes].
|
/// Note that this [may change in the future][changes].
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// An example of a type for which `IsTerminal` is implemented is [`Stdin`]:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use std::io::{self, IsTerminal, Write};
|
||||||
|
///
|
||||||
|
/// fn main() -> io::Result<()> {
|
||||||
|
/// let stdin = io::stdin();
|
||||||
|
///
|
||||||
|
/// // Indicate that the user is prompted for input, if this is a terminal.
|
||||||
|
/// if stdin.is_terminal() {
|
||||||
|
/// print!("> ");
|
||||||
|
/// io::stdout().flush()?;
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let mut name = String::new();
|
||||||
|
/// let _ = stdin.read_line(&mut name)?;
|
||||||
|
///
|
||||||
|
/// println!("Hello {}", name.trim_end());
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// The example can be run in two ways:
|
||||||
|
///
|
||||||
|
/// - If you run this example by piping some text to it, e.g. `echo "foo" | path/to/executable`
|
||||||
|
/// it will print: `Hello foo`.
|
||||||
|
/// - If you instead run the example interactively by running the executable directly, it will
|
||||||
|
/// panic with the message "Expected input to be piped to the process".
|
||||||
|
///
|
||||||
|
///
|
||||||
/// [changes]: io#platform-specific-behavior
|
/// [changes]: io#platform-specific-behavior
|
||||||
|
/// [`Stdin`]: crate::io::Stdin
|
||||||
#[stable(feature = "is_terminal", since = "1.70.0")]
|
#[stable(feature = "is_terminal", since = "1.70.0")]
|
||||||
fn is_terminal(&self) -> bool;
|
fn is_terminal(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user