ui.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use crate::app::App;
  2. use tui::{
  3. backend::Backend,
  4. layout::{Constraint, Direction, Layout, Rect},
  5. style::{Color, Modifier, Style},
  6. text::Spans,
  7. widgets::{Block, Borders, List, ListItem, Paragraph},
  8. Frame,
  9. };
  10. pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
  11. let slice = Layout::default()
  12. .direction(Direction::Horizontal)
  13. .margin(2)
  14. .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
  15. .split(f.size());
  16. let nodes: Vec<ListItem> = app
  17. .node_list
  18. .node_id
  19. .iter()
  20. .map(|id| {
  21. let line1 = Spans::from(id.to_string());
  22. ListItem::new(vec![line1]).style(Style::default())
  23. })
  24. .collect();
  25. let nodes = List::new(nodes)
  26. .block(Block::default().borders(Borders::ALL))
  27. .highlight_style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD));
  28. f.render_stateful_widget(nodes, slice[0], &mut app.node_list.state);
  29. let index = app.node_info.index;
  30. render_info(app, f, index, slice);
  31. }
  32. fn render_info<B: Backend>(app: &mut App, f: &mut Frame<B>, index: usize, slice: Vec<Rect>) {
  33. let id = &app.node_info.infos[index].id;
  34. let connections = app.node_info.infos[index].connections;
  35. let is_active = app.node_info.infos[index].is_active;
  36. let message = &app.node_info.infos[index].last_message;
  37. let span = vec![
  38. Spans::from(format!("NodeId: {}", id)),
  39. Spans::from(format!("Number of connections: {}", connections)),
  40. Spans::from(format!("Is active: {}", is_active)),
  41. Spans::from(format!("Last message: {}", message)),
  42. ];
  43. let graph =
  44. Paragraph::new(span).block(Block::default().borders(Borders::ALL)).style(Style::default());
  45. f.render_widget(graph, slice[1]);
  46. }