use crate::style::{Align, Style};
use rnk_style_core::Color;
use unicode_width::UnicodeWidthStr;
const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
const DIM: &str = "\x1b[2m";
const ITALIC: &str = "\x1b[3m";
const UNDERLINE: &str = "\x1b[4m";
const INVERSE: &str = "\x1b[7m";
const STRIKETHROUGH: &str = "\x1b[9m";
pub fn render(style: &Style, text: &str) -> String {
let lines: Vec<&str> = text.lines().collect();
let content_width = if let Some(w) = style.width {
w as usize
} else {
lines.iter().map(|l| l.width()).max().unwrap_or(0)
};
let border_width = if style.border_style.is_visible() {
2
} else {
0
};
let inner_width = content_width + style.padding_left as usize + style.padding_right as usize;
let _total_width = inner_width + border_width;
let style_codes = build_style_codes(style);
let border_codes = build_border_codes(style);
let mut result = String::new();
for _ in 0..style.margin_top {
result.push('\n');
}
let (tl, tr, bl, br, h, v) = style.border_style.chars();
if style.border_style.is_visible() {
add_margin_left(&mut result, style.margin_left);
result.push_str(&border_codes);
result.push_str(tl);
for _ in 0..inner_width {
result.push_str(h);
}
result.push_str(tr);
result.push_str(RESET);
result.push('\n');
}
for _ in 0..style.padding_top {
render_padding_line(
&mut result,
style,
&border_codes,
v,
inner_width,
&style_codes,
);
}
let line_count = lines.len();
let target_height = style.height.map(|h| h as usize).unwrap_or(line_count);
for (i, line) in lines.iter().enumerate() {
if i >= target_height {
break;
}
render_content_line(
&mut result,
style,
&border_codes,
v,
&style_codes,
line,
content_width,
);
}
for _ in line_count..target_height {
render_padding_line(
&mut result,
style,
&border_codes,
v,
inner_width,
&style_codes,
);
}
for _ in 0..style.padding_bottom {
render_padding_line(
&mut result,
style,
&border_codes,
v,
inner_width,
&style_codes,
);
}
if style.border_style.is_visible() {
add_margin_left(&mut result, style.margin_left);
result.push_str(&border_codes);
result.push_str(bl);
for _ in 0..inner_width {
result.push_str(h);
}
result.push_str(br);
result.push_str(RESET);
result.push('\n');
}
for _ in 0..style.margin_bottom {
result.push('\n');
}
if result.ends_with('\n') {
result.pop();
}
result
}
fn build_style_codes(style: &Style) -> String {
let mut codes = String::new();
if style.fg != Color::Reset {
codes.push_str(&style.fg.to_ansi_fg());
}
if style.bg != Color::Reset {
codes.push_str(&style.bg.to_ansi_bg());
}
if style.bold {
codes.push_str(BOLD);
}
if style.dim {
codes.push_str(DIM);
}
if style.italic {
codes.push_str(ITALIC);
}
if style.underline {
codes.push_str(UNDERLINE);
}
if style.inverse {
codes.push_str(INVERSE);
}
if style.strikethrough {
codes.push_str(STRIKETHROUGH);
}
codes
}
fn build_border_codes(style: &Style) -> String {
if style.border_fg != Color::Reset {
style.border_fg.to_ansi_fg()
} else {
String::new()
}
}
fn add_margin_left(result: &mut String, margin: u16) {
for _ in 0..margin {
result.push(' ');
}
}
fn render_padding_line(
result: &mut String,
style: &Style,
border_codes: &str,
v: &str,
inner_width: usize,
style_codes: &str,
) {
add_margin_left(result, style.margin_left);
if style.border_style.is_visible() {
result.push_str(border_codes);
result.push_str(v);
result.push_str(RESET);
}
result.push_str(style_codes);
for _ in 0..inner_width {
result.push(' ');
}
result.push_str(RESET);
if style.border_style.is_visible() {
result.push_str(border_codes);
result.push_str(v);
result.push_str(RESET);
}
result.push('\n');
}
fn render_content_line(
result: &mut String,
style: &Style,
border_codes: &str,
v: &str,
style_codes: &str,
text: &str,
content_width: usize,
) {
add_margin_left(result, style.margin_left);
if style.border_style.is_visible() {
result.push_str(border_codes);
result.push_str(v);
result.push_str(RESET);
}
result.push_str(style_codes);
for _ in 0..style.padding_left {
result.push(' ');
}
let text_width = text.width();
let padding_needed = content_width.saturating_sub(text_width);
match style.align {
Align::Left => {
result.push_str(text);
for _ in 0..padding_needed {
result.push(' ');
}
}
Align::Center => {
let left_pad = padding_needed / 2;
let right_pad = padding_needed - left_pad;
for _ in 0..left_pad {
result.push(' ');
}
result.push_str(text);
for _ in 0..right_pad {
result.push(' ');
}
}
Align::Right => {
for _ in 0..padding_needed {
result.push(' ');
}
result.push_str(text);
}
}
for _ in 0..style.padding_right {
result.push(' ');
}
result.push_str(RESET);
if style.border_style.is_visible() {
result.push_str(border_codes);
result.push_str(v);
result.push_str(RESET);
}
result.push('\n');
}
#[cfg(test)]
mod tests {
use super::*;
use rnk_style_core::BorderStyle;
#[test]
fn test_simple_render() {
let style = Style::new();
let output = style.render("Hello");
assert!(output.contains("Hello"));
}
#[test]
fn test_colored_render() {
let style = Style::new().fg(Color::Red);
let output = style.render("Hello");
assert!(output.contains("\x1b[31m"));
assert!(output.contains("Hello"));
assert!(output.contains(RESET));
}
#[test]
fn test_bold_render() {
let style = Style::new().bold();
let output = style.render("Hello");
assert!(output.contains(BOLD));
}
#[test]
fn test_padding_render() {
let style = Style::new().padding(1, 2);
let output = style.render("Hi");
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 3); }
#[test]
fn test_border_render() {
let style = Style::new().border(BorderStyle::Round);
let output = style.render("Hi");
assert!(output.contains("╭"));
assert!(output.contains("╯"));
assert!(output.contains("│"));
}
#[test]
fn test_multiline_render() {
let style = Style::new();
let output = style.render("Line 1\nLine 2");
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 2);
}
#[test]
fn test_alignment_center() {
let style = Style::new().width(10).center();
let output = style.render("Hi");
assert!(output.contains(" Hi "));
}
#[test]
fn test_alignment_right() {
let style = Style::new().width(10).right();
let output = style.render("Hi");
assert!(output.contains(" Hi"));
}
#[test]
fn test_full_style() {
let style = Style::new()
.fg(Color::Cyan)
.bg(Color::Black)
.bold()
.padding(1, 2)
.border(BorderStyle::Round)
.border_fg(Color::Magenta);
let output = style.render("Test");
assert!(output.contains("╭"));
assert!(output.contains("Test"));
}
}