//! Convenience constructors for consistently styled terminal text. //! //! These helpers create common painted text fragments such as dim, bold, //! emphasized, or badge-like labels. They keep small style choices consistent //! across examples and widgets without adding a larger theme system. use std::fmt; use super::{Color, Paint, Style, colors}; /// Default foreground color for faint neutral text. pub const FAINT_COLOR: Color = colors::fixed::FAINT; /// Returns unstyled text. pub fn default(msg: D) -> Paint { Paint::new(msg) } /// Returns wrapping text that preserves outer styling across resets. pub fn wrap(msg: D) -> Paint { Paint::wrapping(msg) } /// Returns text styled as negative or failing. pub fn negative(msg: D) -> Paint { Paint::red(msg) } /// Returns text styled as positive or successful. pub fn positive(msg: D) -> Paint { Paint::green(msg) } /// Returns faint neutral text. pub fn faint(msg: D) -> Paint { Paint::new(msg).fg(FAINT_COLOR) } /// Returns emphasized highlighted text. pub fn highlight(input: D) -> Paint { Paint::green(input).bold() } /// Returns bold text. pub fn bold(input: D) -> Paint { Paint::new(input).bold() } /// Returns dim text. pub fn dim(input: D) -> Paint { Paint::new(input).dim() } /// Returns dim italic text. pub fn italic(input: D) -> Paint { Paint::new(input).italic().dim() } /// Returns text styled as an inverted badge. pub fn badge(text: impl fmt::Display, color: Color) -> Paint { Paint::new(format!(" {text} ")).with_style(Style::new(color).invert()) } #[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { use super::*; use crate::Color; fn prefix(paint: Paint) -> String { let mut out = String::new(); paint.style().fmt_prefix(&mut out).unwrap(); out } #[test] fn text_constructors_apply_expected_foreground_colors() { assert_eq!(default("x").style().fg_color(), Color::Unset); assert_eq!(wrap("x").style().fg_color(), Color::Unset); assert!(wrap("x").style().is_wrapping()); assert_eq!(negative("x").style().fg_color(), Color::Red); assert_eq!(positive("x").style().fg_color(), Color::Green); assert_eq!(faint("x").style().fg_color(), Color::Fixed(236)); } #[test] fn emphasis_constructors_set_style_properties() { assert_eq!(prefix(highlight("x")), "\x1B[1;32m"); assert_eq!(prefix(bold("x")), "\x1B[1m"); assert_eq!(prefix(dim("x")), "\x1B[2m"); assert_eq!(prefix(italic("x")), "\x1B[2;3m"); } #[test] fn badge_returns_inverted_paint() { let badge = badge("new", Color::Magenta); assert_eq!(badge.content(), " new "); assert_eq!(badge.style().fg_color(), Color::Magenta); assert_eq!(badge.styled().to_string(), "\x1B[7;35m new \x1B[0m"); } }