ui.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use crate::view::View;
  2. use log::debug;
  3. use tui::{
  4. backend::Backend,
  5. layout::{Constraint, Direction, Layout, Rect},
  6. style::Style,
  7. text::{Span, Spans},
  8. widgets::{Block, Borders, List, ListItem, Paragraph},
  9. Frame,
  10. };
  11. pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
  12. let list_margin = 2;
  13. let list_direction = Direction::Horizontal;
  14. let list_cnstrnts = vec![Constraint::Percentage(50), Constraint::Percentage(50)];
  15. let mut nodes = Vec::new();
  16. let style = Style::default();
  17. // lines.push(sublist)
  18. for id in &view.id_list.node_id {
  19. let id_span = Span::raw(id.to_string());
  20. let mut lines = vec![Spans::from(id_span)];
  21. // create a new vector of addresses
  22. // render as a sub node
  23. match &view.info_list.infos.get(id) {
  24. Some(node) => {
  25. if !node.outbound.iter().all(|node| node.is_empty) {
  26. lines.push(Spans::from(Span::styled(" Outgoing", Style::default())));
  27. }
  28. for outbound in &node.outbound.clone() {
  29. for slot in outbound.slots.clone() {
  30. let addr = Span::styled(format!(" {}", slot.addr), style);
  31. let msg: Span = match slot.channel.last_status.as_str() {
  32. "recv" => Span::styled(
  33. format!(" [R: {}]", slot.channel.last_msg),
  34. style,
  35. ),
  36. "sent" => Span::styled(
  37. format!(" [S: {}]", slot.channel.last_msg),
  38. style,
  39. ),
  40. a => Span::styled(a.to_string(), style),
  41. };
  42. lines.push(Spans::from(vec![addr, msg]));
  43. }
  44. }
  45. if !node.inbound.iter().all(|node| node.is_empty) {
  46. lines.push(Spans::from(Span::styled(" Incoming", Style::default())));
  47. }
  48. for inbound in &node.inbound {
  49. let addr = Span::styled(format!(" {}", inbound.connected), style);
  50. let msg: Span = match inbound.channel.last_status.as_str() {
  51. "recv" => Span::styled(
  52. format!(" [R: {}]", inbound.channel.last_msg),
  53. style,
  54. ),
  55. "sent" => Span::styled(
  56. format!(" [R: {}]", inbound.channel.last_msg),
  57. style,
  58. ),
  59. a => Span::styled(a.to_string(), style),
  60. };
  61. lines.push(Spans::from(vec![addr, msg]));
  62. }
  63. lines.push(Spans::from(Span::styled(" Manual", Style::default())));
  64. for connect in &node.manual {
  65. lines.push(Spans::from(Span::styled(format!(" {}", connect.key), style)));
  66. }
  67. }
  68. None => {
  69. // TODO
  70. debug!("This is also a bug");
  71. }
  72. }
  73. let ids = ListItem::new(lines);
  74. nodes.push(ids);
  75. }
  76. let nodes =
  77. List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
  78. let slice = Layout::default()
  79. .direction(list_direction)
  80. .margin(list_margin)
  81. .constraints(list_cnstrnts)
  82. .split(f.size());
  83. f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
  84. render_info_right(view.clone(), f, slice);
  85. }
  86. fn render_info_right<B: Backend>(_view: View, f: &mut Frame<'_, B>, slice: Vec<Rect>) {
  87. let span = vec![];
  88. let graph =
  89. Paragraph::new(span).block(Block::default().borders(Borders::ALL)).style(Style::default());
  90. f.render_widget(graph, slice[1]);
  91. }