| 1 | // Codewhale patch: a path-patched crate loses Cargo's cap-lints, and CI builds with |
| 2 | // `-D warnings` on a floating stable toolchain. Keep upstream lints from failing our build. |
| 3 | #![allow(warnings)] |
| 4 | #![deny(unused_imports, unused_must_use)] |
| 5 | |
| 6 | //! # Cross-platform Terminal Manipulation Library |
| 7 | //! |
| 8 | //! Crossterm is a pure-rust, terminal manipulation library that makes it possible to write cross-platform text-based interfaces. |
| 9 | //! |
| 10 | //! This crate supports all UNIX and Windows terminals down to Windows 7 (not all terminals are tested |
| 11 | //! see [Tested Terminals](https://github.com/crossterm-rs/crossterm#tested-terminals) |
| 12 | //! for more info). |
| 13 | //! |
| 14 | //! ## Command API |
| 15 | //! |
| 16 | //! The command API makes the use of `crossterm` much easier and offers more control over when and how a |
| 17 | //! command is executed. A command is just an action you can perform on the terminal e.g. cursor movement. |
| 18 | //! |
| 19 | //! The command API offers: |
| 20 | //! |
| 21 | //! * Better Performance. |
| 22 | //! * Complete control over when to flush. |
| 23 | //! * Complete control over where the ANSI escape commands are executed to. |
| 24 | //! * Way easier and nicer API. |
| 25 | //! |
| 26 | //! There are two ways to use the API command: |
| 27 | //! |
| 28 | //! * Functions can execute commands on types that implement Write. Functions are easier to use and debug. |
| 29 | //! There is a disadvantage, and that is that there is a boilerplate code involved. |
| 30 | //! * Macros are generally seen as more difficult and aren't always well supported by editors but offer an API with less boilerplate code. If you are |
| 31 | //! not afraid of macros, this is a recommendation. |
| 32 | //! |
| 33 | //! Linux and Windows 10 systems support ANSI escape codes. Those ANSI escape codes are strings or rather a |
| 34 | //! byte sequence. When we `write` and `flush` those to the terminal we can perform some action. |
| 35 | //! For older windows systems a WinAPI call is made. |
| 36 | //! |
| 37 | //! ### Supported Commands |
| 38 | //! |
| 39 | //! - Module [`cursor`](cursor/index.html) |
| 40 | //! - Visibility - [`Show`](cursor/struct.Show.html), [`Hide`](cursor/struct.Hide.html) |
| 41 | //! - Appearance - [`EnableBlinking`](cursor/struct.EnableBlinking.html), |
| 42 | //! [`DisableBlinking`](cursor/struct.DisableBlinking.html), |
| 43 | //! [`SetCursorStyle`](cursor/enum.SetCursorStyle.html) |
| 44 | //! - Position - |
| 45 | //! [`SavePosition`](cursor/struct.SavePosition.html), [`RestorePosition`](cursor/struct.RestorePosition.html), |
| 46 | //! [`MoveUp`](cursor/struct.MoveUp.html), [`MoveDown`](cursor/struct.MoveDown.html), |
| 47 | //! [`MoveLeft`](cursor/struct.MoveLeft.html), [`MoveRight`](cursor/struct.MoveRight.html), |
| 48 | //! [`MoveTo`](cursor/struct.MoveTo.html), [`MoveToColumn`](cursor/struct.MoveToColumn.html),[`MoveToRow`](cursor/struct.MoveToRow.html), |
| 49 | //! [`MoveToNextLine`](cursor/struct.MoveToNextLine.html), [`MoveToPreviousLine`](cursor/struct.MoveToPreviousLine.html) |
| 50 | //! - Module [`event`](event/index.html) |
| 51 | //! - Keyboard events - |
| 52 | //! [`PushKeyboardEnhancementFlags`](event/struct.PushKeyboardEnhancementFlags.html), |
| 53 | //! [`PopKeyboardEnhancementFlags`](event/struct.PopKeyboardEnhancementFlags.html) |
| 54 | //! - Mouse events - [`EnableMouseCapture`](event/struct.EnableMouseCapture.html), |
| 55 | //! [`DisableMouseCapture`](event/struct.DisableMouseCapture.html) |
| 56 | //! - Module [`style`](style/index.html) |
| 57 | //! - Colors - [`SetForegroundColor`](style/struct.SetForegroundColor.html), |
| 58 | //! [`SetBackgroundColor`](style/struct.SetBackgroundColor.html), |
| 59 | //! [`ResetColor`](style/struct.ResetColor.html), [`SetColors`](style/struct.SetColors.html) |
| 60 | //! - Attributes - [`SetAttribute`](style/struct.SetAttribute.html), [`SetAttributes`](style/struct.SetAttributes.html), |
| 61 | //! [`PrintStyledContent`](style/struct.PrintStyledContent.html) |
| 62 | //! - Module [`terminal`](terminal/index.html) |
| 63 | //! - Scrolling - [`ScrollUp`](terminal/struct.ScrollUp.html), |
| 64 | //! [`ScrollDown`](terminal/struct.ScrollDown.html) |
| 65 | //! - Miscellaneous - [`Clear`](terminal/struct.Clear.html), |
| 66 | //! [`SetSize`](terminal/struct.SetSize.html), |
| 67 | //! [`SetTitle`](terminal/struct.SetTitle.html), |
| 68 | //! [`DisableLineWrap`](terminal/struct.DisableLineWrap.html), |
| 69 | //! [`EnableLineWrap`](terminal/struct.EnableLineWrap.html) |
| 70 | //! - Alternate screen - [`EnterAlternateScreen`](terminal/struct.EnterAlternateScreen.html), |
| 71 | //! [`LeaveAlternateScreen`](terminal/struct.LeaveAlternateScreen.html) |
| 72 | //! - Module [`clipboard`](clipboard/index.html) (requires |
| 73 | //! [`feature = "osc52"`](#optional-features)) |
| 74 | //! - Clipboard - [`CopyToClipboard`](clipboard/struct.CopyToClipboard.html) |
| 75 | //! |
| 76 | //! ### Command Execution |
| 77 | //! |
| 78 | //! There are two different ways to execute commands: |
| 79 | //! |
| 80 | //! * [Lazy Execution](#lazy-execution) |
| 81 | //! * [Direct Execution](#direct-execution) |
| 82 | //! |
| 83 | //! #### Lazy Execution |
| 84 | //! |
| 85 | //! Flushing bytes to the terminal buffer is a heavy system call. If we perform a lot of actions with the terminal, |
| 86 | //! we want to do this periodically - like with a TUI editor - so that we can flush more data to the terminal buffer |
| 87 | //! at the same time. |
| 88 | //! |
| 89 | //! Crossterm offers the possibility to do this with `queue`. |
| 90 | //! With `queue` you can queue commands, and when you call [Write::flush][flush] these commands will be executed. |
| 91 | //! |
| 92 | //! You can pass a custom buffer implementing [std::io::Write][write] to this `queue` operation. |
| 93 | //! The commands will be executed on that buffer. |
| 94 | //! The most common buffer is [std::io::stdout][stdout] however, [std::io::stderr][stderr] is used sometimes as well. |
| 95 | //! |
| 96 | //! ##### Examples |
| 97 | //! |
| 98 | //! A simple demonstration that shows the command API in action with cursor commands. |
| 99 | //! |
| 100 | //! Functions: |
| 101 | //! |
| 102 | //! ```no_run |
| 103 | //! use std::io::{Write, stdout}; |
| 104 | //! use crossterm::{QueueableCommand, cursor}; |
| 105 | //! |
| 106 | //! let mut stdout = stdout(); |
| 107 | //! stdout.queue(cursor::MoveTo(5,5)); |
| 108 | //! |
| 109 | //! // some other code ... |
| 110 | //! |
| 111 | //! stdout.flush(); |
| 112 | //! ``` |
| 113 | //! |
| 114 | //! The [queue](./trait.QueueableCommand.html) function returns itself, therefore you can use this to queue another |
| 115 | //! command. Like `stdout.queue(Goto(5,5)).queue(Clear(ClearType::All))`. |
| 116 | //! |
| 117 | //! Macros: |
| 118 | //! |
| 119 | //! ```no_run |
| 120 | //! use std::io::{Write, stdout}; |
| 121 | //! use crossterm::{queue, QueueableCommand, cursor}; |
| 122 | //! |
| 123 | //! let mut stdout = stdout(); |
| 124 | //! queue!(stdout, cursor::MoveTo(5, 5)); |
| 125 | //! |
| 126 | //! // some other code ... |
| 127 | //! |
| 128 | //! // move operation is performed only if we flush the buffer. |
| 129 | //! stdout.flush(); |
| 130 | //! ``` |
| 131 | //! |
| 132 | //! You can pass more than one command into the [queue](./macro.queue.html) macro like |
| 133 | //! `queue!(stdout, MoveTo(5, 5), Clear(ClearType::All))` and |
| 134 | //! they will be executed in the given order from left to right. |
| 135 | //! |
| 136 | //! #### Direct Execution |
| 137 | //! |
| 138 | //! For many applications it is not at all important to be efficient with 'flush' operations. |
| 139 | //! For this use case there is the `execute` operation. |
| 140 | //! This operation executes the command immediately, and calls the `flush` under water. |
| 141 | //! |
| 142 | //! You can pass a custom buffer implementing [std::io::Write][write] to this `execute` operation. |
| 143 | //! The commands will be executed on that buffer. |
| 144 | //! The most common buffer is [std::io::stdout][stdout] however, [std::io::stderr][stderr] is used sometimes as well. |
| 145 | //! |
| 146 | //! ##### Examples |
| 147 | //! |
| 148 | //! Functions: |
| 149 | //! |
| 150 | //! ```no_run |
| 151 | //! use std::io::{Write, stdout}; |
| 152 | //! use crossterm::{ExecutableCommand, cursor}; |
| 153 | //! |
| 154 | //! let mut stdout = stdout(); |
| 155 | //! stdout.execute(cursor::MoveTo(5,5)); |
| 156 | //! ``` |
| 157 | //! The [execute](./trait.ExecutableCommand.html) function returns itself, therefore you can use this to queue |
| 158 | //! another command. Like `stdout.execute(Goto(5,5))?.execute(Clear(ClearType::All))`. |
| 159 | //! |
| 160 | //! Macros: |
| 161 | //! |
| 162 | //! ```no_run |
| 163 | //! use std::io::{stdout, Write}; |
| 164 | //! use crossterm::{execute, ExecutableCommand, cursor}; |
| 165 | //! |
| 166 | //! let mut stdout = stdout(); |
| 167 | //! execute!(stdout, cursor::MoveTo(5, 5)); |
| 168 | //! ``` |
| 169 | //! You can pass more than one command into the [execute](./macro.execute.html) macro like |
| 170 | //! `execute!(stdout, MoveTo(5, 5), Clear(ClearType::All))` and they will be executed in the given order from |
| 171 | //! left to right. |
| 172 | //! |
| 173 | //! ## Examples |
| 174 | //! |
| 175 | //! Print a rectangle colored with magenta and use both direct execution and lazy execution. |
| 176 | //! |
| 177 | //! Functions: |
| 178 | //! |
| 179 | //! ```no_run |
| 180 | //! use std::io::{self, Write}; |
| 181 | //! use crossterm::{ |
| 182 | //! ExecutableCommand, QueueableCommand, |
| 183 | //! terminal, cursor, style::{self, Stylize} |
| 184 | //! }; |
| 185 | //! |
| 186 | //! fn main() -> io::Result<()> { |
| 187 | //! let mut stdout = io::stdout(); |
| 188 | //! |
| 189 | //! stdout.execute(terminal::Clear(terminal::ClearType::All))?; |
| 190 | //! |
| 191 | //! for y in 0..40 { |
| 192 | //! for x in 0..150 { |
| 193 | //! if (y == 0 || y == 40 - 1) || (x == 0 || x == 150 - 1) { |
| 194 | //! // in this loop we are more efficient by not flushing the buffer. |
| 195 | //! stdout |
| 196 | //! .queue(cursor::MoveTo(x,y))? |
| 197 | //! .queue(style::PrintStyledContent( "█".magenta()))?; |
| 198 | //! } |
| 199 | //! } |
| 200 | //! } |
| 201 | //! stdout.flush()?; |
| 202 | //! Ok(()) |
| 203 | //! } |
| 204 | //! ``` |
| 205 | //! |
| 206 | //! Macros: |
| 207 | //! |
| 208 | //! ```no_run |
| 209 | //! use std::io::{self, Write}; |
| 210 | //! use crossterm::{ |
| 211 | //! execute, queue, |
| 212 | //! style::{self, Stylize}, cursor, terminal |
| 213 | //! }; |
| 214 | //! |
| 215 | //! fn main() -> io::Result<()> { |
| 216 | //! let mut stdout = io::stdout(); |
| 217 | //! |
| 218 | //! execute!(stdout, terminal::Clear(terminal::ClearType::All))?; |
| 219 | //! |
| 220 | //! for y in 0..40 { |
| 221 | //! for x in 0..150 { |
| 222 | //! if (y == 0 || y == 40 - 1) || (x == 0 || x == 150 - 1) { |
| 223 | //! // in this loop we are more efficient by not flushing the buffer. |
| 224 | //! queue!(stdout, cursor::MoveTo(x,y), style::PrintStyledContent( "█".magenta()))?; |
| 225 | //! } |
| 226 | //! } |
| 227 | //! } |
| 228 | //! stdout.flush()?; |
| 229 | //! Ok(()) |
| 230 | //! } |
| 231 | //!``` |
| 232 | //! ## Feature Flags |
| 233 | #![doc = document_features::document_features!()] |
| 234 | //! |
| 235 | //! [write]: https://doc.rust-lang.org/std/io/trait.Write.html |
| 236 | //! [stdout]: https://doc.rust-lang.org/std/io/fn.stdout.html |
| 237 | //! [stderr]: https://doc.rust-lang.org/std/io/fn.stderr.html |
| 238 | //! [flush]: https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.flush |
| 239 | |
| 240 | pub use crate::command::{Command, ExecutableCommand, QueueableCommand, SynchronizedUpdate}; |
| 241 | |
| 242 | /// A module to work with the terminal cursor |
| 243 | pub mod cursor; |
| 244 | /// A module to read events. |
| 245 | #[cfg(feature = "events")] |
| 246 | pub mod event; |
| 247 | /// A module to apply attributes and colors on your text. |
| 248 | pub mod style; |
| 249 | /// A module to work with the terminal. |
| 250 | pub mod terminal; |
| 251 | |
| 252 | /// A module to query if the current instance is a tty. |
| 253 | pub mod tty; |
| 254 | |
| 255 | /// A module for clipboard interaction |
| 256 | #[cfg(feature = "osc52")] |
| 257 | pub mod clipboard; |
| 258 | |
| 259 | #[cfg(windows)] |
| 260 | /// A module that exposes one function to check if the current terminal supports ANSI sequences. |
| 261 | pub mod ansi_support; |
| 262 | mod command; |
| 263 | pub(crate) mod macros; |
| 264 | |
| 265 | #[cfg(all(windows, not(feature = "windows")))] |
| 266 | compile_error!("Compiling on Windows with \"windows\" feature disabled. Feature \"windows\" should only be disabled when project will never be compiled on Windows."); |
| 267 |