use ansi_to_tui::IntoText;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Paragraph, Widget};
use ratatui::Frame;
use crate::app::AppState;
use crate::ui::Theme;
pub fn render(frame: &mut Frame<'_>, area: Rect, state: &AppState, theme: &Theme) {
reset_area(frame.buffer_mut(), area);
let text: Text<'_> = if state.sessions.is_empty() {
placeholder("", theme)
} else {
match state.selected_preview() {
Some(bytes) if !bytes.is_empty() => bytes
.into_text()
.unwrap_or_else(|_| placeholder("preview: (ansi parse failed)", theme)),
_ => placeholder("preview: capturing…", theme),
}
};
let text_lines = text.lines.len() as u16;
let scroll_y = text_lines.saturating_sub(area.height);
Paragraph::new(text)
.scroll((scroll_y, 0))
.render(area, frame.buffer_mut());
}
fn reset_area(buf: &mut Buffer, area: Rect) {
let reset_style = Style::default().fg(Color::Reset).bg(Color::Reset);
for y in area.top()..area.bottom() {
for x in area.left()..area.right() {
let cell = &mut buf[(x, y)];
cell.set_char(' ');
cell.set_style(reset_style);
}
}
}
fn placeholder(msg: &str, theme: &Theme) -> Text<'static> {
Text::from(vec![
Line::from(""),
Line::from(Span::styled(
format!(" {}", msg),
Style::default().fg(theme.text_muted),
)),
])
}