use ratatui::{
Frame,
layout::Alignment,
text::{Line, Span},
widgets::{Block, Clear, Padding, Paragraph},
};
use crate::app::Model;
use super::help::push_block;
use super::modal::centered;
use super::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;
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(
problem.summary.clone(),
theme.warn(),
)));
push_block(
&mut lines,
" ",
&problem.detail,
theme.text(),
inner,
Alignment::Left,
);
}
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(" details ", theme.accent()));
let height = (lines.len() as u16 + DETAILS_CHROME).min(full.height.saturating_sub(2));
let area = centered(full, width, height);
f.render_widget(Clear, area);
let inner_height = height.saturating_sub(DETAILS_CHROME) as usize;
let max_offset = lines.len().saturating_sub(inner_height);
let offset = model.ui.overlay_scroll.min(max_offset);
let visible: Vec<Line> = lines.into_iter().skip(offset).collect();
f.render_widget(Paragraph::new(visible).block(block), area);
offset
}