2018-11-29 02:42:05 +11:00
|
|
|
//! A module that contains all the actions related to the styling of the terminal.
|
2019-06-22 02:10:46 +10:00
|
|
|
//! Like applying attributes to text and changing the foreground and background.
|
2018-07-30 05:30:09 +10:00
|
|
|
|
2019-02-23 01:20:24 +11:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
2019-01-28 07:16:14 +11:00
|
|
|
mod color;
|
2019-02-23 01:20:24 +11:00
|
|
|
mod enums;
|
2018-07-29 03:26:35 +10:00
|
|
|
pub mod objectstyle;
|
|
|
|
pub mod styledobject;
|
2019-02-23 01:20:24 +11:00
|
|
|
mod traits;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2018-07-31 05:35:35 +10:00
|
|
|
mod ansi_color;
|
2019-04-11 07:46:30 +10:00
|
|
|
#[cfg(windows)]
|
2018-07-29 03:26:35 +10:00
|
|
|
mod winapi_color;
|
2018-01-04 00:43:54 +11:00
|
|
|
|
2019-09-14 07:27:19 +10:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2018-07-31 05:35:35 +10:00
|
|
|
use self::ansi_color::AnsiColor;
|
2019-04-11 07:46:30 +10:00
|
|
|
#[cfg(windows)]
|
2018-07-29 03:26:35 +10:00
|
|
|
use self::winapi_color::WinApiColor;
|
2019-09-14 07:27:19 +10:00
|
|
|
pub use crossterm_utils::{execute, queue, Command, ExecutableCommand, QueueableCommand, Result};
|
2018-01-26 04:26:08 +11:00
|
|
|
|
2019-07-25 04:10:27 +10:00
|
|
|
pub use self::color::{color, PrintStyledFont, SetAttr, SetBg, SetFg, TerminalColor};
|
2019-02-23 01:20:24 +11:00
|
|
|
pub use self::enums::{Attribute, Color, Colored};
|
2018-07-29 03:26:35 +10:00
|
|
|
pub use self::objectstyle::ObjectStyle;
|
2018-11-22 03:48:22 +11:00
|
|
|
pub use self::styledobject::StyledObject;
|
2019-02-23 01:20:24 +11:00
|
|
|
pub use self::traits::{Colorize, Styler};
|
2018-07-29 03:26:35 +10:00
|
|
|
|
2019-06-22 02:10:46 +10:00
|
|
|
/// This trait defines the actions that can be performed with terminal colors.
|
2018-11-29 02:42:05 +11:00
|
|
|
/// This trait can be implemented so that a concrete implementation of the ITerminalColor can fulfill
|
2019-06-22 02:10:46 +10:00
|
|
|
/// the wishes to work on a specific platform.
|
2018-07-30 05:30:09 +10:00
|
|
|
///
|
|
|
|
/// ## For example:
|
|
|
|
///
|
2018-11-29 02:42:05 +11:00
|
|
|
/// This trait is implemented for `WinApi` (Windows specific) and `ANSI` (Unix specific),
|
|
|
|
/// so that color-related actions can be performed on both UNIX and Windows systems.
|
2018-08-15 07:02:25 +10:00
|
|
|
trait ITerminalColor {
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Set the foreground color to the given color.
|
2019-04-11 07:46:30 +10:00
|
|
|
fn set_fg(&self, fg_color: Color) -> Result<()>;
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Set the background color to the given color.
|
2019-04-11 07:46:30 +10:00
|
|
|
fn set_bg(&self, fg_color: Color) -> Result<()>;
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Reset the terminal color to default.
|
2019-04-11 07:46:30 +10:00
|
|
|
fn reset(&self) -> Result<()>;
|
2018-07-29 03:26:35 +10:00
|
|
|
}
|
|
|
|
|
2019-06-22 02:10:46 +10:00
|
|
|
/// This could be used to style a type that implements `Display` with colors and attributes.
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
2019-01-28 07:16:14 +11:00
|
|
|
/// # Example
|
2019-09-15 00:42:42 +10:00
|
|
|
/// ```ignore
|
2019-06-22 02:10:46 +10:00
|
|
|
/// // get a styled object which could be painted to the terminal.
|
2019-01-28 07:16:14 +11:00
|
|
|
/// let styled_object = style("Some Blue colored text on black background")
|
2018-11-15 02:53:27 +11:00
|
|
|
/// .with(Color::Blue)
|
|
|
|
/// .on(Color::Black);
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
2019-06-22 02:10:46 +10:00
|
|
|
/// // print the styled text * times to the current screen.
|
2018-08-14 05:04:07 +10:00
|
|
|
/// for i in 1..10
|
|
|
|
/// {
|
2018-11-15 02:53:27 +11:00
|
|
|
/// println!("{}", styled_object);
|
2018-08-14 05:04:07 +10:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-02-23 01:20:24 +11:00
|
|
|
///
|
|
|
|
/// # Important Remark
|
|
|
|
///
|
|
|
|
/// - Please checkout the documentation for `Colorizer` or `Styler`.
|
|
|
|
/// Those types will make it a bit easier to style a string.
|
2018-11-22 03:48:22 +11:00
|
|
|
pub fn style<'a, D: 'a>(val: D) -> StyledObject<D>
|
|
|
|
where
|
2019-07-25 04:10:27 +10:00
|
|
|
D: Display + Clone,
|
2018-11-22 03:48:22 +11:00
|
|
|
{
|
|
|
|
ObjectStyle::new().apply_to(val)
|
2018-08-12 22:51:08 +10:00
|
|
|
}
|
|
|
|
|
2019-02-23 01:20:24 +11:00
|
|
|
impl Colorize<&'static str> for &'static str {
|
|
|
|
// foreground colors
|
|
|
|
def_str_color!(fg_color: black => Color::Black);
|
2019-04-29 15:34:27 +10:00
|
|
|
def_str_color!(fg_color: dark_grey => Color::DarkGrey);
|
2019-02-23 01:20:24 +11:00
|
|
|
def_str_color!(fg_color: red => Color::Red);
|
|
|
|
def_str_color!(fg_color: dark_red => Color::DarkRed);
|
|
|
|
def_str_color!(fg_color: green => Color::Green);
|
|
|
|
def_str_color!(fg_color: dark_green => Color::DarkGreen);
|
|
|
|
def_str_color!(fg_color: yellow => Color::Yellow);
|
|
|
|
def_str_color!(fg_color: dark_yellow => Color::DarkYellow);
|
|
|
|
def_str_color!(fg_color: blue => Color::Blue);
|
|
|
|
def_str_color!(fg_color: dark_blue => Color::DarkBlue);
|
|
|
|
def_str_color!(fg_color: magenta => Color::Magenta);
|
|
|
|
def_str_color!(fg_color: dark_magenta => Color::DarkMagenta);
|
|
|
|
def_str_color!(fg_color: cyan => Color::Cyan);
|
|
|
|
def_str_color!(fg_color: dark_cyan => Color::DarkCyan);
|
|
|
|
def_str_color!(fg_color: white => Color::White);
|
|
|
|
def_str_color!(fg_color: grey => Color::Grey);
|
|
|
|
|
|
|
|
// background colors
|
|
|
|
def_str_color!(bg_color: on_black => Color::Black);
|
2019-04-29 15:34:27 +10:00
|
|
|
def_str_color!(bg_color: on_dark_grey => Color::DarkGrey);
|
2019-02-23 01:20:24 +11:00
|
|
|
def_str_color!(bg_color: on_red => Color::Red);
|
|
|
|
def_str_color!(bg_color: on_dark_red => Color::DarkRed);
|
|
|
|
def_str_color!(bg_color: on_green => Color::Green);
|
|
|
|
def_str_color!(bg_color: on_dark_green => Color::DarkGreen);
|
|
|
|
def_str_color!(bg_color: on_yellow => Color::Yellow);
|
|
|
|
def_str_color!(bg_color: on_dark_yellow => Color::DarkYellow);
|
|
|
|
def_str_color!(bg_color: on_blue => Color::Blue);
|
|
|
|
def_str_color!(bg_color: on_dark_blue => Color::DarkBlue);
|
|
|
|
def_str_color!(bg_color: on_magenta => Color::Magenta);
|
|
|
|
def_str_color!(bg_color: on_dark_magenta => Color::DarkMagenta);
|
|
|
|
def_str_color!(bg_color: on_cyan => Color::Cyan);
|
|
|
|
def_str_color!(bg_color: on_dark_cyan => Color::DarkCyan);
|
|
|
|
def_str_color!(bg_color: on_white => Color::White);
|
|
|
|
def_str_color!(bg_color: on_grey => Color::Grey);
|
2018-01-26 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2019-02-23 01:20:24 +11:00
|
|
|
impl Styler<&'static str> for &'static str {
|
|
|
|
def_str_attr!(reset => Attribute::Reset);
|
|
|
|
def_str_attr!(bold => Attribute::Bold);
|
|
|
|
def_str_attr!(underlined => Attribute::Underlined);
|
|
|
|
def_str_attr!(reverse => Attribute::Reverse);
|
|
|
|
def_str_attr!(dim => Attribute::Dim);
|
|
|
|
def_str_attr!(italic => Attribute::Italic);
|
|
|
|
def_str_attr!(negative => Attribute::Reverse);
|
|
|
|
def_str_attr!(slow_blink => Attribute::SlowBlink);
|
|
|
|
def_str_attr!(rapid_blink => Attribute::RapidBlink);
|
|
|
|
def_str_attr!(hidden => Attribute::Hidden);
|
|
|
|
def_str_attr!(crossed_out => Attribute::CrossedOut);
|
2018-07-02 06:43:43 +10:00
|
|
|
}
|