use crate::mini_salsa::theme::THEME; use crate::mini_salsa::{run_ui, setup_logging, MiniSalsaState}; use format_num_pattern::NumberFormat; use rat_ftable::event::Outcome; use rat_ftable::selection::{rowselection, RowSelection}; use rat_ftable::textdata::{Cell, Row}; use rat_ftable::{Table, TableState}; use rat_scrolled::Scroll; use ratatui::layout::{Constraint, Flex, Layout, Rect}; use ratatui::text::Text; use ratatui::widgets::{block, Block, StatefulWidget}; use ratatui::Frame; mod data; mod mini_salsa; fn main() -> Result<(), anyhow::Error> { setup_logging()?; let mut data = Data { table_data: data::DATA .iter() .map(|v| Sample { text: *v, num1: rand::random(), num2: rand::random(), check: rand::random(), }) .collect(), }; let mut state = State { table: Default::default(), }; run_ui("text", handle_table, repaint_table, &mut data, &mut state) } struct Sample { pub(crate) text: &'static str, pub(crate) num1: f32, pub(crate) num2: f32, pub(crate) check: bool, } struct Data { pub(crate) table_data: Vec, } struct State { pub(crate) table: TableState, } fn repaint_table( frame: &mut Frame<'_>, area: Rect, data: &mut Data, _istate: &mut MiniSalsaState, state: &mut State, ) -> Result<(), anyhow::Error> { let l0 = Layout::horizontal([Constraint::Percentage(61)]) .flex(Flex::Center) .split(area); let row_fmt = NumberFormat::new("000000").expect("fmt"); let num1_fmt = NumberFormat::new("####0.00").expect("fmt"); let num2_fmt = NumberFormat::new("####0.00").expect("fmt"); Table::default() .rows(data.table_data.iter().take(3000).enumerate().map(|(i, v)| { Row::new([ Text::from(row_fmt.fmt_u(i)), v.text.into(), num1_fmt.fmt_u(v.num1).into(), num2_fmt.fmt_u(v.num2).into(), v.check.to_string().into(), ]) })) .widths([ Constraint::Length(6), Constraint::Length(20), Constraint::Length(15), Constraint::Length(15), Constraint::Length(3), ]) .column_spacing(1) .header( Row::new([ Cell::from("Nr"), Cell::from("Text"), Cell::from("Val1"), Cell::from("Val2"), Cell::from("State"), ]) .style(Some(THEME.table_header())), ) .footer(Row::new(["a", "b", "c", "d", "e"]).style(Some(THEME.table_footer()))) .block( Block::bordered() .border_type(block::BorderType::Rounded) .border_style(THEME.block()) .title("text-tabledata"), ) .vscroll(Scroll::new().style(THEME.block())) .flex(Flex::End) .style(THEME.table()) .select_row_style(Some(THEME.gray(3))) .render(l0[0], frame.buffer_mut(), &mut state.table); Ok(()) } fn handle_table( event: &crossterm::event::Event, _data: &mut Data, _istate: &mut MiniSalsaState, state: &mut State, ) -> Result { let r = rowselection::handle_events(&mut state.table, true, event); Ok(r) }