Просмотр исходного кода

ui: render Outbound, Inbound, and Manual connection data

created seperate functions to render the different data types.  frames
dynamically resize according to the length of the data.  we do this by
keeping track of the previous length and passing it into each new render.

there's a lot of boilerplate rn that needs to be cleaned up, and the
dynamic resizing can probably be further simplified.
lunar-mining 4 лет назад
Родитель
Сommit
827062ff96
2 измененных файлов с 160 добавлено и 80 удалено
  1. 8 8
      bin/dnetview/src/model.rs
  2. 152 72
      bin/dnetview/src/ui.rs

+ 8 - 8
bin/dnetview/src/model.rs

@@ -70,7 +70,7 @@ impl ManualInfo {
 
 #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
 pub struct OutboundInfo {
-    slots: Vec<Slot>,
+    pub slots: Vec<Slot>,
 }
 
 impl OutboundInfo {
@@ -81,9 +81,9 @@ impl OutboundInfo {
 
 #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
 pub struct Slot {
-    addr: String,
-    channel: Channel,
-    state: String,
+    pub addr: String,
+    pub channel: Channel,
+    pub state: String,
 }
 
 impl Slot {
@@ -94,8 +94,8 @@ impl Slot {
 
 #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
 pub struct Channel {
-    last_msg: String,
-    last_status: String,
+    pub last_msg: String,
+    pub last_status: String,
 }
 
 impl Channel {
@@ -106,8 +106,8 @@ impl Channel {
 
 #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
 pub struct InboundInfo {
-    connected: String,
-    channel: Channel,
+    pub connected: String,
+    pub channel: Channel,
 }
 
 impl InboundInfo {

+ 152 - 72
bin/dnetview/src/ui.rs

@@ -1,4 +1,5 @@
 use crate::view::View;
+//use log::debug;
 
 use tui::{
     backend::Backend,
@@ -21,13 +22,7 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
         .node_id
         .iter()
         .map(|id| {
-            let mut lines = vec![Spans::from(id.to_string())];
-            lines.push(Spans::from(""));
-            lines.push(Spans::from(""));
-            lines.push(Spans::from(""));
-            lines.push(Spans::from(""));
-            lines.push(Spans::from(""));
-            lines.push(Spans::from(""));
+            let lines = vec![Spans::from(id.to_string())];
             ListItem::new(lines).style(Style::default())
         })
         .collect();
@@ -39,111 +34,196 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
 
     let index = view.info_list.index;
 
-    render_info_left(view.clone(), f, index);
+    render_info_left(view.clone(), f);
     render_info_right(view.clone(), f, index, slice);
 }
 
-fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>, _index: usize) {
-    let slice = Layout::default()
+fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>) {
+    let length = render_outbound(view.clone(), f);
+    let length = render_inbound(view.clone(), f, length);
+    render_manual(view.clone(), f, length);
+}
+
+fn render_manual<B: Backend>(view: View, f: &mut Frame<'_, B>, length: usize) {
+    let new_num: u16 = length.try_into().unwrap();
+    let title_slice = Layout::default()
         .direction(Direction::Horizontal)
         .horizontal_margin(8)
-        .vertical_margin(4)
+        .vertical_margin(new_num)
         .constraints([Constraint::Percentage(100)].as_ref())
         .split(f.size());
 
-    let info = &view.info_list.infos;
+    let info_slice = Layout::default()
+        .direction(Direction::Horizontal)
+        .horizontal_margin(10)
+        .vertical_margin(new_num)
+        .constraints([Constraint::Percentage(45), Constraint::Percentage(55)].as_ref())
+        .split(f.size());
 
-    let mut titles = Vec::new();
+    let info = &view.info_list.infos;
+    let mut title = Vec::new();
     for id in &view.id_list.node_id {
         match info.get(id) {
             Some(_) => {
-                titles.push(Spans::from(Span::styled("Outbound:", Style::default())));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(Span::styled("Inbound:", Style::default())));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(Span::styled("Manual:", Style::default())));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(""));
-                titles.push(Spans::from(""));
+                title.push(Spans::from(Span::styled("Manual:", Style::default())));
             }
             None => {
                 // TODO
             }
         }
     }
-    let title_graph = Paragraph::new(titles).style(Style::default()).alignment(Alignment::Left);
 
-    f.render_widget(title_graph, slice[0]);
+    let mut man_info = Vec::new();
+    for id in &view.id_list.node_id {
+        match info.get(id) {
+            Some(connects) => {
+                man_info.push(Spans::from(""));
+                man_info.push(Spans::from(format!("Key: {}", connects.manual[0].key)));
+                man_info.push(Spans::from(""));
+            }
+            None => {
+                // TODO
+            }
+        }
+    }
 
-    let slice = Layout::default()
+    let info_graph = Paragraph::new(man_info).style(Style::default()).alignment(Alignment::Left);
+    let title_graph = Paragraph::new(title).style(Style::default()).alignment(Alignment::Left);
+
+    f.render_widget(info_graph, info_slice[0]);
+    f.render_widget(title_graph, title_slice[0]);
+}
+fn render_inbound<B: Backend>(view: View, f: &mut Frame<'_, B>, length: usize) -> usize {
+    let mut inbound_info = Vec::new();
+    // TODO: find better way of doing this
+    let new_num: u16 = length.try_into().unwrap();
+    let num = new_num + 3;
+    let title_slice = Layout::default()
         .direction(Direction::Horizontal)
-        .horizontal_margin(6)
-        .vertical_margin(4)
-        .constraints([Constraint::Percentage(47), Constraint::Percentage(53)].as_ref())
+        .horizontal_margin(8)
+        .vertical_margin(num)
+        .constraints([Constraint::Percentage(100)].as_ref())
         .split(f.size());
 
-    let mut msgs = Vec::new();
+    let info_slice = Layout::default()
+        .direction(Direction::Horizontal)
+        .horizontal_margin(10)
+        .vertical_margin(num)
+        .constraints([Constraint::Percentage(45), Constraint::Percentage(55)].as_ref())
+        .split(f.size());
 
+    let info = &view.info_list.infos;
+    let mut title = Vec::new();
     for id in &view.id_list.node_id {
-        //match info.get(id) {
-        //    Some(connects) => {
-        //        msgs.push(Spans::from(""));
-        //        msgs.push(Spans::from(format!("[R: {}]", connects.outbound[0].message)));
-        //        //msgs.push(Spans::from(format!("[S: {}]", connects.outgoing[1].message)));
-        //        msgs.push(Spans::from(""));
-        //        msgs.push(Spans::from(format!("[R: {}]", connects.inbound[0].message)));
-        //        //msgs.push(Spans::from(format!("[S: {}]", connects.incoming[1].message)));
-        //        msgs.push(Spans::from(""));
-        //        msgs.push(Spans::from(""));
-        //        msgs.push(Spans::from(format!("[R: {}]", connects.manual[0].message)));
-        //        msgs.push(Spans::from(""));
-        //    }
-        //    None => {
-        //        // TODO
-        //    }
-        //}
+        match info.get(id) {
+            Some(_) => {
+                title.push(Spans::from(Span::styled("Inbound:", Style::default())));
+            }
+            None => {
+                // TODO
+            }
+        }
     }
 
-    let msg_graph = Paragraph::new(msgs).style(Style::default()).alignment(Alignment::Right);
+    for id in &view.id_list.node_id {
+        match info.get(id) {
+            Some(connects) => {
+                if connects.inbound.is_empty() {
+                    inbound_info.push(Spans::from(""));
+                    inbound_info.push(Spans::from(format!("Connected: Null")));
+                    inbound_info.push(Spans::from(format!("Last msg: Null")));
+                    inbound_info.push(Spans::from(format!("Last status: Null")));
+                } else {
+                    for connect in &connects.inbound {
+                        inbound_info.push(Spans::from(""));
+                        inbound_info.push(Spans::from(format!("Connected: {}", connect.connected)));
+                        inbound_info
+                            .push(Spans::from(format!("Last msg: {}", connect.channel.last_msg)));
+                        inbound_info.push(Spans::from(format!(
+                            "Last status: {}",
+                            connect.channel.last_status
+                        )));
+                    }
+                }
+            }
+            None => {
+                // TODO
+            }
+        }
+    }
+    for _n in 1..info.len() {
+        inbound_info.push(Spans::from(""))
+    }
 
-    f.render_widget(msg_graph, slice[0]);
+    let info_graph =
+        Paragraph::new(inbound_info.clone()).style(Style::default()).alignment(Alignment::Left);
+    let title_graph =
+        Paragraph::new(title.clone()).style(Style::default()).alignment(Alignment::Left);
 
-    let slice = Layout::default()
+    f.render_widget(info_graph, info_slice[0]);
+    f.render_widget(title_graph, title_slice[0]);
+
+    return inbound_info.len() + title.len() + length + 2;
+}
+
+fn render_outbound<B: Backend>(view: View, f: &mut Frame<'_, B>) -> usize {
+    let title_slice = Layout::default()
+        .direction(Direction::Horizontal)
+        .horizontal_margin(8)
+        .vertical_margin(4)
+        .constraints([Constraint::Percentage(100)].as_ref())
+        .split(f.size());
+
+    let info_slice = Layout::default()
         .direction(Direction::Horizontal)
         .horizontal_margin(10)
         .vertical_margin(4)
         .constraints([Constraint::Percentage(45), Constraint::Percentage(55)].as_ref())
         .split(f.size());
 
-    let mut ids = Vec::new();
+    let info = &view.info_list.infos;
+    let mut title = Vec::new();
+    for id in &view.id_list.node_id {
+        match info.get(id) {
+            Some(_) => {
+                title.push(Spans::from(Span::styled("Outbound:", Style::default())));
+            }
+            None => {
+                // TODO
+            }
+        }
+    }
 
+    let mut slots = Vec::new();
     for id in &view.id_list.node_id {
-        //match info.get(id) {
-        //    Some(connects) => {
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(format!("{}", connects.outbound[0].id)));
-        //        //ids.push(Spans::from(format!("{}", connects.outgoing[1].id)));
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(format!("{}", connects.inbound[0].id)));
-        //        //ids.push(Spans::from(format!("{}", connects.incoming[1].id)));
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(format!("{}", connects.manual[0].id)));
-        //        ids.push(Spans::from(""));
-        //        ids.push(Spans::from(""));
-        //    }
-        //    None => {
-        //        // TODO
-        //    }
-        //}
+        match info.get(id) {
+            Some(connects) => {
+                for slot in &connects.outbound[0].slots {
+                    slots.push(Spans::from(""));
+                    slots.push(Spans::from(format!("Addr: {}", slot.addr)));
+                    slots.push(Spans::from(format!("Last msg: {}", slot.channel.last_msg)));
+                    slots.push(Spans::from(format!("Last status: {}", slot.channel.last_status)));
+                }
+            }
+            None => {
+                // TODO
+            }
+        }
     }
+    for _n in 1..info.len() {
+        slots.push(Spans::from(""))
+    }
+
+    let info_graph =
+        Paragraph::new(slots.clone()).style(Style::default()).alignment(Alignment::Left);
+    let title_graph =
+        Paragraph::new(title.clone()).style(Style::default()).alignment(Alignment::Left);
 
-    let id_graph = Paragraph::new(ids).style(Style::default()).alignment(Alignment::Left);
+    f.render_widget(info_graph, info_slice[0]);
+    f.render_widget(title_graph, title_slice[0]);
 
-    f.render_widget(id_graph, slice[0]);
+    let out_len = slots.len() + title.len();
+    return out_len;
 }
 
 fn render_info_right<B: Backend>(