mod border;
mod emphasis;
mod frame;
mod layout;
mod render;
pub use border::Border;
pub use layout::{
Position, block_width, cut, height, join_horizontal, join_vertical, truncate_with, wrap,
};
#[cfg(feature = "bubbles")]
pub(crate) use layout::{round_clamped, to_f64};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
Ansi(u8),
Palette(u8),
Rgb(u8, u8, u8),
}
impl Color {
pub const BLACK: Self = Self::Ansi(0);
pub const RED: Self = Self::Ansi(1);
pub const GREEN: Self = Self::Ansi(2);
pub const YELLOW: Self = Self::Ansi(3);
pub const BLUE: Self = Self::Ansi(4);
pub const MAGENTA: Self = Self::Ansi(5);
pub const CYAN: Self = Self::Ansi(6);
pub const WHITE: Self = Self::Ansi(7);
pub const GREY: Self = Self::Ansi(8);
fn sgr(self, background: bool) -> String {
match self {
Self::Ansi(n) => {
let base = match (background, n < 8) {
(false, true) => 30,
(false, false) => 90 - 8,
(true, true) => 40,
(true, false) => 100 - 8,
};
(base + u16::from(n.min(15))).to_string()
}
Self::Palette(n) => format!("{};5;{n}", if background { 48 } else { 38 }),
Self::Rgb(r, g, b) => format!("{};2;{r};{g};{b}", if background { 48 } else { 38 }),
}
}
}
const BOLD: u8 = 1;
const DIM: u8 = 1 << 1;
const ITALIC: u8 = 1 << 2;
const UNDERLINE: u8 = 1 << 3;
const REVERSE: u8 = 1 << 4;
const ATTRIBUTES: [(u8, &str); 5] = [
(BOLD, "1"),
(DIM, "2"),
(ITALIC, "3"),
(UNDERLINE, "4"),
(REVERSE, "7"),
];
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Style {
fg: Option<Color>,
bg: Option<Color>,
attributes: u8,
chosen: u8,
frame: Frame,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Frame {
padding: [u16; 4],
margin: [u16; 4],
border: Option<Border>,
sides: u8,
border_fg: Option<Color>,
width: u16,
height: u16,
}
const SIDES_SET: u8 = 1 << 4;
impl Style {
#[must_use]
pub const fn new() -> Self {
Self {
fg: None,
bg: None,
attributes: 0,
chosen: 0,
frame: Frame {
padding: [0; 4],
margin: [0; 4],
border: None,
sides: 0,
border_fg: None,
width: 0,
height: 0,
},
}
}
#[must_use]
pub fn paint(&self, text: &str) -> String {
let codes = self.codes();
if codes.is_empty() {
text.to_string()
} else {
format!("\x1b[{}m{text}\x1b[0m", codes.join(";"))
}
}
fn codes(&self) -> Vec<String> {
let mut codes: Vec<String> = ATTRIBUTES
.iter()
.filter(|(bit, _)| self.attributes & bit != 0)
.map(|(_, c)| (*c).to_string())
.collect();
codes.extend(self.fg.map(|c| c.sgr(false)));
codes.extend(self.bg.map(|c| c.sgr(true)));
codes
}
}
pub use crate::unicode::WidthMethod;
use crate::ansi::{Token, spans};
use std::sync::atomic::{AtomicBool, Ordering};
static GRAPHEME_WIDTH: AtomicBool = AtomicBool::new(false);
#[must_use]
pub fn width_method() -> WidthMethod {
if GRAPHEME_WIDTH.load(Ordering::Relaxed) {
WidthMethod::GraphemeWidth
} else {
WidthMethod::WcWidth
}
}
pub(crate) fn set_width_method(method: WidthMethod) {
GRAPHEME_WIDTH.store(method == WidthMethod::GraphemeWidth, Ordering::Relaxed);
}
#[must_use]
pub fn width(text: &str) -> usize {
width_method().string_width(text)
}
#[must_use]
pub fn strip_ansi(text: &str) -> String {
spans(text, WidthMethod::WcWidth)
.filter(|(token, _)| matches!(token, Token::Text(..) | Token::Control(_)))
.map(|(_, raw)| raw)
.collect()
}
#[must_use]
pub fn truncate(text: &str, columns: usize) -> String {
let method = width_method();
if method.string_width(text) <= columns {
return text.to_string();
}
let mut out = String::with_capacity(text.len());
let mut room = Some(columns);
for (token, raw) in spans(text, method) {
if keep(token, &mut room) {
out.push_str(raw);
}
}
out + "\x1b[0m"
}
fn keep(token: Token<'_>, room: &mut Option<usize>) -> bool {
match (token, *room) {
(Token::Text(_, w), Some(left)) if w <= left => {
*room = Some(left - w);
true
}
(Token::Text(..), _) => {
*room = None;
false
}
(Token::Control(_), None) => false,
_ => true,
}
}
#[cfg(test)]
mod tests;