mod border;
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,
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,
frame: Frame {
padding: [0; 4],
margin: [0; 4],
border: None,
sides: 0,
border_fg: None,
width: 0,
height: 0,
},
}
}
#[must_use]
pub const fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
#[must_use]
pub const fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
#[must_use]
pub const fn bold(self) -> Self {
self.with(BOLD)
}
#[must_use]
pub const fn dim(self) -> Self {
self.with(DIM)
}
#[must_use]
pub const fn italic(self) -> Self {
self.with(ITALIC)
}
#[must_use]
pub const fn underline(self) -> Self {
self.with(UNDERLINE)
}
#[must_use]
pub const fn reverse(self) -> Self {
self.with(REVERSE)
}
const fn with(mut self, bit: u8) -> Self {
self.attributes |= bit;
self
}
#[must_use]
pub const fn inherit(mut self, other: Self) -> Self {
if self.fg.is_none() {
self.fg = other.fg;
}
if self.bg.is_none() {
self.bg = other.bg;
}
self.attributes |= other.attributes;
self
}
#[must_use]
pub const fn plain(mut self) -> Self {
self.frame = Self::new().frame;
self
}
#[must_use]
pub const fn padding(mut self, vertical: u16, horizontal: u16) -> Self {
self.frame.padding = [vertical, horizontal, vertical, horizontal];
self
}
#[must_use]
pub const fn padding_sides(mut self, top: u16, right: u16, bottom: u16, left: u16) -> Self {
self.frame.padding = [top, right, bottom, left];
self
}
#[must_use]
pub const fn padding_left(mut self, columns: u16) -> Self {
self.frame.padding[3] = columns;
self
}
#[must_use]
pub const fn margin(mut self, vertical: u16, horizontal: u16) -> Self {
self.frame.margin = [vertical, horizontal, vertical, horizontal];
self
}
#[must_use]
pub const fn margin_sides(mut self, top: u16, right: u16, bottom: u16, left: u16) -> Self {
self.frame.margin = [top, right, bottom, left];
self
}
#[must_use]
pub const fn margin_top(mut self, rows: u16) -> Self {
self.frame.margin[0] = rows;
self
}
#[must_use]
pub fn get_margin_top(&self) -> usize {
usize::from(self.frame.margin[0])
}
#[must_use]
pub const fn border(mut self, border: Border) -> Self {
self.frame.border = Some(border);
self
}
#[must_use]
pub fn border_sides(mut self, [top, right, bottom, left]: [bool; 4]) -> Self {
let bit = |on: bool, n: u8| u8::from(on) << n;
self.frame.sides = SIDES_SET | bit(top, 0) | bit(right, 1) | bit(bottom, 2) | bit(left, 3);
self
}
#[must_use]
pub const fn border_fg(mut self, color: Color) -> Self {
self.frame.border_fg = Some(color);
self
}
#[must_use]
pub const fn width(mut self, columns: u16) -> Self {
self.frame.width = columns;
self
}
#[must_use]
pub const fn height(mut self, rows: u16) -> Self {
self.frame.height = rows;
self
}
#[must_use]
pub fn get_padding_left(&self) -> usize {
usize::from(self.frame.padding[3])
}
#[must_use]
pub fn get_padding_right(&self) -> usize {
usize::from(self.frame.padding[1])
}
#[must_use]
pub fn horizontal_frame(&self) -> usize {
let [_, right, _, left] = self.frame.padding;
let [_, m_right, _, m_left] = self.frame.margin;
let [_, b_right, _, b_left] = self.border_widths();
usize::from(right + left + m_right + m_left) + b_right + b_left
}
#[must_use]
pub fn frame_offset(&self) -> (usize, usize) {
let [top, _, _, left] = self.frame.padding;
let [m_top, _, _, m_left] = self.frame.margin;
let [b_top, _, _, b_left] = self.border_widths();
(
usize::from(left + m_left) + b_left,
usize::from(top + m_top) + b_top,
)
}
#[must_use]
pub fn vertical_frame(&self) -> usize {
let [top, _, bottom, _] = self.frame.padding;
let [m_top, _, m_bottom, _] = self.frame.margin;
let [b_top, _, b_bottom, _] = self.border_widths();
usize::from(top + bottom + m_top + m_bottom) + b_top + b_bottom
}
#[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;