view.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. use darkfi::error::{Error, Result};
  2. use fxhash::{FxHashMap, FxHashSet};
  3. use tui::widgets::ListState;
  4. use tui::{
  5. backend::Backend,
  6. layout::{Constraint, Direction, Layout, Rect},
  7. style::Style,
  8. text::{Span, Spans},
  9. widgets::{Block, Borders, List, ListItem, Paragraph},
  10. Frame,
  11. };
  12. use crate::model::{NodeInfo, SelectableObject};
  13. //use log::debug;
  14. #[derive(Debug)]
  15. pub struct View {
  16. pub active_ids: IdListView,
  17. pub nodes: NodeInfoView,
  18. pub selectables: FxHashMap<String, SelectableObject>,
  19. pub msg_log: FxHashMap<String, Vec<(String, String)>>,
  20. }
  21. impl View {
  22. pub fn new(
  23. active_ids: IdListView,
  24. nodes: NodeInfoView,
  25. selectables: FxHashMap<String, SelectableObject>,
  26. msg_log: FxHashMap<String, Vec<(String, String)>>,
  27. ) -> View {
  28. View { active_ids, nodes, selectables, msg_log }
  29. }
  30. pub fn update(
  31. &mut self,
  32. nodes: FxHashMap<String, NodeInfo>,
  33. selectables: FxHashMap<String, SelectableObject>,
  34. msg_log: FxHashMap<String, Vec<(String, String)>>,
  35. ) {
  36. self.update_nodes(nodes);
  37. self.update_selectable(selectables);
  38. self.update_active_ids();
  39. self.update_msg_log(msg_log);
  40. }
  41. fn update_nodes(&mut self, nodes: FxHashMap<String, NodeInfo>) {
  42. for (id, node) in nodes {
  43. self.nodes.infos.insert(id, node);
  44. }
  45. }
  46. fn update_selectable(&mut self, selectables: FxHashMap<String, SelectableObject>) {
  47. for (id, obj) in selectables {
  48. self.selectables.insert(id, obj);
  49. }
  50. }
  51. fn update_active_ids(&mut self) {
  52. for info in self.nodes.infos.values() {
  53. self.active_ids.ids.insert(info.node_id.to_string());
  54. for child in &info.children {
  55. if !child.is_empty == true {
  56. self.active_ids.ids.insert(child.session_id.to_string());
  57. for child in &child.children {
  58. self.active_ids.ids.insert(child.connect_id.to_string());
  59. }
  60. }
  61. }
  62. }
  63. }
  64. fn update_msg_log(&mut self, msg_log: FxHashMap<String, Vec<(String, String)>>) {
  65. for (id, msg) in msg_log {
  66. self.msg_log.insert(id, msg);
  67. }
  68. }
  69. pub fn render<B: Backend>(&mut self, f: &mut Frame<'_, B>) {
  70. let mut nodes = Vec::new();
  71. let mut ids = Vec::new();
  72. let style = Style::default();
  73. let list_margin = 2;
  74. let list_direction = Direction::Horizontal;
  75. let list_cnstrnts = vec![Constraint::Percentage(50), Constraint::Percentage(50)];
  76. for info in self.nodes.infos.values() {
  77. let name_span = Span::raw(&info.node_name);
  78. let lines = vec![Spans::from(name_span)];
  79. let names = ListItem::new(lines);
  80. nodes.push(names);
  81. ids.push(&info.node_id);
  82. for session in &info.children {
  83. if !session.is_empty == true {
  84. let name = Span::styled(format!(" {}", session.session_name), style);
  85. let lines = vec![Spans::from(name)];
  86. let names = ListItem::new(lines);
  87. nodes.push(names);
  88. ids.push(&session.session_id);
  89. for connection in &session.children {
  90. let mut info = Vec::new();
  91. let name = Span::styled(format!(" {}", connection.addr), style);
  92. info.push(name);
  93. match connection.last_status.as_str() {
  94. "recv" => {
  95. let msg = Span::styled(
  96. format!(" [R: {}]", connection.last_msg),
  97. style,
  98. );
  99. info.push(msg);
  100. }
  101. "sent" => {
  102. let msg = Span::styled(
  103. format!(" [S: {}]", connection.last_msg),
  104. style,
  105. );
  106. info.push(msg);
  107. }
  108. _ => {
  109. // TODO
  110. }
  111. }
  112. let lines = vec![Spans::from(info)];
  113. let names = ListItem::new(lines);
  114. nodes.push(names);
  115. ids.push(&connection.connect_id);
  116. }
  117. }
  118. }
  119. }
  120. let slice = Layout::default()
  121. .direction(list_direction)
  122. .margin(list_margin)
  123. .constraints(list_cnstrnts)
  124. .split(f.size());
  125. let nodes =
  126. List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
  127. f.render_stateful_widget(nodes, slice[0], &mut self.active_ids.state);
  128. ids.dedup();
  129. // get the id at the current index
  130. match self.active_ids.state.selected() {
  131. Some(i) => {
  132. match ids.get(i) {
  133. Some(i) => {
  134. self.render_info(f, slice.clone(), i.to_string());
  135. // found id
  136. }
  137. None => {
  138. // TODO: Error
  139. }
  140. }
  141. }
  142. None => {
  143. // TODO: nothing is selected
  144. }
  145. }
  146. }
  147. fn render_info<B: Backend>(
  148. &mut self,
  149. f: &mut Frame<'_, B>,
  150. slice: Vec<Rect>,
  151. selected: String,
  152. ) {
  153. let style = Style::default();
  154. //let mut infos = Vec::new();
  155. let mut spans = Vec::new();
  156. //let mut msgs = Vec::new();
  157. let info = self.selectables.get(&selected);
  158. match info {
  159. Some(SelectableObject::Node(node)) => {
  160. let name_span = Spans::from("Node Info");
  161. spans.push(name_span);
  162. }
  163. Some(SelectableObject::Session(session)) => {
  164. let name_span = Spans::from("Session Info");
  165. spans.push(name_span);
  166. }
  167. Some(SelectableObject::Connect(connect)) => {
  168. let log = self.msg_log.get(&connect.connect_id);
  169. match log {
  170. Some(values) => {
  171. for (k, v) in values {
  172. match k.as_str() {
  173. "send" => {
  174. let msg_log =
  175. Spans::from(Span::styled(format!("S: {}", v), style));
  176. spans.push(msg_log);
  177. }
  178. "recv" => {
  179. let msg_log =
  180. Spans::from(Span::styled(format!("R: {}", v), style));
  181. spans.push(msg_log);
  182. }
  183. _ => {
  184. // TODO
  185. }
  186. }
  187. }
  188. }
  189. None => {
  190. // TODO
  191. }
  192. }
  193. }
  194. None => {
  195. // TODO
  196. }
  197. }
  198. let graph = Paragraph::new(spans)
  199. .block(Block::default().borders(Borders::ALL))
  200. .style(Style::default());
  201. f.render_widget(graph, slice[1]);
  202. }
  203. }
  204. #[derive(Debug, Clone)]
  205. pub struct IdListView {
  206. pub state: ListState,
  207. pub ids: FxHashSet<String>,
  208. }
  209. impl IdListView {
  210. pub fn new(ids: FxHashSet<String>) -> IdListView {
  211. IdListView { state: ListState::default(), ids }
  212. }
  213. pub fn next(&mut self) {
  214. let i = match self.state.selected() {
  215. Some(i) => {
  216. if i >= self.ids.len() - 1 {
  217. 0
  218. } else {
  219. i + 1
  220. }
  221. }
  222. None => 0,
  223. };
  224. self.state.select(Some(i));
  225. }
  226. pub fn previous(&mut self) {
  227. let i = match self.state.selected() {
  228. Some(i) => {
  229. if i == 0 {
  230. self.ids.len() - 1
  231. } else {
  232. i - 1
  233. }
  234. }
  235. None => 0,
  236. };
  237. self.state.select(Some(i));
  238. }
  239. pub fn unselect(&mut self) {
  240. self.state.select(None);
  241. }
  242. }
  243. #[derive(Debug, Clone)]
  244. pub struct NodeInfoView {
  245. pub index: usize,
  246. pub infos: FxHashMap<String, NodeInfo>,
  247. }
  248. impl NodeInfoView {
  249. pub fn new(infos: FxHashMap<String, NodeInfo>) -> NodeInfoView {
  250. let index = 0;
  251. NodeInfoView { index, infos }
  252. }
  253. pub fn next(&mut self) {
  254. self.index = (self.index + 1) % self.infos.len();
  255. }
  256. pub fn previous(&mut self) {
  257. if self.index > 0 {
  258. self.index -= 1;
  259. } else {
  260. self.index = self.infos.len() - 1;
  261. }
  262. }
  263. }