tinterm 0.2.0

A powerful library for vibrant solid and gradient text with shimmer animations in terminal outputs.
Documentation
use crate::color::Color;

/// Trait for applying colors and text styling to strings.
///
/// This trait provides methods to colorize text (foreground and background) and apply
/// various text styles like bold, italic, underline, etc. All methods return a new
/// String with ANSI escape codes applied.
///
/// # Examples
///
/// ```
/// use tinterm::{TextModifier, Color};
///
/// // Basic coloring
/// let red_text = "Hello".color(Color::RED);
/// let blue_bg = "World".bg(Color::BLUE);
///
/// // Text styling
/// let bold_text = "Important".bold();
/// let italic_text = "Emphasis".italic();
///
/// // Method chaining
/// let styled = "Fancy Text".bold().color(Color::GREEN);
/// ```
pub trait TextModifier {
    fn color(&self, color: Color) -> String;
    fn background_color(&self, color: Color) -> String;
    fn fg(&self, color: Color) -> String;
    fn bg(&self, color: Color) -> String;
    fn bold(&self) -> String;
    fn dim(&self) -> String;
    fn italic(&self) -> String;
    fn underline(&self) -> String;
    fn blink(&self) -> String;
    fn reverse(&self) -> String;
    fn hidden(&self) -> String;
    fn strikethrough(&self) -> String;
    fn bright(&self) -> String;
}

impl TextModifier for str {
    fn color(&self, color: Color) -> String {
        self.fg(color)
    }

    fn background_color(&self, color: Color) -> String {
        self.bg(color)
    }

    fn fg(&self, color: Color) -> String {
        format!(
            "\x1b[38;2;{};{};{}m{}\x1b[39m",
            color.r, color.g, color.b, self
        )
    }

    fn bg(&self, color: Color) -> String {
        format!(
            "\x1b[48;2;{};{};{}m{}\x1b[49m",
            color.r, color.g, color.b, self
        )
    }

    fn bold(&self) -> String {
        format!("\x1b[1m{}\x1b[22m", self)
    }

    fn dim(&self) -> String {
        format!("\x1b[2m{}\x1b[22m", self)
    }

    fn italic(&self) -> String {
        format!("\x1b[3m{}\x1b[23m", self)
    }

    fn underline(&self) -> String {
        format!("\x1b[4m{}\x1b[24m", self)
    }

    fn blink(&self) -> String {
        format!("\x1b[5m{}\x1b[25m", self)
    }

    fn reverse(&self) -> String {
        format!("\x1b[7m{}\x1b[27m", self)
    }

    fn hidden(&self) -> String {
        format!("\x1b[8m{}\x1b[28m", self)
    }

    fn strikethrough(&self) -> String {
        format!("\x1b[9m{}\x1b[29m", self)
    }

    fn bright(&self) -> String {
        format!("\x1b[1m{}\x1b[0m", self)
    }
}