### Run Crossterm Examples Source: https://github.com/crossterm-rs/crossterm/blob/master/examples/README.md Use this command to execute specific example files from the crossterm library. Replace '[file name]' with the desired example's filename. ```bash $ cargo run --example [file name] ``` -------------------------------- ### Rust Import Order Example Source: https://github.com/crossterm-rs/crossterm/blob/master/docs/CONTRIBUTING.md Demonstrates the required semantic grouping and ordering of imports in Rust code. Standard library imports come first, followed by external crates, then current crate, parent module, current module, and finally module declarations. An empty line must separate these groups. ```rust use crossterm_utils::{csi, write_cout, Result}; use crate::sys::{get_cursor_position, show_cursor}; use super::Cursor; ``` -------------------------------- ### Deprecated Alternate/Raw Screen Management (0.3) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.3-to-0.4 These are examples of how alternate and raw screens could not be used in version 0.3. ```rust // could not be used any more ::crossterm::AlternateScreen::from(); // cannot put any Write into raw mode. ::std::io::Write::into_raw_mode() ``` -------------------------------- ### Rust Key Event Example with Modifiers Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Demonstrates how to handle key events with advanced modifier support using the KeyModifiers struct. This replaces the older method of embedding modifiers directly into KeyEvent. ```rust use crossterm::event::{KeyModifiers, KeyEvent, KeyCode}; // Example of how to check for modifiers let key_event = KeyEvent { code: KeyCode::Char('a'), modifiers: KeyModifiers::ALT | KeyModifiers::SHIFT, }; if key_event.modifiers.contains(KeyModifiers::ALT) { // Handle ALT key press } if key_event.modifiers.contains(KeyModifiers::SHIFT) { // Handle SHIFT key press } if key_event.modifiers.contains(KeyModifiers::CONTROL) { // Handle CONTROL key press } if key_event.modifiers.contains(KeyModifiers::ALT | KeyModifiers::SHIFT) { // Handle ALT + SHIFT key press } ``` -------------------------------- ### Run Interactive Demo Source: https://github.com/crossterm-rs/crossterm/blob/master/examples/README.md Navigate to the 'examples/interactive-demo' directory and run this command to execute the interactive demo. ```bash cargo run ``` -------------------------------- ### Initialize Terminal Components (0.9) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 Version 0.9 simplifies initialization by removing the need to pass screen or output objects. Components can now be created directly. ```rust Terminal::new(); TerminalCursor::new(); TerminalColor::new(); TerminalInput::new(); Crossterm::new(); ``` -------------------------------- ### Create Raw and Alternate Screens (0.9) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 Version 0.9 introduces dedicated types `RawScreen` and `AlternateScreen` for managing screen modes. Raw modes can be entered using `into_raw_mode`, and alternate screens using `to_alternate`. ```rust use crossterm::{AlternateScreen, RawScreen, IntoRawModes}; let raw_screen = RawScreen::into_raw_mode(); let raw_screen = stdout().into_raw_mode(); let alternate = AlternateScreen::to_alternate(true); ``` -------------------------------- ### New Alternate/Raw Screen Management (0.4) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.3-to-0.4 In version 0.4, alternate and raw screen management is handled via the `Screen` type. `Screen::default()` creates a default screen, `Screen::new(true)` creates a screen with raw modes enabled, and `enable_alternate_modes` can be used to enter alternate screen modes. ```rust use crossterm::Screen; use crossterm::cursor::cursor; // this will create a default screen. let screen = Screen::default(); // this will create a new screen with raw modes enabled. let screen = Screen::new(true); // `false` specifies whether the alternate screen should be in raw modes. if let Ok(alternate) = screen.enable_alternate_modes(false) { let cursor = cursor(&alternate.screen); } ``` -------------------------------- ### New API for Cursor, Terminal, and Color Modules Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2.1-to-0.3.0 Demonstrates the updated API requiring an `Rc` reference for cursor, terminal, and color functions. ```rust use crossterm::Context; let context: Rc = Context::new(); let cursor = cursor(&context); let terminal = terminal(&context); let color = color(&context); ``` -------------------------------- ### Execute Styled Text with Crossterm Source: https://github.com/crossterm-rs/crossterm/blob/master/README.md Demonstrates how to apply foreground and background colors to text using both the `execute!` macro and chained function calls. Remember to reset colors after styling. ```rust use std::io::{stdout, Write}; use crossterm::{ execute, style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor}, ExecutableCommand, event, }; fn main() -> std::io::Result<()> { // using the macro execute!( stdout(), SetForegroundColor(Color::Blue), SetBackgroundColor(Color::Red), Print("Styled text here."), ResetColor )?; // or using functions stdout() .execute(SetForegroundColor(Color::Blue))? .execute(SetBackgroundColor(Color::Red))? .execute(Print("Styled text here."))? .execute(ResetColor)?; Ok(()) } ``` -------------------------------- ### Provide Screen for alternate/raw screen operations Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.4-to-0.5 When working with alternate or raw screens, a `Screen` instance must still be provided. This ensures actions are performed on the correct screen buffer. Functions like `from_screen` and `from_output` are used in this context. ```rust use crossterm::cursor; use crossterm::color; use crossterm::input; use crossterm::terminal; let screen = Screen::default(); if let Ok(alternate) = screen.enable_alternate_modes(false) { let screen = alternate.screen; let color = color::from_screen(&screen); let cursor = cursor::from_screen(&screen); let input = input::from_screen(&screen); let terminal = terminal::from_screen(&screen); let crossterm = Crossterm::from_screen(&screen); let terminal = Terminal::from_output(&screen.stdout); let cursor = TerminalCursor::from_output(&screen.stdout); let color = TerminalColor::from_output(&screen.stdout); let input = TerminalInput::from_output(&screen.stdout); } ``` -------------------------------- ### Create Raw Screen (0.8) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 In version 0.8, the `Screen` type was used to manage raw and alternate screen modes. Creating a raw screen involved instantiating `Screen` and then enabling raw modes. ```rust use crossterm::Screen; // create raw screen let screen = Screen::new(true); // create alternate raw screen let screen = Screen::new(true); let alternate = screen.enable_raw_modes(true); ``` -------------------------------- ### Initialize Terminal Components (0.8) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 In version 0.8, terminal components required explicit screen and output objects. This was necessary for managing specific output contexts like alternate screens. ```rust use crossterm::{Screen, Terminal, TerminalCursor, TerminalColor, TerminalInput, Crossterm}; let screen = Screen::new(false); Terminal::from_output(&screen.stdout); TerminalCursor::from_output(&screen.stdout); TerminalColor::from_output(&screen.stdout); TerminalInput::from_output(&screen.stdout); Crossterm::from_screen(&screen.stdout); ``` -------------------------------- ### Old API for Cursor, Terminal, and Color Modules Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2.1-to-0.3.0 Illustrates the previous method of calling cursor, terminal, and color functions without context. ```rust use crossterm::terminal.terminal; use crossterm::cursor.cursor; use crossterm::style.color; /// Old situation let cursor = cursor(); let terminal = terminal(); let color = color(); ``` -------------------------------- ### Enable raw mode Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use `terminal::enable_raw_mode()` to enable raw mode, replacing the functionality previously managed by `RawScreen::into_raw_mode()`. ```rust terminal::enable_raw_mode()?; ``` -------------------------------- ### Migrate screen module imports Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Update imports from `crossterm::screen` to `crossterm::terminal` for `EnterAlternateScreen` and `LeaveAlternateScreen`. ```rust // Old: use crossterm::screen::{EnterAlternateScreen, LeaveAlternateScreen}; // New: use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen}; ``` -------------------------------- ### Wait Until: Old Method Signature Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Demonstrates the previous usage of `wait_until` which did not require an instance of `TerminalInput`. ```rust TerminalInput::wait_until(KeyEvent::OnKeyPress(b'x')); ``` -------------------------------- ### Namespace Refactor: New Imports Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Demonstrates the simplified import method in crossterm 0.6, allowing direct use of types via `use crossterm::*;`. ```rust crossterm::{TerminalInput, style, TerminalColor, StyledObject, Terminal, ...} ``` -------------------------------- ### Painting Text with Crossterm 0.3.0 (Terminal Type) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2.1-to-0.3.0 Demonstrates painting text with specified foreground and background colors using the `Terminal` type and context. ```rust use crossterm::Context; use crossterm::style::Color; use crossterm::terminal::terminal; // 2: use the `Terminal` type let context: Rc = Context::new(); let terminal = terminal(&context).paint("Red on Blue").with(Color::Red).on(Color::Blue); ``` -------------------------------- ### Painting Text with Crossterm 0.3.0 (Crossterm Type) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2.1-to-0.3.0 Shows how to paint text with specified foreground and background colors using the `Crossterm` type. ```rust use crossterm::Crossterm; use crossterm::style::Color; // 1: use the `Crossterm` type let crossterm = Crossterm::new(); let mut color = crossterm.paint("Red on Blue").with(Color::Red).on(Color::Blue); ``` -------------------------------- ### New Screen Component Retrieval using from_output() Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 An alternative to the `Crossterm` type, this method uses `from_output()` to retrieve components from the screen's stored output. ```rust let screen = Screen::new(); let cursor = TerminalCursor::from_output(&screen.stdout); let input = TerminalInput::from_output(&screen.stdout); ``` -------------------------------- ### Namespace Refactor: Old Imports Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Illustrates the previous method of importing types from specific crossterm modules. ```rust crossterm::input::{TerminalInput, ...}; crossterm::style::style; crossterm::cursor::*; crossterm::teriminal::{Terminal, ...}; ``` -------------------------------- ### Paint Styled Text (0.8) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 In version 0.8, painting styled text onto a screen required passing a reference to the screen object. This method was used for applying styles like color and background. ```rust use crossterm::{Crossterm, Screen, style}; let screen = Screen::new(false); style("Some colored text") .with(Color::Blue) .on(Color::Black) .paint(&screen); let crossterm = Crossterm::new(); crossterm.style("Some colored text") .with(Color::Blue) .on(Color::Black) .paint(&screen); ``` -------------------------------- ### Print Styled Text (0.9) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 Version 0.9 allows direct printing of styled text using method chaining on string literals, eliminating the need for explicit screen objects. ```rust print!("{}", "Some colored text".blue().on_black()); ``` -------------------------------- ### Wait Until: New Method Signature Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Shows the updated `wait_until` method in crossterm 0.6, which now requires an instance of `TerminalInput`. ```rust let terminal_input = TerminalInput::new(); terminal_input.wait_until(KeyEvent::OnKeyPress(b'x')); ``` -------------------------------- ### Add Crossterm Dependency to Cargo.toml Source: https://github.com/crossterm-rs/crossterm/blob/master/README.md To use Crossterm, add it as a dependency in your Cargo.toml file. Ensure you are using a compatible version. ```toml [dependencies] crossterm = "0.27" ``` -------------------------------- ### Handle Key Modifiers Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Match on `KeyEvent` to handle different key modifiers like Control, Shift, and Alt. ```rust match event { Event::Key(KeyEvent { modifiers: KeyModifiers::CONTROL, code }) => { /* handle control key */ } Event::Key(KeyEvent { modifiers: KeyModifiers::Shift, code}) => { /* handle shift key */ } Event::Key(KeyEvent { modifiers: KeyModifiers::ALT, code }) => { /* handle alt key */ } _ => {} // Handle other events } ``` -------------------------------- ### Pass Screen Reference to Modules (0.4) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.3-to-0.4 In version 0.4, functions like `cursor()`, `color()`, and `terminal()` now require a reference to a `Screen` object. This replaces the previous usage with `Context`. ```rust use crossterm::Screen; let screen: Screen = Screen::default(); let cursor = cursor(&screen); let terminal = terminal(&screen); let color = color(&screen); ``` -------------------------------- ### Rename AlternateScreen::to_main_screen Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 The function `to_main_screen` on `AlternateScreen` has been renamed to `to_main`. ```rust AlternateScreen::to_main_screen => Alternate::to_main ``` -------------------------------- ### Replace TerminalInput::read_sync() Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Replace `TerminalInput::read_sync()` with `event::read()` to read a single event. ```rust use crossterm::event; let event = event::read()?; ``` -------------------------------- ### Replace TerminalInput::read_line() with std library Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use the standard library's `io::stdin().read_line()` for reading a line, as an alternative to the removed `TerminalInput::read_line()`. ```rust fn read_line(&self) -> Result { let mut rv = String::new(); io::stdin().read_line(&mut rv)?; let len = rv.trim_end_matches(&['\r', '\n'][..]).len(); rv.truncate(len); Ok(rv) } ``` -------------------------------- ### Replace TerminalInput::enable_mouse_modes() Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use `execute!(io::stdout(), event::EnableMouseCapture)` to enable mouse capture, replacing `TerminalInput::enable_mouse_modes()`. ```rust use crossterm::event; execute!(io::stdout(), event::EnableMouseCapture)?; ``` -------------------------------- ### New Screen Component Retrieval using Crossterm Type Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Replaces removed `from_screen` methods by using the `Crossterm` type to access screen components. ```rust let screen = Screen::new(); let crossterm = Crossterm::from_screen(screen); let cursor = crossterm.cursor(); .... ``` -------------------------------- ### Namespace Changes in crossterm 0.2.1 Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2-to-0.2.1 Namespaces have been shortened for better readability. Update your imports to reflect these changes. ```rust Old: crossterm::crossterm_style New: crossterm::style ``` ```rust Old: crossterm::crossterm_terminal New: crossterm::terminal ``` ```rust Old: crossterm::crossterm_cursor New: crossterm::cursor ``` -------------------------------- ### Rename AlternateScreen::to_alternate_screen Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 The function `to_alternate_screen` on `AlternateScreen` has been renamed to `to_alternate`. ```rust AlternateScreen::to_alternate_screen => Alternate::to_alternate ``` -------------------------------- ### Removed Methods: Old Screen Component Retrieval Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.5-to-0.6 Shows the deprecated methods for obtaining screen components like cursor, input, terminal, and color. ```rust let screen = Screen::new(); // the below methods are not available anymore cursor::from_screen(&screen); input::from_screen(&screen); terminal::from_screen(&screen); color::from_screen(&screen); ``` -------------------------------- ### Remove Screen from function calls (main screen) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.4-to-0.5 For actions on the main screen, `Screen` is no longer required as an argument for `cursor()`, `color()`, `input()`, and `terminal()`. This applies to direct calls and constructors of related types. ```rust let screen = Screen::default(); let color = color(&screen); let cursor = cursor(&screen); let input = input(&screen); let terminal = terminal(&screen); let crossterm = Crossterm::new(&screen); let terminal = Terminal::new(&screen.stdout); let cursor = TerminalCursor::new(&screen.stdout); let color = TerminalColor::new(&screen.stdout); let input = TerminalInput::new(&screen.stdout); ``` ```rust let color = color(); let cursor = cursor(); let input = input(); let terminal = terminal(); let crossterm = Crossterm::new(); let terminal = Terminal::new(); let cursor = TerminalCursor::new(); let color = TerminalColor::new(); let input = TerminalInput::new(); ``` -------------------------------- ### New Crossterm::paint() Usage (0.4) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.3-to-0.4 In version 0.4, `Crossterm::paint()` is removed. Use `Crossterm::style()` or `style()` directly, then call `paint()` on the styled object, passing the `Screen`. ```rust use crossterm::Crossterm; use crossterm::style::{Color, input, style}; // 1: use the `Crossterm` type let crossterm = Crossterm::new(); let styled_object = crossterm.style("Red text on Black background").with(Color::Red).on(Color::Black); styled_object.paint(&screen); // 2: use the `Terminal` type let styled_object = style("Red text on Black background").with(Color::Red).on(Color::Black); styled_object.paint(&screen); ``` -------------------------------- ### Replace TerminalInput::read_line() with custom function Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Implement this `read_line` function to replace the removed `TerminalInput::read_line()`, reading input until Enter is pressed. ```rust pub fn read_line() -> Result { let mut line = String::new(); while let Event::Key(KeyEvent { code: code, .. }) = event::read()? { match code { KeyCode::Enter => { break; }, KeyCode::Char(c) => { line.push(c); }, _ => {} // Ignore other keys } } return Ok(line); } ``` -------------------------------- ### Deprecated Crossterm::paint() Usage (0.3) Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.3-to-0.4 This shows the old way of using `Crossterm::paint()` before version 0.4. ```rust use crossterm::terminal::terminal; use crossterm::cursor::cursor; use crossterm::style::color; use crossterm::Context; let context: Rc = Context::new(); let cursor = cursor(&context); let terminal = terminal(&context); let color = color(&context); ``` -------------------------------- ### Rename RawScreen::disable_raw_modes Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.8-to-0.9 The function `disable_raw_modes` on `RawScreen` has been renamed to `disable_raw_mode` for consistency. ```rust RawScreen::disable_raw_modes => RawScreen::disable_raw_mode ``` -------------------------------- ### Method Name Changes in crossterm 0.2.1 Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.2-to-0.2.1 Several method names have been updated to be more consistent. Ensure you use the new method names in your code. ```rust Old: ::crossterm::crossterm_cursor::get(); New: ::crossterm::cursor::cursor(); ``` ```rust Old: ::crossterm::crossterm_terminal::get(); New: ::crossterm::terminal::terminal(); ``` ```rust Old: ::crossterm::crossterm_style::color::get(); New: ::crossterm::style::color::color(); ``` -------------------------------- ### Disable raw mode Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use `terminal::disable_raw_mode()` to disable raw mode. This function is now manual and does not rely on RAII like the old `RawScreen` type. ```rust terminal::disable_raw_mode()?; ``` -------------------------------- ### Replace TerminalInput::read_char() with std library Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Alternatively, use the standard library's `io::stdin().bytes()` for reading a character, similar to the removed `TerminalInput::read_char()`. ```rust pub fn read_char() -> Result { let char = io::stdin().bytes().next()?.map(|b| b as char)?; /* or something simmilar */ } ``` -------------------------------- ### Replace TerminalInput::read_async() with poll Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use `poll` with a timeout to check for events before calling `event::read()`, replacing the non-blocking behavior of `TerminalInput::read_async()`. ```rust use crossterm::event; use std::time::Duration; if poll(Duration::from_millis(0))? { // Guaranteed that `read()` wont block if `poll` returns `Ok(true)` let event = event::read()?; } ``` -------------------------------- ### Replace TerminalInput::disable_mouse_modes() Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use `execute!(io::stdout(), event::DisableMouseCapture)` to disable mouse capture, replacing `TerminalInput::disable_mouse_modes()`. ```rust use crossterm::event; execute!(io::stdout(), event::DisableMouseCapture)?; ``` -------------------------------- ### Replace TerminalInput::read_char() with custom function Source: https://github.com/crossterm-rs/crossterm/wiki/Upgrade-from-0.13-to-0.14 Use this custom `read_char` function as a replacement for the removed `TerminalInput::read_char()` to read a single character. ```rust pub fn read_char() -> Result { loop { if let Event::Key(KeyEvent { code: KeyCode::Char(c), .. }) = event::read()? { return Ok(c); } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.