Browse Source

dnetview: implement previous() and next() on msg list

lunar-mining 4 years ago
parent
commit
f07a340fdd
2 changed files with 26 additions and 44 deletions
  1. 4 31
      bin/dnetview/src/main.rs
  2. 22 13
      bin/dnetview/src/view.rs

+ 4 - 31
bin/dnetview/src/main.rs

@@ -260,10 +260,6 @@ async fn parse_data(
 async fn update_msgs(model: Arc<Model>, sessions: Vec<SessionInfo>) -> DnetViewResult<()> {
 async fn update_msgs(model: Arc<Model>, sessions: Vec<SessionInfo>) -> DnetViewResult<()> {
     for session in sessions {
     for session in sessions {
         for connection in session.children {
         for connection in session.children {
-            //// update the data
-            //for connect in connection.msg_log.clone() {
-            //    model.msg_log.lock().await.push(connect);
-            //}
             if !model.msg_map.lock().await.contains_key(&connection.id) {
             if !model.msg_map.lock().await.contains_key(&connection.id) {
                 // we don't have this ID: it is a new node
                 // we don't have this ID: it is a new node
                 model.msg_map.lock().await.insert(connection.id, connection.msg_log.clone());
                 model.msg_map.lock().await.insert(connection.id, connection.msg_log.clone());
@@ -584,11 +580,10 @@ async fn render_view<B: Backend>(
     terminal.clear()?;
     terminal.clear()?;
 
 
     let nodes = NodeInfoView::new(FxHashMap::default());
     let nodes = NodeInfoView::new(FxHashMap::default());
-    let msg_list = MsgList::new(FxHashMap::default());
+    let msg_list = MsgList::new(FxHashMap::default(), 0);
     let id_list = IdListView::new(Vec::new());
     let id_list = IdListView::new(Vec::new());
     let selectables = FxHashMap::default();
     let selectables = FxHashMap::default();
 
 
-    // pass msg list into view
     let mut view = View::new(nodes, msg_list, id_list, selectables);
     let mut view = View::new(nodes, msg_list, id_list, selectables);
     view.id_list.state.select(Some(0));
     view.id_list.state.select(Some(0));
     view.msg_list.state.select(Some(0));
     view.msg_list.state.select(Some(0));
@@ -627,32 +622,10 @@ async fn render_view<B: Backend>(
                     view.id_list.previous();
                     view.id_list.previous();
                 }
                 }
                 Key::Char('n') => {
                 Key::Char('n') => {
-                    debug!("STATE: {:?}", view.msg_list.state);
-                    //view.msg_list.next();
+                    view.msg_list.next();
                 }
                 }
-                Key::Char('\n') => {
-                    // select an id
-                    //match view.active_ids.state.selected() {
-                    //    // behavior:
-                    //    //          get selected index
-                    //    //          return id at index
-                    //    //          if it's a connection id
-                    //    //              msg_log = msg_map.get(id)
-                    //    //          Key::Char('n')
-                    //    //              msg_log.next()
-                    //    //
-                    //    //
-                    //    //Some(i) => match view.id_list.get(i) {
-                    //    //    //Some(i) => {
-                    //    //    //    self.render_info(f, slice.clone(), i.to_string())?;
-                    //    //    //    Ok(())
-                    //    //    //}
-                    //    //    //None => Err(DnetViewError::NoIdAtIndex),
-                    //    //},
-                    //    //// nothing is selected right now
-                    //    //None => Ok(()),
-                    //}
-                    // if the id at this index is a connection, render the msg log
+                Key::Char('p') => {
+                    view.msg_list.previous();
                 }
                 }
                 _ => (),
                 _ => (),
             }
             }

+ 22 - 13
bin/dnetview/src/view.rs

@@ -1,4 +1,3 @@
-//use darkfi::error::{Error, Result};
 use fxhash::FxHashMap;
 use fxhash::FxHashMap;
 use tui::widgets::ListState;
 use tui::widgets::ListState;
 
 
@@ -115,7 +114,15 @@ impl<'a> View {
             match self.id_list.state.selected() {
             match self.id_list.state.selected() {
                 Some(i) => match self.id_list.ids.get(i) {
                 Some(i) => match self.id_list.ids.get(i) {
                     Some(i) => {
                     Some(i) => {
+                        match self.msg_list.msg_log.get(i) {
+                            Some(i) => {
+                                // set the length of msg_list to current len
+                                self.msg_list.msg_len = i.len();
+                            }
+                            None => {}
+                        }
                         self.render_info(f, slice.clone(), i.to_string())?;
                         self.render_info(f, slice.clone(), i.to_string())?;
+                        //self.msg_auto_scroll(f);
                         Ok(())
                         Ok(())
                     }
                     }
                     None => Err(DnetViewError::NoIdAtIndex),
                     None => Err(DnetViewError::NoIdAtIndex),
@@ -292,12 +299,12 @@ impl<'a> View {
         Ok(())
         Ok(())
     }
     }
 
 
-    //fn msg_auto_scroll<B: Backend>(&mut self, f: &mut Frame<'_, B>) {
-    //    let rect = f.size();
-    //    if usize::from(rect.height) < self.msg_list.msg_log.len() {
-    //        self.msg_list.next();
-    //    }
-    //}
+    fn msg_auto_scroll<B: Backend>(&mut self, f: &mut Frame<'_, B>) {
+        let rect = f.size();
+        if usize::from(rect.height) < self.msg_list.msg_len {
+            self.msg_list.previous();
+        }
+    }
 }
 }
 
 
 #[derive(Debug, Clone)]
 #[derive(Debug, Clone)]
@@ -347,19 +354,21 @@ impl IdListView {
 pub struct MsgList {
 pub struct MsgList {
     pub state: ListState,
     pub state: ListState,
     pub msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
     pub msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
+    pub msg_len: usize,
 }
 }
 
 
 impl MsgList {
 impl MsgList {
-    pub fn new(msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>) -> MsgList {
-        MsgList { state: ListState::default(), msg_log }
+    pub fn new(
+        msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
+        msg_len: usize,
+    ) -> MsgList {
+        MsgList { state: ListState::default(), msg_log, msg_len }
     }
     }
 
 
     pub fn next(&mut self) {
     pub fn next(&mut self) {
-        //debug!("CONTENT {:?}", self.msg_log.values());
-        //debug!("MSG LOG LEN: {}", self.msg_log.values().len());
         let i = match self.state.selected() {
         let i = match self.state.selected() {
             Some(i) => {
             Some(i) => {
-                if i >= self.msg_log.values().len() - 1 {
+                if i >= self.msg_len - 1 {
                     0
                     0
                 } else {
                 } else {
                     i + 1
                     i + 1
@@ -374,7 +383,7 @@ impl MsgList {
         let i = match self.state.selected() {
         let i = match self.state.selected() {
             Some(i) => {
             Some(i) => {
                 if i == 0 {
                 if i == 0 {
-                    self.msg_log.len() - 1
+                    self.msg_len - 1
                 } else {
                 } else {
                     i - 1
                     i - 1
                 }
                 }