2018-11-29 02:42:05 +11:00
|
|
|
//! A module that contains all the actions related to the styling of the terminal.
|
|
|
|
//! Like applying attributes to font and changing the foreground and background.
|
2018-07-30 05:30:09 +10:00
|
|
|
|
2018-07-29 03:26:35 +10:00
|
|
|
pub mod color;
|
|
|
|
pub mod objectstyle;
|
|
|
|
pub mod styledobject;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2018-07-31 05:35:35 +10:00
|
|
|
mod ansi_color;
|
2018-07-29 03:26:35 +10:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
mod winapi_color;
|
2018-01-04 00:43:54 +11:00
|
|
|
|
2018-07-31 05:35:35 +10:00
|
|
|
use self::ansi_color::AnsiColor;
|
2018-07-29 03:26:35 +10:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
use self::winapi_color::WinApiColor;
|
2018-01-26 04:26:08 +11:00
|
|
|
|
|
|
|
use std::convert::From;
|
2018-11-22 03:48:22 +11:00
|
|
|
use std::fmt::Display;
|
2018-01-26 04:26:08 +11:00
|
|
|
use std::str::FromStr;
|
2018-08-12 01:58:15 +10:00
|
|
|
use std::sync::Arc;
|
2018-01-26 04:26:08 +11:00
|
|
|
|
2018-11-22 03:48:22 +11:00
|
|
|
pub use self::color::{color, from_screen, TerminalColor};
|
2018-07-29 03:26:35 +10:00
|
|
|
pub use self::objectstyle::ObjectStyle;
|
2018-09-21 06:24:10 +10:00
|
|
|
pub use self::styledobject::DisplayableObject;
|
2018-11-22 03:48:22 +11:00
|
|
|
pub use self::styledobject::StyledObject;
|
2018-08-20 07:14:45 +10:00
|
|
|
use super::functions;
|
2018-09-22 05:36:25 +10:00
|
|
|
|
2018-08-20 07:14:45 +10:00
|
|
|
use TerminalOutput;
|
2018-07-29 03:26:35 +10:00
|
|
|
|
2018-11-29 02:42:05 +11:00
|
|
|
/// This trait defines the actions that can be preformed with terminal color.
|
|
|
|
/// This trait can be implemented so that a concrete implementation of the ITerminalColor can fulfill
|
2018-07-30 05:30:09 +10:00
|
|
|
/// the wishes to work on an specific platform.
|
|
|
|
///
|
|
|
|
/// ## 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.
|
2018-11-15 02:53:27 +11:00
|
|
|
fn set_fg(&self, fg_color: Color, stdout: &Option<&Arc<TerminalOutput>>);
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Set the background color to the given color.
|
2018-11-15 02:53:27 +11:00
|
|
|
fn set_bg(&self, fg_color: Color, stdout: &Option<&Arc<TerminalOutput>>);
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Reset the terminal color to default.
|
2018-11-15 02:53:27 +11:00
|
|
|
fn reset(&self, stdout: &Option<&Arc<TerminalOutput>>);
|
2018-07-29 03:26:35 +10:00
|
|
|
/// Gets an value that represents an color from the given `Color` and `ColorType`.
|
|
|
|
fn color_value(&self, color: Color, color_type: ColorType) -> String;
|
|
|
|
}
|
|
|
|
|
2018-11-15 02:53:27 +11:00
|
|
|
/// This could be used to style an `Displayable` type with colors and attributes.
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
|
|
|
/// ```rust
|
2018-11-15 02:53:27 +11:00
|
|
|
/// extern crate crossterm;
|
|
|
|
/// use crossterm::Crossterm;
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
2018-11-15 02:53:27 +11:00
|
|
|
/// let crossterm = Crossterm::new();
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
|
|
|
/// // get an styled object which could be painted to the terminal.
|
2018-11-15 02:53:27 +11:00
|
|
|
/// let styled_object = crossterm.style("Some Blue colored text on black background")
|
|
|
|
/// .with(Color::Blue)
|
|
|
|
/// .on(Color::Black);
|
2018-08-14 05:04:07 +10:00
|
|
|
///
|
|
|
|
/// // print the styled font * times to the current screen.
|
|
|
|
/// for i in 1..10
|
|
|
|
/// {
|
2018-11-15 02:53:27 +11:00
|
|
|
/// println!("{}", styled_object);
|
2018-08-14 05:04:07 +10:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-11-22 03:48:22 +11:00
|
|
|
pub fn style<'a, D: 'a>(val: D) -> StyledObject<D>
|
|
|
|
where
|
|
|
|
D: Display,
|
|
|
|
{
|
|
|
|
ObjectStyle::new().apply_to(val)
|
2018-08-12 22:51:08 +10:00
|
|
|
}
|
|
|
|
|
2018-12-31 10:13:45 +11:00
|
|
|
/// Attributes that could be applied on some text. (*nix values)
|
|
|
|
#[cfg(unix)]
|
2018-01-26 04:26:08 +11:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
|
|
|
pub enum Attribute {
|
|
|
|
Bold = 1,
|
|
|
|
Dim = 2,
|
|
|
|
Italic = 3,
|
|
|
|
Underlined = 4,
|
2018-01-28 04:26:08 +11:00
|
|
|
SlowBlink = 5,
|
|
|
|
RapidBlink = 6,
|
2018-01-26 04:26:08 +11:00
|
|
|
Reverse = 7,
|
|
|
|
Hidden = 8,
|
2018-07-02 06:43:43 +10:00
|
|
|
CrossedOut = 9,
|
2018-01-26 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2018-12-31 10:13:45 +11:00
|
|
|
/// Attributes that could be applied on some text. (Windows specific)
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
|
|
|
pub enum Attribute {
|
|
|
|
Reset = 0,
|
|
|
|
Bold = 1,
|
|
|
|
Underlined = 4,
|
|
|
|
NoUnderline = 24,
|
|
|
|
Negative = 7,
|
|
|
|
Positive = 27,
|
|
|
|
}
|
|
|
|
|
2018-07-30 05:30:09 +10:00
|
|
|
/// Colors that are available for coloring the terminal font.
|
2018-01-26 04:26:08 +11:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum Color {
|
|
|
|
Black,
|
|
|
|
|
|
|
|
Red,
|
|
|
|
DarkRed,
|
|
|
|
|
|
|
|
Green,
|
|
|
|
DarkGreen,
|
|
|
|
|
|
|
|
Yellow,
|
|
|
|
DarkYellow,
|
|
|
|
|
|
|
|
Blue,
|
|
|
|
DarkBlue,
|
|
|
|
|
|
|
|
Magenta,
|
|
|
|
DarkMagenta,
|
|
|
|
|
|
|
|
Cyan,
|
|
|
|
DarkCyan,
|
|
|
|
|
|
|
|
Grey,
|
|
|
|
White,
|
2018-01-28 04:26:08 +11:00
|
|
|
|
2018-12-29 00:58:09 +11:00
|
|
|
Rgb { r: u8, g: u8, b: u8 },
|
2018-07-02 06:43:43 +10:00
|
|
|
AnsiValue(u8),
|
2018-01-26 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2018-11-29 02:42:05 +11:00
|
|
|
/// Color types that can be used to determine if the Color enum is a Fore- or Background Color.
|
2018-01-26 04:26:08 +11:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum ColorType {
|
|
|
|
Background,
|
|
|
|
Foreground,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a str> for Color {
|
2018-11-29 02:42:05 +11:00
|
|
|
/// Get an color from an &str like `Color::from("blue")`.
|
2018-01-26 04:26:08 +11:00
|
|
|
fn from(src: &str) -> Self {
|
|
|
|
src.parse().unwrap_or(Color::White)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Color {
|
2018-11-29 02:42:05 +11:00
|
|
|
/// Get an color from an &str like `Color::from(String::from(blue))`.
|
2018-01-26 04:26:08 +11:00
|
|
|
fn from(src: String) -> Self {
|
|
|
|
src.parse().unwrap_or(Color::White)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Color {
|
|
|
|
type Err = ();
|
|
|
|
|
2018-08-14 05:04:07 +10:00
|
|
|
/// Convert a string to an Color value
|
2018-01-26 04:26:08 +11:00
|
|
|
fn from_str(src: &str) -> Result<Self, Self::Err> {
|
|
|
|
let src = src.to_lowercase();
|
|
|
|
|
|
|
|
match src.as_ref() {
|
|
|
|
"black" => Ok(Color::Black),
|
|
|
|
"red" => Ok(Color::Red),
|
|
|
|
"dark_red" => Ok(Color::DarkRed),
|
|
|
|
"green" => Ok(Color::Green),
|
|
|
|
"dark_green" => Ok(Color::DarkGreen),
|
|
|
|
"yellow" => Ok(Color::Yellow),
|
|
|
|
"dark_yellow" => Ok(Color::DarkYellow),
|
|
|
|
"blue" => Ok(Color::Blue),
|
|
|
|
"dark_blue" => Ok(Color::DarkBlue),
|
|
|
|
"magenta" => Ok(Color::Magenta),
|
|
|
|
"dark_magenta" => Ok(Color::DarkMagenta),
|
|
|
|
"cyan" => Ok(Color::Cyan),
|
|
|
|
"dark_cyan" => Ok(Color::DarkCyan),
|
|
|
|
"grey" => Ok(Color::Grey),
|
|
|
|
"white" => Ok(Color::White),
|
|
|
|
_ => Ok(Color::White),
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 06:43:43 +10:00
|
|
|
}
|