use rnk_style_core::{BorderStyle, Color};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Align {
#[default]
Left,
Center,
Right,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
pub(crate) fg: Color,
pub(crate) bg: Color,
pub(crate) bold: bool,
pub(crate) italic: bool,
pub(crate) underline: bool,
pub(crate) strikethrough: bool,
pub(crate) dim: bool,
pub(crate) inverse: bool,
pub(crate) padding_top: u16,
pub(crate) padding_right: u16,
pub(crate) padding_bottom: u16,
pub(crate) padding_left: u16,
pub(crate) margin_top: u16,
pub(crate) margin_right: u16,
pub(crate) margin_bottom: u16,
pub(crate) margin_left: u16,
pub(crate) border_style: BorderStyle,
pub(crate) border_fg: Color,
pub(crate) width: Option<u16>,
pub(crate) height: Option<u16>,
pub(crate) align: Align,
}
impl Default for Style {
fn default() -> Self {
Self::new()
}
}
impl Style {
pub fn new() -> Self {
Self {
fg: Color::Reset,
bg: Color::Reset,
bold: false,
italic: false,
underline: false,
strikethrough: false,
dim: false,
inverse: false,
padding_top: 0,
padding_right: 0,
padding_bottom: 0,
padding_left: 0,
margin_top: 0,
margin_right: 0,
margin_bottom: 0,
margin_left: 0,
border_style: BorderStyle::None,
border_fg: Color::Reset,
width: None,
height: None,
align: Align::Left,
}
}
pub fn fg(mut self, color: Color) -> Self {
self.fg = color;
self
}
pub fn bg(mut self, color: Color) -> Self {
self.bg = color;
self
}
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
pub fn italic(mut self) -> Self {
self.italic = true;
self
}
pub fn underline(mut self) -> Self {
self.underline = true;
self
}
pub fn strikethrough(mut self) -> Self {
self.strikethrough = true;
self
}
pub fn dim(mut self) -> Self {
self.dim = true;
self
}
pub fn inverse(mut self) -> Self {
self.inverse = true;
self
}
pub fn padding(mut self, vertical: u16, horizontal: u16) -> Self {
self.padding_top = vertical;
self.padding_bottom = vertical;
self.padding_left = horizontal;
self.padding_right = horizontal;
self
}
pub fn p(mut self, value: u16) -> Self {
self.padding_top = value;
self.padding_right = value;
self.padding_bottom = value;
self.padding_left = value;
self
}
pub fn px(mut self, value: u16) -> Self {
self.padding_left = value;
self.padding_right = value;
self
}
pub fn py(mut self, value: u16) -> Self {
self.padding_top = value;
self.padding_bottom = value;
self
}
pub fn pt(mut self, value: u16) -> Self {
self.padding_top = value;
self
}
pub fn pr(mut self, value: u16) -> Self {
self.padding_right = value;
self
}
pub fn pb(mut self, value: u16) -> Self {
self.padding_bottom = value;
self
}
pub fn pl(mut self, value: u16) -> Self {
self.padding_left = value;
self
}
pub fn margin(mut self, vertical: u16, horizontal: u16) -> Self {
self.margin_top = vertical;
self.margin_bottom = vertical;
self.margin_left = horizontal;
self.margin_right = horizontal;
self
}
pub fn m(mut self, value: u16) -> Self {
self.margin_top = value;
self.margin_right = value;
self.margin_bottom = value;
self.margin_left = value;
self
}
pub fn mx(mut self, value: u16) -> Self {
self.margin_left = value;
self.margin_right = value;
self
}
pub fn my(mut self, value: u16) -> Self {
self.margin_top = value;
self.margin_bottom = value;
self
}
pub fn mt(mut self, value: u16) -> Self {
self.margin_top = value;
self
}
pub fn mr(mut self, value: u16) -> Self {
self.margin_right = value;
self
}
pub fn mb(mut self, value: u16) -> Self {
self.margin_bottom = value;
self
}
pub fn ml(mut self, value: u16) -> Self {
self.margin_left = value;
self
}
pub fn border(mut self, style: BorderStyle) -> Self {
self.border_style = style;
self
}
pub fn border_fg(mut self, color: Color) -> Self {
self.border_fg = color;
self
}
pub fn width(mut self, w: u16) -> Self {
self.width = Some(w);
self
}
pub fn height(mut self, h: u16) -> Self {
self.height = Some(h);
self
}
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
pub fn center(mut self) -> Self {
self.align = Align::Center;
self
}
pub fn right(mut self) -> Self {
self.align = Align::Right;
self
}
pub fn error() -> Self {
Self::new().fg(Color::Red).bold()
}
pub fn success() -> Self {
Self::new().fg(Color::Green)
}
pub fn warning() -> Self {
Self::new().fg(Color::Yellow)
}
pub fn info() -> Self {
Self::new().fg(Color::Cyan)
}
pub fn muted() -> Self {
Self::new().dim()
}
pub fn highlight() -> Self {
Self::new().inverse()
}
pub fn render(&self, text: &str) -> String {
crate::render::render(self, text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_style_builder() {
let style = Style::new()
.fg(Color::Red)
.bg(Color::Black)
.bold()
.padding(1, 2);
assert_eq!(style.fg, Color::Red);
assert_eq!(style.bg, Color::Black);
assert!(style.bold);
assert_eq!(style.padding_top, 1);
assert_eq!(style.padding_left, 2);
}
#[test]
fn test_preset_styles() {
let error = Style::error();
assert_eq!(error.fg, Color::Red);
assert!(error.bold);
let success = Style::success();
assert_eq!(success.fg, Color::Green);
}
#[test]
fn test_padding_shortcuts() {
let style = Style::new().p(2);
assert_eq!(style.padding_top, 2);
assert_eq!(style.padding_right, 2);
assert_eq!(style.padding_bottom, 2);
assert_eq!(style.padding_left, 2);
let style = Style::new().px(3).py(1);
assert_eq!(style.padding_left, 3);
assert_eq!(style.padding_right, 3);
assert_eq!(style.padding_top, 1);
assert_eq!(style.padding_bottom, 1);
}
#[test]
fn test_margin_shortcuts() {
let style = Style::new().m(2);
assert_eq!(style.margin_top, 2);
assert_eq!(style.margin_right, 2);
assert_eq!(style.margin_bottom, 2);
assert_eq!(style.margin_left, 2);
}
#[test]
fn test_alignment() {
let style = Style::new().center();
assert_eq!(style.align, Align::Center);
let style = Style::new().right();
assert_eq!(style.align, Align::Right);
}
}