From 723626fa1737dccbf71f3e84bc99dc2961d099b7 Mon Sep 17 00:00:00 2001 From: Timon Date: Thu, 25 Jul 2019 19:57:14 +0200 Subject: [PATCH] Some fixes (#183) --- README.md | 14 --- crossterm_cursor/README.md | 5 + crossterm_cursor/src/cursor/ansi_cursor.rs | 8 +- crossterm_cursor/src/cursor/mod.rs | 2 +- crossterm_cursor/src/sys/mod.rs | 5 + crossterm_cursor/src/sys/unix.rs | 14 ++- crossterm_cursor/src/sys/winapi.rs | 5 + crossterm_input/src/input/mod.rs | 8 +- crossterm_style/README.md | 2 +- crossterm_style/src/ansi_color.rs | 8 +- crossterm_style/src/enums/attribute.rs | 2 +- crossterm_style/src/enums/color.rs | 2 +- crossterm_style/src/enums/colored.rs | 1 + crossterm_terminal/README.md | 6 ++ crossterm_terminal/src/terminal/mod.rs | 2 +- docs/CHANGELOG.md | 7 ++ docs/mdbook/src/SUMMARY.md | 1 - docs/mdbook/src/feature_flags.md | 12 ++- docs/mdbook/src/input.md | 4 +- docs/mdbook/src/screen.md | 4 +- docs/mdbook/src/styling.md | 103 ++++++++++++++++++++- docs/mdbook/src/styling_example.md | 100 -------------------- examples/cursor.rs | 2 +- examples/style.rs | 4 +- examples/terminal.rs | 12 +-- 25 files changed, 175 insertions(+), 158 deletions(-) delete mode 100644 docs/mdbook/src/styling_example.md diff --git a/README.md b/README.md index b1b0b5a..1e57fb3 100644 --- a/README.md +++ b/README.md @@ -109,20 +109,6 @@ These are some basic examples demonstrating how to use this crate. See [examples My first recommendation is to use the [command API](https://timonpost.github.io/crossterm/docs/command.html) because this might replace some of the existing API in the future. Because it is more convenient, faster, and easier to use. -### Crossterm Type -This is a wrapper for all the modules crossterm provides like terminal, cursor, styling, and input. - -Good documentation can be found at the following places: [docs](https://docs.rs/crossterm/), [examples](https://github.com/TimonPost/crossterm/blob/master/examples/crossterm.rs). - -```rust -let crossterm = Crossterm::new(); - -let color = crossterm.color(); -let cursor = crossterm.cursor(); -let terminal = crossterm.terminal(); -let input = crossterm.input(); -``` - ### Styled Text This module enables you to style the terminal text. diff --git a/crossterm_cursor/README.md b/crossterm_cursor/README.md index 92ef05d..eaa6486 100644 --- a/crossterm_cursor/README.md +++ b/crossterm_cursor/README.md @@ -74,6 +74,11 @@ These are the features of this crate: - Store cursor position and resetting to that later - Hiding/Showing - Blinking Cursor (only some terminals are supporting this) + +## Command API + +My first recommendation is to use the [command API](https://timonpost.github.io/crossterm/docs/command.html) because this might replace some of the existing API in the future. +Because it is more convenient, faster, and easier to use. ## Examples The [examples](./examples) folder has more complete and verbose examples. diff --git a/crossterm_cursor/src/cursor/ansi_cursor.rs b/crossterm_cursor/src/cursor/ansi_cursor.rs index da99dde..b96c048 100644 --- a/crossterm_cursor/src/cursor/ansi_cursor.rs +++ b/crossterm_cursor/src/cursor/ansi_cursor.rs @@ -3,10 +3,10 @@ //! Note that the cursor position is 0 based. This means that we start counting at 0 when setting the cursor position etc. use super::ITerminalCursor; -use crate::sys::get_cursor_position; +use crate::sys::{get_cursor_position, show_cursor}; use std::io::Write; -use crossterm_utils::{write_cout, ErrorKind, Result}; +use crossterm_utils::{write_cout, Result}; #[inline] pub fn get_goto_ansi(x: u16, y: u16) -> String { @@ -86,12 +86,12 @@ impl ITerminalCursor for AnsiCursor { } fn hide(&self) -> Result<()> { - write_cout!(HIDE_ANSI)?; + show_cursor(false)?; Ok(()) } fn show(&self) -> Result<()> { - write_cout!(SHOW_ANSI)?; + show_cursor(true)?; Ok(()) } diff --git a/crossterm_cursor/src/cursor/mod.rs b/crossterm_cursor/src/cursor/mod.rs index 8877633..f6c5039 100644 --- a/crossterm_cursor/src/cursor/mod.rs +++ b/crossterm_cursor/src/cursor/mod.rs @@ -21,7 +21,7 @@ pub use self::cursor::{ TerminalCursor, Up, }; -use crossterm_utils::{Command, Result}; +use crossterm_utils::Result; ///! This trait defines the actions that can be performed with the terminal cursor. ///! This trait can be implemented so that a concrete implementation of the ITerminalCursor can fulfill diff --git a/crossterm_cursor/src/sys/mod.rs b/crossterm_cursor/src/sys/mod.rs index 6cc3a17..30d9af3 100644 --- a/crossterm_cursor/src/sys/mod.rs +++ b/crossterm_cursor/src/sys/mod.rs @@ -8,3 +8,8 @@ pub mod winapi; pub use self::unix::get_cursor_position; #[cfg(windows)] pub use self::winapi::get_cursor_position; + +#[cfg(unix)] +pub use self::unix::show_cursor; +#[cfg(windows)] +pub use self::winapi::show_cursor; \ No newline at end of file diff --git a/crossterm_cursor/src/sys/unix.rs b/crossterm_cursor/src/sys/unix.rs index c5bd814..0eb14d0 100644 --- a/crossterm_cursor/src/sys/unix.rs +++ b/crossterm_cursor/src/sys/unix.rs @@ -1,7 +1,6 @@ -use crossterm_utils::sys::unix::{self, RAW_MODE_ENABLED}; +use crossterm_utils::{Result, sys::unix::{self, RAW_MODE_ENABLED}}; use std::io::{self, BufRead, Write}; -/// Get the cursor position based on the current platform. #[cfg(unix)] pub fn get_cursor_position() -> (u16, u16) { if unsafe { RAW_MODE_ENABLED } { @@ -19,6 +18,17 @@ pub fn get_cursor_position() -> (u16, u16) { } } +#[cfg(unix)] +pub fn show_cursor(show_cursor: bool) -> Result<()> { + if show_cursor { + write_cout!(csi!("?25h"))?; + } + else { + write_cout!(csi!("?25l"))?; + } + Ok(()) +} + pub fn pos() -> io::Result<(u16, u16)> { unix::into_raw_mode()?; let pos = pos_raw(); diff --git a/crossterm_cursor/src/sys/winapi.rs b/crossterm_cursor/src/sys/winapi.rs index 9787526..e8b1af1 100644 --- a/crossterm_cursor/src/sys/winapi.rs +++ b/crossterm_cursor/src/sys/winapi.rs @@ -9,6 +9,11 @@ pub fn get_cursor_position() -> (u16, u16) { } } +#[cfg(windows)] +pub fn show_cursor(show_cursor: bool) -> Result<()> { + Cursor::from(Handle::current_out_handle()?).set_visibility(show_cursor) +} + pub use crossterm_winapi::{is_true, Coord, Handle, HandleType, ScreenBuffer}; use winapi::{ diff --git a/crossterm_input/src/input/mod.rs b/crossterm_input/src/input/mod.rs index 80cfd04..0b01b43 100644 --- a/crossterm_input/src/input/mod.rs +++ b/crossterm_input/src/input/mod.rs @@ -52,7 +52,7 @@ trait ITerminalInput { } /// Enum to specify which input event has occurred. -#[derive(Debug, PartialOrd, PartialEq, Hash)] +#[derive(Debug, PartialOrd, PartialEq, Hash, Clone)] pub enum InputEvent { /// A single key or a combination is pressed. Keyboard(KeyEvent), @@ -65,7 +65,7 @@ pub enum InputEvent { } /// Enum to specify which mouse event has occurred. -#[derive(Debug, PartialOrd, PartialEq, Hash)] +#[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)] pub enum MouseEvent { /// A mouse press has occurred, this contains the pressed button and the position of the press. Press(MouseButton, u16, u16), @@ -78,7 +78,7 @@ pub enum MouseEvent { } /// Enum to define mouse buttons. -#[derive(Debug, PartialOrd, PartialEq, Hash)] +#[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)] pub enum MouseButton { /// Left mouse button Left, @@ -93,7 +93,7 @@ pub enum MouseButton { } /// Enum with different key or key combinations. -#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)] pub enum KeyEvent { Backspace, Left, diff --git a/crossterm_style/README.md b/crossterm_style/README.md index e63a2af..3e2d003 100644 --- a/crossterm_style/README.md +++ b/crossterm_style/README.md @@ -78,7 +78,7 @@ These are the features of this crate: - 256 (ANSI) Color Support (Windows 10 and UNIX Only) - RGB Color Support (Windows 10 and UNIX only) - Text Attributes: bold, italic, underscore and crossed word and [more](https://timonpost.github.io/crossterm/docs/styling.html#attributes) (Windows 10 and UNIX only) - + ## Examples The [examples](./examples) folder has more complete and verbose examples. diff --git a/crossterm_style/src/ansi_color.rs b/crossterm_style/src/ansi_color.rs index 42e236c..e96c6b3 100644 --- a/crossterm_style/src/ansi_color.rs +++ b/crossterm_style/src/ansi_color.rs @@ -2,7 +2,7 @@ //! This module is used for Windows 10 terminals and Unix terminals by default. use crate::{Attribute, Color, ITerminalColor}; -use crossterm_utils::Result; +use crossterm_utils::{Result, write_cout}; use crate::Colored; use std::io::Write; @@ -35,17 +35,17 @@ impl AnsiColor { impl ITerminalColor for AnsiColor { fn set_fg(&self, fg_color: Color) -> Result<()> { - write!(std::io::stdout(), "{}", get_set_fg_ansi(fg_color))?; + write_cout!(get_set_fg_ansi(fg_color))?; Ok(()) } fn set_bg(&self, bg_color: Color) -> Result<()> { - write!(std::io::stdout(), "{}", get_set_bg_ansi(bg_color))?; + write_cout!(get_set_bg_ansi(bg_color))?; Ok(()) } fn reset(&self) -> Result<()> { - write!(std::io::stdout(), "{}", RESET_ANSI)?; + write_cout!(RESET_ANSI)?; Ok(()) } } diff --git a/crossterm_style/src/enums/attribute.rs b/crossterm_style/src/enums/attribute.rs index 1ffc2fb..066b6e3 100644 --- a/crossterm_style/src/enums/attribute.rs +++ b/crossterm_style/src/enums/attribute.rs @@ -28,7 +28,7 @@ use std::fmt::Display; /// println!("{}", style("Underlined text").underlined()); /// println!("{}", style("Negative text").negative()); /// ``` -#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum Attribute { /// All attributes off /// [info]: This will reset all current set attributes. diff --git a/crossterm_style/src/enums/color.rs b/crossterm_style/src/enums/color.rs index e7f1211..25a693a 100644 --- a/crossterm_style/src/enums/color.rs +++ b/crossterm_style/src/enums/color.rs @@ -2,7 +2,7 @@ use std::convert::AsRef; use std::str::FromStr; /// Enum with the different colors to color your test and terminal. -#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum Color { // This resets the color. Reset, diff --git a/crossterm_style/src/enums/colored.rs b/crossterm_style/src/enums/colored.rs index 521482d..a316193 100644 --- a/crossterm_style/src/enums/colored.rs +++ b/crossterm_style/src/enums/colored.rs @@ -22,6 +22,7 @@ use std::fmt::Display; /// let styled_text = "Red forground color on blue background.".red().on_blue(); /// println!("{}", styled_text); /// ``` +#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum Colored { Fg(Color), Bg(Color), diff --git a/crossterm_terminal/README.md b/crossterm_terminal/README.md index ffb94b1..7ab8fa0 100644 --- a/crossterm_terminal/README.md +++ b/crossterm_terminal/README.md @@ -77,7 +77,13 @@ These are the features of this crate: - Terminal Size (get/set) - Exit Current Process +## Command API + +My first recommendation is to use the [command API](https://timonpost.github.io/crossterm/docs/command.html) because this might replace some of the existing API in the future. +Because it is more convenient, faster, and easier to use. + ## Examples + The [examples](./examples) folder has more complete and verbose examples. ```rust diff --git a/crossterm_terminal/src/terminal/mod.rs b/crossterm_terminal/src/terminal/mod.rs index bdfce39..4d141c8 100644 --- a/crossterm_terminal/src/terminal/mod.rs +++ b/crossterm_terminal/src/terminal/mod.rs @@ -17,7 +17,7 @@ pub use self::terminal::{terminal, Clear, ScrollDown, ScrollUp, SetSize, Termina use crossterm_utils::Result; /// Enum with the different values to clear the terminal. -#[derive(Clone)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum ClearType { /// clear all cells in terminal. All, diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index af90a0f..d58e327 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,3 +1,10 @@ +# Changes crossterm 0.10.0 +- Implemented command API, to have better performance and more control over how and when commands are executed. [PR](https://github.com/TimonPost/crossterm/commit/1a60924abd462ab169b6706aab68f4cca31d7bc2), [issue](https://github.com/TimonPost/crossterm/issues/171) +- Fixed showing, hiding cursor windows implementation +- Removed some of the parsing logic from windows keys to ansi codes to key events [PR](https://github.com/TimonPost/crossterm/commit/762c3a9b8e3d1fba87acde237f8ed09e74cd9ecd) +- Made terminal size 1-based [PR](https://github.com/TimonPost/crossterm/commit/d689d7e8ed46a335474b8262bd76f21feaaf0c50) +- Added some derive implementation + # Changes crossterm 0.9.6 - Copy for KeyEvent - CTRL + Left, Down, Up, Right key support diff --git a/docs/mdbook/src/SUMMARY.md b/docs/mdbook/src/SUMMARY.md index c9eee06..5e47232 100644 --- a/docs/mdbook/src/SUMMARY.md +++ b/docs/mdbook/src/SUMMARY.md @@ -4,6 +4,5 @@ This book will cover styling, user input, terminal modes, and feature flags. - [Feature Flags](feature_flags.md) - [Command API](command.md) - [Styling Output](styling.md) - - [example](styling_example.md) - [Reading Input Events](input.md) - [Alternate, Raw Screen](screen.md) diff --git a/docs/mdbook/src/feature_flags.md b/docs/mdbook/src/feature_flags.md index e7d2bed..c6ee0f6 100644 --- a/docs/mdbook/src/feature_flags.md +++ b/docs/mdbook/src/feature_flags.md @@ -1,6 +1,6 @@ From `crossterm 0.6` you are able to use feature flags. -With feature flags you can pick the features you want which reduces the size of the library and could prevent you from having unnecessary dependencies. +With feature flags, you can pick the features you want which reduces the size of the library and could prevent you from having unnecessary dependencies. Crossterm provides the following feature flags: - input ; reading input events @@ -9,7 +9,7 @@ Crossterm provides the following feature flags: - cursor ; moving the terminal cursor - screen ; alternate and raw screen -By default all of those will be enabled. +By default, all of those will be enabled. _Cargo.toml_ @@ -21,13 +21,15 @@ crossterm = { version="0.9", default-features = false, features = ["screen", "te By default all flags are enabled, the types and functions available to use depend on the specified flags. ```rust -"cursor" => cursor, TerminalCursor +"cursor" => cursor, BlinkOff, BlinkOn, Down, Goto, Hide, Left, ResetPos, Right, SavePos, Show, TerminalCursor, Up, "input" => input, AsyncReader, InputEvent, KeyEvent, MouseButton, MouseEvent, SyncReader, TerminalInput "screen" => AlternateScreen, IntoRawMode, RawScreen -"style" => color, style, Attribute, Color, Colored, Colorize, ObjectStyle, StyledObject, Styler, TerminalColor, -"terminal" => terminal, ClearType, Terminal +"style" => style, Attribute, Color, Colored, Colorize, ObjectStyle, StyledObject, Styler, color, PrintStyledFont, SetAttr, SetBg, SetFg, TerminalColor +"terminal" => terminal, Clear, ClearType, ScrollDown, ScrollUp, SetSize, Terminal ``` +All modules export types for the ability to use the command api, those are: `execute, queue, Command, ExecutableCommand, QueueableCommand` + You can also use all the crossterm modules individually by directly referencing the crate. - [Crossterm Style](https://crates.io/crates/crossterm_style) diff --git a/docs/mdbook/src/input.md b/docs/mdbook/src/input.md index 8878b53..70c4fa1 100644 --- a/docs/mdbook/src/input.md +++ b/docs/mdbook/src/input.md @@ -9,11 +9,11 @@ Crossterm provides two ways to read user input, synchronous and asynchronous. Read the input synchronously from the user, the reads performed will be blocking calls. Using synchronous over asynchronous reading has the benefit that it is using fewer resources than the asynchronous because background thread and queues are left away. -You can get a synchronous event reader by calling: `TerminalInput::read_sync`. +You can get asynchronous event reader by calling: `TerminalInput::read_sync`. ### Asynchronous reading -Read the input asynchronously, input events are gathered on the background and will be queued for you to read. +Read the input asynchronously, input events are gathered in the background and will be queued for you to read. Using asynchronous reading has the benefit that input events are queued until you read them. You can poll for occurred events, and the reads won't block your program. You can get a synchronous event reader by calling: `TerminalInput::read_async`, `TerminalInput::read_async_until`. diff --git a/docs/mdbook/src/screen.md b/docs/mdbook/src/screen.md index 6c0573f..1309dbe 100644 --- a/docs/mdbook/src/screen.md +++ b/docs/mdbook/src/screen.md @@ -1,6 +1,6 @@ ## Screen Buffer A screen buffer is a two-dimensional array of characters and color data to be output in a console window. -An terminal can have multiple of those screen buffers, and the active screen buffer is the one that is displayed on the screen. +A terminal can have multiple of those screen buffers, and the active screen buffer is the one that is displayed on the screen. Crossterm allows you to switch between those buffers; the screen you are working in is called the 'main screen'. We call the other screen the 'alternate screen'. One note to take is that crossterm does not support the creation and switching between several buffers. @@ -26,7 +26,7 @@ Normally the terminals use line buffering. This means that the input will be sen **Characters** -The characters are not processed by the terminal driver. Also, special character have no meaning. For example, backspace will not be interpreted as backspace but instead will be sent directly to the terminal. +The characters are not processed by the terminal driver. Also, special character has no meaning. For example, backspace will not be interpreted as backspace but instead will be sent directly to the terminal. **Escape Characters** Note that in raw mode `\n` `\r` will move the cursor to a new line but it will be at the same position as it was on the previous line. diff --git a/docs/mdbook/src/styling.md b/docs/mdbook/src/styling.md index 59fbf36..6e7abe5 100644 --- a/docs/mdbook/src/styling.md +++ b/docs/mdbook/src/styling.md @@ -49,7 +49,106 @@ Crossterm implements almost all attributes shown in this [Wikipedia-list](https: (There are a few attributes who disable one of the above attributes, I did not write those down to keep the list short). -Now we have covered the basics of styling lets go some [examples](styling_example.md). +Now we have covered the basics of styling lets go over to some examples. +# Example + +_setup the basics_ +```rust +extern crate crossterm; + +use crossterm::{Colored, Color, Attribute, Styler, Colorize}; + +fn main() { + /* your code here */ +} +``` + +There are a couple of ways to style the terminal output with crossterm. The most important part of the styling module is `StyledObject`. + +A `StyledObject` is just a wrapper crossterm uses to store the text and style together. +A `StyledObject` implements `Display` and thus you could use it inside `print!`, `println!` etc. + +Without further ado let's get straight into it. + +## Coloring + +There are a few ways to do the coloring, the first one is by using the `Colored` enum. + +### Using Enum +```rust +println!("{} Red foreground color", Colored::Fg(Color::Red)); +println!("{} Blue background color", Colored::Bg(Color::Blue)); +``` +`Colored::Bg` will set the background color, and `Colored::Fg` will set the foreground color to the provided color. +The provided color is of type `Color` and has a bunch of enum values you could choose out. + +Because `Colored` implements `Display` you are able to use it inside any write statement. + +### Using Methods +You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also color the only piece of text. +(Make sure to include the `crossterm::Coloring` trait). + +```rust +let styled_text = "Red forground color on blue background.".red().on_blue(); +println!("{}", styled_text); +``` + +As you see in the above example you could call coloring methods on a string. How is this possible you might ask..? +Well, the trait `Coloring`, who you need to include, is implemented for `&'static str`. +When calling a method on this string crossterm transforms it into a `StyledObject` who you could use in your write statements. + + +### RGB +Most UNIX terminals and all Windows 10 consoles are supporting [True color(24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme. +You can set the color of the terminal by using `Color::RGB(r,g,b)`. + +``` +// custom rgb value (Windows 10 and UNIX systems) +println!("{}{} 'Light green' text on 'Black' background", Colored::Fg(Color::Rgb { r: 0, g: 255, b: 128 }), Colored::Bg(Color::Rgb {r: 0, g: 0, b: 0})); +``` +This will print some light green text on black background. + +### Custom ANSI color value +When working on UNIX or Windows 10 you could also specify a custom ANSI value ranging up from 0 to 256. +See [256 (Xterm, 8-bit) colors](https://jonasjacek.github.io/colors/) for more information. + +``` +// custom ansi color value (Windows 10 and UNIX systems) +println!("{} some colored text", Colored::Fg(Color::AnsiValue(10))); +``` + +## Attributes +When working with UNIX or Windows 10 terminals you could also use attributes to style your text. For example, you could cross your text with a line and make it bold. +See [this](styling.md#Attributes) for more information. + +### Using Enum +You could use the `Attribute` enum for styling text with attributes. +`Attribute` implements `Display`, thus crossterm will enable the attribute style when using it in any writing operation. + +``` +println!( + "{} Underlined {} No Underline", + Attribute::Underlined, + Attribute::NoUnderline +); +``` + +### Using Method + +You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also style only one piece of text. +(Make sure to include the `crossterm::Styler` trait). + +``` +println!("{}", "Bold text".bold(); +println!("{}", "Underlined text".underlined(); +println!("{}", "Negative text".negative(); +``` + +As you see in the above example you could call attributes methods on a string. How is this possible you might ask..? +Well, the trait `Styling`, who you need to include, is implemented for `&'static str`. +When calling a method on any string crossterm transforms will transform it into a `StyledObject` who you could use in your write statements. + --------------------------------------------------------------------------------------------------------------------------------------------- -Next up: [Examples](styling_example.md) +More examples could be found at this [link](https://github.com/TimonPost/crossterm/blob/master/examples/style.rs). + diff --git a/docs/mdbook/src/styling_example.md b/docs/mdbook/src/styling_example.md deleted file mode 100644 index b282d76..0000000 --- a/docs/mdbook/src/styling_example.md +++ /dev/null @@ -1,100 +0,0 @@ -# Example - -_setup the basics_ -```rust -extern crate crossterm; - -use crossterm::{Colored, Color, Attribute, Styler, Colorize}; - -fn main() { - /* your code here */ -} -``` - -There are a couple of ways to style the terminal output with crossterm. The most important part of the styling module is `StyledObject`. - -A `StyledObject` is just a wrapper crossterm uses to store the text and style together. -A `StyledObject` implements `Display` and thus you could use it inside `print!`, `println!` etc. - -Without further ado let's get straight into it. - -## Coloring - -There are a few ways to do the coloring, the first one is by using the `Colored` enum. - -### Using Enum -```rust -println!("{} Red foreground color", Colored::Fg(Color::Red)); -println!("{} Blue background color", Colored::Bg(Color::Blue)); -``` -`Colored::Bg` will set the background color, and `Colored::Fg` will set the foreground color to the provided color. -The provided color is of type `Color` and has a bunch of enum values you could choose out. - -Because `Colored` implements `Display` you are able to use it inside any write statement. - -### Using Methods -You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also color the only piece of text. -(Make sure to include the `crossterm::Coloring` trait). - -```rust -let styled_text = "Red forground color on blue background.".red().on_blue(); -println!("{}", styled_text); -``` - -As you see in the above example you could call coloring methods on a string. How is this possible you might ask..? -Well, the trait `Coloring`, who you need to include, is implemented for `&'static str`. -When calling a method on this string crossterm transforms it into a `StyledObject` who you could use in your write statements. - - -### RGB -Most UNIX terminals and all Windows 10 consoles are supporting [True color(24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme. -You can set the color of the terminal by using `Color::RGB(r,g,b)`. - -``` -// custom rgb value (Windows 10 and UNIX systems) -println!("{}{} 'Light green' text on 'Black' background", Colored::Fg(Color::Rgb { r: 0, g: 255, b: 128 }), Colored::Bg(Color::Rgb {r: 0, g: 0, b: 0})); -``` -This will print some light green text on black background. - -### Custom ANSI color value -When working on UNIX or Windows 10 you could also specify a custom ANSI value ranging up from 0 to 256. -See [256 (Xterm, 8-bit) colors](https://jonasjacek.github.io/colors/) for more information. - -``` -// custom ansi color value (Windows 10 and UNIX systems) -println!("{} some colored text", Colored::Fg(Color::AnsiValue(10))); -``` - -## Attributes -When working with UNIX or Windows 10 terminals you could also use attributes to style your text. For example, you could cross your text with a line and make it bold. -See [this](styling.md#Attributes) for more information. - -### Using Enum -You could use the `Attribute` enum for styling text with attributes. -`Attribute` implements `Display`, thus crossterm will enable the attribute style when using it in any writing operation. - -``` -println!( - "{} Underlined {} No Underline", - Attribute::Underlined, - Attribute::NoUnderline -); -``` - -### Using Method - -You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also style only one piece of text. -(Make sure to include the `crossterm::Styler` trait). - -``` -println!("{}", "Bold text".bold(); -println!("{}", "Underlined text".underlined(); -println!("{}", "Negative text".negative(); -``` - -As you see in the above example you could call attributes methods on a string. How is this possible you might ask..? -Well, the trait `Styling`, who you need to include, is implemented for `&'static str`. -When calling a method on any string crossterm transforms will transform it into a `StyledObject` who you could use in your write statements. - ---------------------------------------------------------------------------------------------------------------------------------------------- -More examples could be found at this [link](https://github.com/TimonPost/crossterm/blob/master/examples/style.rs). diff --git a/examples/cursor.rs b/examples/cursor.rs index 08974d0..be33a59 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -92,5 +92,5 @@ pub fn blink_cursor() { } fn main() { - pos() + hide_cursor() } diff --git a/examples/style.rs b/examples/style.rs index 5d4e056..8330221 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -4,6 +4,7 @@ extern crate crossterm; use self::crossterm::{color, Attribute, Color, Colored, Colorize, Styler}; +use std::io::stdout; /// print some red text | demonstration. pub fn paint_foreground() { @@ -411,5 +412,6 @@ pub fn reset_fg_and_bg() { } fn main() { - print_all_foreground_colors_with_method() + use std::io::Write; + print_all_background_colors_with_method() } diff --git a/examples/terminal.rs b/examples/terminal.rs index ecac273..e709ab3 100644 --- a/examples/terminal.rs +++ b/examples/terminal.rs @@ -129,16 +129,6 @@ pub fn scroll_up() -> io::Result<()> { Ok(()) } -/// Resize the terminal to X: 10, Y: 10 | demonstration. -pub fn resize_terminal() -> io::Result<()> { - let terminal = terminal(); - - // Get terminal size - terminal.set_size(10, 10)?; - - Ok(()) -} - /// exit the current proccess. pub fn exit() { let terminal = terminal(); @@ -146,5 +136,5 @@ pub fn exit() { } fn main() { - clear_until_new_line(); + scroll_down(); }