use crate::grid::Grid;
use crate::text::display_width;
use crate::types::Subgraph;
use unicode_width::UnicodeWidthChar;
use super::charset::CharSet;
pub fn draw_subgraph(grid: &mut Grid, sg: &Subgraph, chars: &CharSet) {
if sg.width == 0 || sg.height == 0 {
return;
}
let x = sg.x;
let y = sg.y;
let width = sg.width;
let height = sg.height;
grid.set(x, y, chars.dtl);
grid.set(x + width - 1, y, chars.dtr);
grid.set(x, y + height - 1, chars.dbl);
grid.set(x + width - 1, y + height - 1, chars.dbr);
for i in 1..width - 1 {
grid.set(x + i, y, chars.dh);
grid.set(x + i, y + height - 1, chars.dh);
}
for i in 1..height - 1 {
grid.set(x, y + i, chars.dv);
grid.set(x + width - 1, y + i, chars.dv);
}
let label_w = display_width(&sg.label);
if !sg.label.is_empty() && width > label_w + 2 {
let label_x = x + (width - label_w) / 2;
let mut dx = 0;
for c in sg.label.chars() {
grid.set(label_x + dx, y, c);
dx += UnicodeWidthChar::width(c).unwrap_or(1);
}
}
}
pub fn protect_subgraph_borders(grid: &mut Grid, sg: &Subgraph) {
if sg.width == 0 || sg.height == 0 {
return;
}
let x = sg.x;
let y = sg.y;
let width = sg.width;
let height = sg.height;
grid.mark_protected(x, y);
grid.mark_protected(x + width - 1, y);
grid.mark_protected(x, y + height - 1);
grid.mark_protected(x + width - 1, y + height - 1);
for i in 1..width - 1 {
grid.mark_protected(x + i, y);
grid.mark_protected(x + i, y + height - 1);
}
for i in 1..height - 1 {
grid.mark_protected(x, y + i);
grid.mark_protected(x + width - 1, y + i);
}
}