use super::app_state::AppState;
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout},
style::{Color, Style},
widgets::{Block, Borders, List, ListItem, Paragraph},
};
pub fn draw_ui(f: &mut Frame, app_state: &mut AppState) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(1), Constraint::Length(3), ])
.split(f.area());
let output_items: Vec<ListItem> = app_state
.output_lines
.iter()
.map(|line| ListItem::new(line.as_str()))
.collect();
let title = if app_state.auto_scroll {
"Serial Monitor (Auto-scroll ON - ↑↓/PgUp/PgDn to scroll, Ctrl+A to re-enable auto-scroll)"
} else {
"Serial Monitor (Auto-scroll OFF - ↑↓/PgUp/PgDn to scroll, Ctrl+A to re-enable auto-scroll)"
};
let output_list = List::new(output_items)
.block(Block::default().borders(Borders::ALL).title(title))
.style(Style::default().fg(Color::White))
.highlight_style(Style::default().fg(Color::Black).bg(Color::White));
if app_state.auto_scroll {
f.render_stateful_widget(output_list, chunks[0], &mut app_state.auto_scroll_state);
} else {
f.render_stateful_widget(output_list, chunks[0], &mut app_state.list_state);
}
let input_paragraph = Paragraph::new(app_state.input_line.as_str())
.block(
Block::default()
.borders(Borders::ALL)
.title("Input (Press Enter to send, Ctrl+C or Esc to exit)"),
)
.style(Style::default().fg(Color::Yellow));
f.render_widget(input_paragraph, chunks[1]);
f.set_cursor_position((
chunks[1].x + app_state.input_line.len() as u16 + 1,
chunks[1].y + 1,
));
}