use fnv::FnvHashMap; use karsa::printer::Size; use karsa::renderloop::run; use karsa::widgets::table::{self, Column, Table}; use karsa::widgets::widgetbox::WidgetBox; use std::io; #[derive(Hash, Eq, PartialEq)] enum ColumnKind { Col1, Col2, Col3, } pub struct Data { data: Vec<(String, String, String)>, } impl Data { pub fn new() -> Self { let mut data = Vec::new(); for i in 0..7 { data.push(( format!("line{i}c1"), format!("line{i}c2"), format!("line{i}c3"), )); } Self { data } } } impl table::Data for Data { fn element_count(&self) -> usize { self.data.len() } fn get_row_content(&self, index: usize) -> Option> { self.data.iter().nth(index).map(|elem| { let mut row = FnvHashMap::default(); row.insert(ColumnKind::Col1, elem.0.clone()); row.insert(ColumnKind::Col2, elem.1.clone()); row.insert(ColumnKind::Col3, elem.2.clone()); row }) } fn on_click(&self) -> io::Result<()> { Ok(()) } fn delete(&mut self, index: usize) {} } fn main() -> io::Result<()> { let data = Data::new(); let table = Table::new( data, vec![ Column { name: String::from("col1"), width: Size::Ratio(25), kind: ColumnKind::Col1, }, Column { name: String::from("col2"), width: Size::Ratio(50), kind: ColumnKind::Col2, }, Column { name: String::from("col3"), width: Size::Ratio(25), kind: ColumnKind::Col3, }, ], ); let widgetbox = WidgetBox::new(table, Some(70), Some(7), Some("Title".to_string())); run(widgetbox, None)?; Ok(()) }