ui.rs 4.6 KB

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