use ratatui::{
style::Style,
widgets::{Block, BorderType, Borders},
};
use std::collections::HashMap as Map;
use unicode_width::UnicodeWidthStr;
use crate::id::PortId;
#[derive(Debug, Clone)]
pub struct NodeLayout<'a> {
pub size: (u16, u16),
border_type: BorderType,
title: &'a str,
border_style: Style,
port_names: Map<PortId, &'a str>,
}
impl<'a> NodeLayout<'a> {
pub fn new(size: (u16, u16)) -> Self {
Self {
size,
border_type: BorderType::Plain,
title: "",
border_style: Style::default(),
port_names: Map::new(),
}
}
pub fn from_content(content: &str) -> Self {
let inner_w = content.lines().map(UnicodeWidthStr::width).max().unwrap_or(0);
let inner_h = content.lines().count();
Self::new((inner_w.saturating_add(2) as u16, inner_h.saturating_add(2) as u16))
}
pub fn with_title(mut self, title: &'a str) -> Self {
self.title = title;
self
}
pub fn title(&self) -> &str {
self.title
}
pub fn with_border_type(mut self, border: BorderType) -> Self {
self.border_type = border;
self
}
pub fn border_type(&self) -> BorderType {
self.border_type
}
pub fn with_border_style(mut self, style: Style) -> Self {
self.border_style = style;
self
}
#[must_use]
pub fn with_port_name(mut self, port: PortId, name: &'a str) -> Self {
self.port_names.insert(port, name);
self
}
pub fn port_name(&self, port: PortId) -> Option<&str> {
self.port_names.get(&port).copied()
}
pub fn block(&self) -> Block<'_> {
Block::default()
.borders(Borders::ALL)
.border_type(self.border_type)
.border_style(self.border_style)
.title(self.title)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_content_single_line() {
let node = NodeLayout::from_content("hello");
assert_eq!(node.size, (7, 3));
}
#[test]
fn from_content_multi_line_width_is_widest() {
let node = NodeLayout::from_content("short\nlonger_line\ncat");
assert_eq!(node.size, ((11 + 2), (3 + 2)));
}
#[test]
fn from_content_empty_string() {
let node = NodeLayout::from_content("");
assert_eq!(node.size, (2, 2));
}
#[test]
fn from_content_single_char() {
let node = NodeLayout::from_content("x");
assert_eq!(node.size, ((1 + 2), (1 + 2)));
}
#[test]
fn from_content_trailing_newline_does_not_add_line() {
let node = NodeLayout::from_content("a\n");
assert_eq!(node.size, ((1 + 2), (1 + 2)));
}
#[test]
fn from_content_cjk_uses_display_width() {
let node = NodeLayout::from_content("中文");
assert_eq!(node.size, ((4 + 2), (1 + 2)));
}
#[test]
fn from_content_cjk_mixed_with_ascii() {
let node = NodeLayout::from_content("中ab\nabc");
assert_eq!(node.size, ((4 + 2), (2 + 2)));
}
#[test]
fn from_content_zero_width_chars_dont_add_width() {
let node = NodeLayout::from_content("a\u{0308}");
assert_eq!(node.size, ((1 + 2), (1 + 2)));
}
#[test]
fn new_stores_size_directly() {
let node = NodeLayout::new((10, 4));
assert_eq!(node.size, (10, 4));
}
#[test]
fn port_name_defaults_to_none() {
let node = NodeLayout::new((6, 4));
assert_eq!(node.port_name(PortId(0)), None);
assert_eq!(node.port_name(PortId(7)), None);
}
#[test]
fn with_port_name_sets_and_reads_back() {
let node = NodeLayout::new((6, 4)).with_port_name(PortId(0), "data");
assert_eq!(node.port_name(PortId(0)), Some("data"));
assert_eq!(node.port_name(PortId(1)), None);
}
#[test]
fn with_port_name_overwrites_existing() {
let node = NodeLayout::new((6, 4))
.with_port_name(PortId(2), "old")
.with_port_name(PortId(2), "new");
assert_eq!(node.port_name(PortId(2)), Some("new"));
}
#[test]
fn node_layout_clones_with_port_names() {
let node = NodeLayout::new((6, 4))
.with_port_name(PortId(0), "in")
.with_port_name(PortId(1), "out");
let cloned = node.clone();
assert_eq!(cloned.port_name(PortId(0)), Some("in"));
assert_eq!(cloned.port_name(PortId(1)), Some("out"));
}
}