use ratatui::{
Frame,
layout::Alignment,
text::{Line, Span},
widgets::{Block, Padding},
};
use unicode_width::UnicodeWidthStr;
use crate::app::Model;
use super::modal::render_overlay;
use super::text::{push_block, wrap_text};
use super::{PROBLEM_MARK, theme::Theme};
const DETAILS_MAX_WIDTH: u16 = 78;
const DETAILS_PAD_X: u16 = 2;
const DETAILS_PAD_Y: u16 = 1;
const DETAILS_CHROME: u16 = 2 + DETAILS_PAD_Y * 2;
const DETAILS_INDENT: &str = " ";
pub fn draw(f: &mut Frame, model: &Model, theme: &Theme) -> usize {
let full = f.area();
let width = full.width.saturating_sub(4).min(DETAILS_MAX_WIDTH);
let inner = width.saturating_sub(2 + DETAILS_PAD_X * 2) as usize;
let mut lines: Vec<Line> = Vec::new();
for (index, problem) in model.problems().enumerate() {
if index > 0 {
lines.push(Line::default());
}
lines.push(Line::from(Span::styled(
format!("{PROBLEM_MARK} {}", problem.summary),
theme.warn().bold(),
)));
push_block(
&mut lines,
DETAILS_INDENT,
&problem.detail,
theme.text(),
inner,
Alignment::Left,
);
if let Some(hint) = &problem.hint {
push_hint(&mut lines, hint, theme, inner);
}
}
if lines.is_empty() {
lines.push(Line::from(Span::styled("nothing to report", theme.muted())));
}
let padding = Padding::new(DETAILS_PAD_X, DETAILS_PAD_X, DETAILS_PAD_Y, DETAILS_PAD_Y);
let block = Block::bordered()
.border_style(theme.accent())
.padding(padding)
.title(Span::styled(" problems ", theme.accent()));
render_overlay(
f,
full,
lines,
width,
DETAILS_CHROME,
block,
model.ui.overlay_scroll,
)
}
fn push_hint(lines: &mut Vec<Line<'static>>, hint: &str, theme: &Theme, inner: usize) {
const ARROW: &str = "\u{2192} ";
let indent = DETAILS_INDENT.width();
let body = inner.saturating_sub(indent + ARROW.width()).max(1);
for (index, chunk) in wrap_text(hint, body).into_iter().enumerate() {
let chunk = chunk.trim_end();
let mut spans = vec![Span::raw(DETAILS_INDENT.to_string())];
if index == 0 {
spans.push(Span::styled(ARROW, theme.accent()));
} else {
spans.push(Span::raw(" ".repeat(ARROW.width())));
}
spans.push(Span::styled(chunk.to_string(), theme.muted()));
lines.push(Line::from(spans));
}
}