ui.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::app::App;
  2. use tui::{
  3. backend::Backend,
  4. layout::{Constraint, Direction, Layout},
  5. style::{Color, Modifier, Style},
  6. text::{Span, Spans},
  7. widgets::{Block, Borders, List, ListItem},
  8. Frame,
  9. };
  10. pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
  11. let chunks = Layout::default()
  12. .direction(Direction::Vertical)
  13. .constraints([Constraint::Percentage(6), Constraint::Percentage(94)].as_ref())
  14. .split(f.size());
  15. // Iterate through all elements in the `items` app and append some debug text to it.
  16. let items: Vec<ListItem> = app
  17. .items
  18. .items
  19. .iter()
  20. .map(|i| {
  21. let mut lines = vec![Spans::from(i.0)];
  22. for _ in 0..i.1 {
  23. lines.push(Spans::from(Span::styled(
  24. "Just some random garbage",
  25. Style::default().add_modifier(Modifier::ITALIC),
  26. )));
  27. }
  28. ListItem::new(lines).style(Style::default())
  29. })
  30. .collect();
  31. // Create a List from all list items and highlight the currently selected one
  32. let items = List::new(items)
  33. .block(Block::default().borders(Borders::ALL).title("List"))
  34. .highlight_style(Style::default().bg(Color::LightGreen).add_modifier(Modifier::BOLD))
  35. .highlight_symbol(">> ");
  36. // Render the item list
  37. f.render_stateful_widget(items, chunks[0], &mut app.items.state);
  38. }