ui.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // we write all the span data to a Vec<String> for debugging purposes
  17. let mut data = Vec::new();
  18. let style = Style::default();
  19. for id in &view.id_list.node_id {
  20. let id_span = Span::raw(id.to_string());
  21. let mut lines = vec![Spans::from(id_span)];
  22. data.push(id.to_string());
  23. debug!("1 LINES: {:?}", lines);
  24. // create a new vector of addresses
  25. // render as a sub node
  26. match &view.info_list.infos.get(id) {
  27. Some(node) => {
  28. if !node.outbound.iter().all(|node| node.is_empty == true) {
  29. lines.push(Spans::from(Span::styled(" Outgoing", Style::default())));
  30. data.push("Outgoing".to_string());
  31. }
  32. for outbound in &node.outbound.clone() {
  33. for slot in outbound.slots.clone() {
  34. let addr = Span::styled(format!(" {}", slot.addr), style);
  35. data.push(format!("{}", slot.addr));
  36. let msg: Span = match slot.channel.last_status.as_str() {
  37. "recv" => Span::styled(
  38. format!(" [R: {}]", slot.channel.last_msg),
  39. style,
  40. ),
  41. "sent" => Span::styled(
  42. format!(" [S: {}]", slot.channel.last_msg),
  43. style,
  44. ),
  45. a => Span::styled(a.to_string(), style),
  46. };
  47. data.push(format!("{}", slot.channel.last_msg));
  48. lines.push(Spans::from(vec![addr, msg]));
  49. }
  50. }
  51. if !node.inbound.iter().all(|node| node.is_empty == true) {
  52. lines.push(Spans::from(Span::styled(" Incoming", Style::default())));
  53. data.push("Incoming".to_string());
  54. }
  55. for inbound in &node.inbound {
  56. let addr = Span::styled(format!(" {}", inbound.connected), style);
  57. data.push(format!("{}", inbound.connected));
  58. let msg: Span = match inbound.channel.last_status.as_str() {
  59. "recv" => Span::styled(
  60. format!(" [R: {}]", inbound.channel.last_msg),
  61. style,
  62. ),
  63. "sent" => Span::styled(
  64. format!(" [R: {}]", inbound.channel.last_msg),
  65. style,
  66. ),
  67. a => Span::styled(a.to_string(), style),
  68. };
  69. data.push(format!("{}", inbound.channel.last_msg));
  70. lines.push(Spans::from(vec![addr, msg]));
  71. }
  72. lines.push(Spans::from(Span::styled(" Manual", Style::default())));
  73. data.push("Manual".to_string());
  74. for connect in &node.manual {
  75. lines.push(Spans::from(Span::styled(format!(" {}", connect.key), style)));
  76. data.push(format!("{}", connect.key));
  77. }
  78. }
  79. None => {
  80. // TODO
  81. debug!("This is also a bug");
  82. }
  83. }
  84. debug!("2 LINES: {:?}", lines);
  85. let ids = ListItem::new(lines);
  86. debug!("1 IDS: {:?}", ids);
  87. nodes.push(ids);
  88. debug!("1 NODES: {:?}", nodes);
  89. }
  90. let nodes =
  91. List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
  92. let slice = Layout::default()
  93. .direction(list_direction)
  94. .margin(list_margin)
  95. .constraints(list_cnstrnts)
  96. .split(f.size());
  97. f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
  98. render_info_right(view.clone(), f, slice);
  99. }
  100. fn render_info_right<B: Backend>(_view: View, f: &mut Frame<'_, B>, slice: Vec<Rect>) {
  101. let span = vec![];
  102. let graph =
  103. Paragraph::new(span).block(Block::default().borders(Borders::ALL)).style(Style::default());
  104. f.render_widget(graph, slice[1]);
  105. }