use embedded_io::{Read, Write};
use ratatui::{prelude::*, widgets::*};
use crate::lerobot::robot::Robot;
pub fn render_tui<PORT: Read + Write>(f: &mut Frame, robot: &Robot<PORT>, selected_index: usize) {
let area = f.area();
let header = Row::new(vec![
Cell::from("ID"),
Cell::from("Position"),
Cell::from("Goal Position"),
Cell::from("Speed"),
Cell::from("Temp (°C)"),
Cell::from("Load"),
Cell::from("Voltage"),
Cell::from("Current"),
Cell::from("Moving"),
Cell::from("Error"),
])
.style(Style::default().fg(Color::Yellow).bold());
let servo_state = robot.servo_state();
let rows: Vec<Row> = servo_state
.infos
.iter()
.enumerate()
.map(|(index, info)| {
let is_selected = index == selected_index;
let row = Row::new(vec![
Cell::from(info.id.to_string()),
Cell::from(info.position.to_string()),
Cell::from(info.goal_position.to_string()),
Cell::from(info.speed.to_string()),
Cell::from(info.temperature.to_string()),
Cell::from(info.load.to_string()),
Cell::from(info.voltage.to_string()),
Cell::from(info.current.to_string()),
Cell::from(if info.is_moving { "Yes" } else { "No" }).style(if info.is_moving {
Style::default().fg(Color::Green)
} else {
Style::default().fg(Color::Gray)
}),
Cell::from(if info.has_error { "Yes" } else { "No" }).style(if info.has_error {
Style::default().fg(Color::Red)
} else {
Style::default().fg(Color::Green)
}),
]);
if is_selected {
row.style(Style::default().bg(Color::DarkGray).fg(Color::White))
} else {
row
}
})
.collect();
let table = Table::new(
rows,
vec![
Constraint::Length(4), Constraint::Length(10), Constraint::Length(10), Constraint::Length(8), Constraint::Length(12), Constraint::Length(8), Constraint::Length(10), Constraint::Length(10), Constraint::Length(8), Constraint::Length(8), ],
)
.header(header)
.block(
Block::default()
.title("Servo Status Monitor (↑↓: Select, q: Quit)")
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan)),
)
.style(Style::default().fg(Color::White));
f.render_widget(table, area);
}