1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//! # rust-colors
//! `rust-colors` is a library for parsing ANSI Strings to colors.
//!
//! ## Example
//! ```rust
//! use rust_colors::{Ansi, Color, Colors};
//!
//! fn my_function() {
//! let colors = Ansi;
//!
//! println!(
//! "The sky is {}, apples can be {}, do you like to touch {} grass?",
//! colors.color("blue", Colors::Blue),
//! colors.bold_color("red", Colors::Red),
//! colors.underline_color("green", Colors::Green),
//! );
//! }
//!
//! ```
//!
//! # Github
//! [Git repo RedIsGaming rust-colors](https://github.com/RedIsGaming/rust-colors)
pub use self::ansi::{Ansi, Color};
pub use self::colors::Colors;
mod colors;
mod ansi {
use std::fmt;
use crate::colors::Colors;
#[derive(Debug)]
pub struct Ansi;
pub trait Color<T: fmt::Debug> {
type Transform;
fn color(&self, txt: T, color: Colors) -> Self::Transform;
fn bold_color(&self, txt: T, bold_color: Colors) -> Self::Transform;
fn underline_color(&self, txt: T, underline_color: Colors) -> Self::Transform;
}
impl<T: fmt::Debug> Color<T> for Ansi {
type Transform = String;
fn color(&self, txt: T, color: Colors) -> Self::Transform {
format!(
"\x1b{}{:?}\x1b{}",
Colors::assign(&color),
txt,
Colors::assign(&Colors::Default)
)
}
fn bold_color(&self, txt: T, bold_color: Colors) -> Self::Transform {
format!(
"\x1b[1;{}{:?}\x1b{}",
Colors::assign(&bold_color).replace('[', ""),
txt,
Colors::assign(&Colors::Default)
)
}
fn underline_color(&self, txt: T, underline_color: Colors) -> Self::Transform {
format!(
"\x1b[4;{}{:?}\x1b{}",
Colors::assign(&underline_color).replace('[', ""),
txt,
Colors::assign(&Colors::Default)
)
}
}
}