1use ratatui::{
2 style::Style,
3 widgets::{Block, BorderType, Borders},
4};
5use std::collections::HashMap as Map;
6use unicode_width::UnicodeWidthStr;
7
8use crate::id::PortId;
9
10#[derive(Debug, Clone)]
23pub struct NodeLayout<'a> {
24 pub size: (u16, u16),
25 border_type: BorderType,
26 title: &'a str,
27 border_style: Style,
28 port_names: Map<PortId, &'a str>,
32}
33
34impl<'a> NodeLayout<'a> {
35 pub fn new(size: (u16, u16)) -> Self {
36 Self {
37 size,
38 border_type: BorderType::Plain,
39 title: "",
40 border_style: Style::default(),
41 port_names: Map::new(),
42 }
43 }
44
45 pub fn from_content(content: &str) -> Self {
58 let inner_w = content.lines().map(UnicodeWidthStr::width).max().unwrap_or(0);
59 let inner_h = content.lines().count();
60 Self::new((inner_w.saturating_add(2) as u16, inner_h.saturating_add(2) as u16))
61 }
62
63 pub fn with_title(mut self, title: &'a str) -> Self {
64 self.title = title;
65 self
66 }
67
68 pub fn title(&self) -> &str {
69 self.title
70 }
71
72 pub fn with_border_type(mut self, border: BorderType) -> Self {
73 self.border_type = border;
74 self
75 }
76
77 pub fn border_type(&self) -> BorderType {
78 self.border_type
79 }
80
81 pub fn with_border_style(mut self, style: Style) -> Self {
82 self.border_style = style;
83 self
84 }
85
86 #[must_use]
105 pub fn with_port_name(mut self, port: PortId, name: &'a str) -> Self {
106 self.port_names.insert(port, name);
107 self
108 }
109
110 pub fn port_name(&self, port: PortId) -> Option<&str> {
114 self.port_names.get(&port).copied()
115 }
116
117 pub fn block(&self) -> Block<'_> {
118 Block::default()
119 .borders(Borders::ALL)
120 .border_type(self.border_type)
121 .border_style(self.border_style)
122 .title(self.title)
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn from_content_single_line() {
132 let node = NodeLayout::from_content("hello");
134 assert_eq!(node.size, (7, 3));
135 }
136
137 #[test]
138 fn from_content_multi_line_width_is_widest() {
139 let node = NodeLayout::from_content("short\nlonger_line\ncat");
141 assert_eq!(node.size, ((11 + 2), (3 + 2)));
142 }
143
144 #[test]
145 fn from_content_empty_string() {
146 let node = NodeLayout::from_content("");
149 assert_eq!(node.size, (2, 2));
150 }
151
152 #[test]
153 fn from_content_single_char() {
154 let node = NodeLayout::from_content("x");
155 assert_eq!(node.size, ((1 + 2), (1 + 2)));
156 }
157
158 #[test]
159 fn from_content_trailing_newline_does_not_add_line() {
160 let node = NodeLayout::from_content("a\n");
164 assert_eq!(node.size, ((1 + 2), (1 + 2)));
165 }
166
167 #[test]
168 fn from_content_cjk_uses_display_width() {
169 let node = NodeLayout::from_content("中文");
172 assert_eq!(node.size, ((4 + 2), (1 + 2)));
173 }
174
175 #[test]
176 fn from_content_cjk_mixed_with_ascii() {
177 let node = NodeLayout::from_content("中ab\nabc");
179 assert_eq!(node.size, ((4 + 2), (2 + 2)));
180 }
181
182 #[test]
183 fn from_content_zero_width_chars_dont_add_width() {
184 let node = NodeLayout::from_content("a\u{0308}");
187 assert_eq!(node.size, ((1 + 2), (1 + 2)));
188 }
189
190 #[test]
191 fn new_stores_size_directly() {
192 let node = NodeLayout::new((10, 4));
193 assert_eq!(node.size, (10, 4));
194 }
195
196 #[test]
197 fn port_name_defaults_to_none() {
198 let node = NodeLayout::new((6, 4));
200 assert_eq!(node.port_name(PortId(0)), None);
201 assert_eq!(node.port_name(PortId(7)), None);
202 }
203
204 #[test]
205 fn with_port_name_sets_and_reads_back() {
206 let node = NodeLayout::new((6, 4)).with_port_name(PortId(0), "data");
207 assert_eq!(node.port_name(PortId(0)), Some("data"));
208 assert_eq!(node.port_name(PortId(1)), None);
209 }
210
211 #[test]
212 fn with_port_name_overwrites_existing() {
213 let node = NodeLayout::new((6, 4))
215 .with_port_name(PortId(2), "old")
216 .with_port_name(PortId(2), "new");
217 assert_eq!(node.port_name(PortId(2)), Some("new"));
218 }
219
220 #[test]
221 fn node_layout_clones_with_port_names() {
222 let node = NodeLayout::new((6, 4))
224 .with_port_name(PortId(0), "in")
225 .with_port_name(PortId(1), "out");
226 let cloned = node.clone();
227 assert_eq!(cloned.port_name(PortId(0)), Some("in"));
228 assert_eq!(cloned.port_name(PortId(1)), Some("out"));
229 }
230}