[−][src]Struct crossterm::Screen
This type represents a screen which could be in normal, raw and alternate modes.
Let's talk about the different modes a bit:
-
Alternate modes:
*Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. The alternate buffer is exactly the dimensions of the window, without any scrollback region. For an example of this behavior, consider when vim is launched from bash. Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged.
-
RawModes
- No line buffering. Normally the terminals use line buffering. This means that the input will be sent to the terminal line by line. With raw mode the input will send one byte at a time.
- Input All input has to be written manually by the programmer.
- Characters The characters are not processed by the terminal driver but are sent straight through. Special character have no meaning, like backspace will not be interpreted as backspace but instead will be directly sent to the terminal.
- Escape characters
Note that in raw modes
\n
\r
will move to the new line but the cursor will be at the same position as before on the new line therefor use\n\r
to start at the new line at the first cell.
You have to make sure that you pass the correct Screen
to the modules cursor, terminal, color, input, style
.
If you switch to alternate screen modes you will get some Screen
handle back. This Screen
handle represents the alternate screen.
Once you want to do coloring or such you need to pass the Screen
handle the library so that it could be used for coloring on the right screen.
Example
// create default screen (not raw). let screen = Screen::default(); // create raw screen. let mut screen = Screen::new(true); // create a `Screen` with raw modes disabled. let screen = Screen::new(false); // create 'raw alternate screen' from normal screen. if let Ok(alternate_screen) = screen.enable_alternate_modes(true) { // 'alternate screen' is an instance which you should use when you want your actions like: coloring and cursor movement happening at the alternate screen. // For that you can use `Crossterm::from_screen(alternate.screen)` so that all modules like: cursor, input, terminal will be executed on alternate screen. let crossterm = Crossterm::from_screen(&alternate_screen.screen); crossterm.cursor(); crossterm.terminal(); // If you want access modules directly without the `Crossterm` type. You should do the following: let cursor = crossterm::cursor::from_screen(&alternate_screen.screen); let terminal = crossterm::terminal::from_screen(&alternate_screen.screen); let input = crossterm::input::from_screen(&alternate_screen.screen); }
Remarks
Note that using Screen
is preferred over manually using AlternateScreen
or RawScreen
.
Fields
stdout: Arc<TerminalOutput>
Methods
impl Screen
[src]
ⓘImportant traits for Screenpub fn new(raw_mode: bool) -> Screen
[src]
Create a new instance of the Screen also specify if the current screen should be in raw mode or normal mode.
If you are not sure what raw mode is then passed false or use the Screen::default()
to create an instance.
pub fn enable_alternate_modes(
&self,
raw_mode: bool
) -> Result<AlternateScreen, Error>
[src]
&self,
raw_mode: bool
) -> Result<AlternateScreen, Error>
Switch to alternate screen. This function will return an AlternateScreen
instance. If everything went well this type will give you control over the AlternateScreen
.
The bool 'raw_mode' specifies whether the alternate screen should be raw mode or not.
What is Alternate screen?
*Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. The alternate buffer is exactly the dimensions of the window, without any scrollback region. For an example of this behavior, consider when vim is launched from bash. Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged.
pub fn write_buf(&mut self, buf: &[u8]) -> Result<usize, Error>
[src]
Write buffer to an internal buffer. When you want to write the buffer to screen use flush_buf()
.
This function is useful if you want to build up some output and when you are ready you could flush the output to the screen.
Example
// write some text to the internal buffer of this type. Note that this will not be printed until you call `flush_buf` let screen = Screen::default(); screen.write_buf(b"Some text"); screen.write_buf(b"Some more text"); screen.write_buf(b"Some more text");
pub fn flush_buf(&mut self) -> Result<(), Error>
[src]
Flush the internal buffer to the screen.
pub fn disable_drop(&mut self)
[src]
This will disable the drop which will cause raw modes not to be undone on the drop of Screen
.
Trait Implementations
impl Write for Screen
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
[src]
fn flush(&mut self) -> Result<(), Error>
[src]
default fn write_vectored(&mut self, bufs: &[IoVec]) -> Result<usize, Error>
[src]
iovec
)Like write
, except that it writes from a slice of buffers. Read more
default fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
Attempts to write an entire buffer into this writer. Read more
default fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
Writes a formatted string into this writer, returning any error encountered. Read more
default fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Creates a "by reference" adaptor for this instance of Write
. Read more
impl Drop for Screen
[src]
fn drop(&mut self)
[src]
If the current screen is in raw mode we need to disable it when the instance goes out of scope.
impl From<Arc<TerminalOutput>> for Screen
[src]
ⓘImportant traits for Screenfn from(stdout: Arc<TerminalOutput>) -> Screen
[src]
Create a screen with the given 'Arc
impl From<TerminalOutput> for Screen
[src]
ⓘImportant traits for Screenfn from(stdout: TerminalOutput) -> Screen
[src]
Create a screen with the given Stdout
impl Default for Screen
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,