فهرست منبع

map: improve data structures and simplify names.

lunar-mining 4 سال پیش
والد
کامیت
3dba0489bc
5فایلهای تغییر یافته به همراه115 افزوده شده و 64 حذف شده
  1. 17 7
      bin/map/src/app.rs
  2. 8 8
      bin/map/src/list.rs
  3. 1 0
      bin/map/src/main.rs
  4. 80 40
      bin/map/src/node.rs
  5. 9 9
      bin/map/src/ui.rs

+ 17 - 7
bin/map/src/app.rs

@@ -1,6 +1,6 @@
 use crate::{
-    list::StatefulList,
-    node::{NodeId, NodeInfo},
+    list::NodeIdList,
+    node::{NodeInfo, NodeInfoView},
 };
 use rand::Rng;
 use smol::Timer;
@@ -8,15 +8,25 @@ use std::{collections::HashMap, time::Duration};
 
 #[derive(Clone)]
 pub struct App {
-    pub node_list: StatefulList,
-    pub node_info: NodeInfo,
+    pub node_list: NodeIdList,
+    pub node_info: NodeInfoView,
 }
 
 impl App {
     pub fn new() -> App {
-        let node_info = NodeInfo::new();
-        let node_id = NodeId::new();
-        let node_list = StatefulList::new(node_id);
+        // node info struct w fields
+        // make 10 node info
+        let infos = vec![NodeInfo {
+            id: "sodisofjhosd".to_string(),
+            connections: 10,
+            is_active: true,
+            last_message: "hey how are you?".to_string(),
+        }];
+
+        let node_info = NodeInfoView::new(infos.clone());
+
+        let ids = vec![infos[0].id.clone()];
+        let node_list = NodeIdList::new(ids);
         App { node_list, node_info }
     }
 

+ 8 - 8
bin/map/src/list.rs

@@ -1,22 +1,22 @@
-use crate::node::{NodeId, NodeInfo};
+use crate::node::{NodeInfo, NodeInfoView};
 use std::collections::HashMap;
 use tui::widgets::ListState;
 
 #[derive(Clone)]
-pub struct StatefulList {
+pub struct NodeIdList {
     pub state: ListState,
-    pub nodes: NodeId,
+    pub node_id: Vec<String>,
 }
 
-impl StatefulList {
-    pub fn new(nodes: NodeId) -> StatefulList {
-        StatefulList { state: ListState::default(), nodes }
+impl NodeIdList {
+    pub fn new(node_id: Vec<String>) -> NodeIdList {
+        NodeIdList { state: ListState::default(), node_id }
     }
 
     pub fn next(&mut self) {
         let i = match self.state.selected() {
             Some(i) => {
-                if i >= self.nodes.id.len() - 1 {
+                if i >= self.node_id.len() - 1 {
                     0
                 } else {
                     i + 1
@@ -31,7 +31,7 @@ impl StatefulList {
         let i = match self.state.selected() {
             Some(i) => {
                 if i == 0 {
-                    self.nodes.id.len() - 1
+                    self.node_id.len() - 1
                 } else {
                     i - 1
                 }

+ 1 - 0
bin/map/src/main.rs

@@ -19,6 +19,7 @@ use tui::{
 pub mod app;
 pub mod list;
 pub mod node;
+pub mod types;
 pub mod ui;
 
 use crate::app::App;

+ 80 - 40
bin/map/src/node.rs

@@ -1,66 +1,106 @@
 use rand::Rng;
 
+// TODO: make Node data structure
+//       hashmap of node_id and node_info
+//       make NodeInfoView that implements scrolling
+//
+// NodeInfoView
+// next and previous updates index
+// and updates info content
 #[derive(Clone)]
-pub struct NodeInfo {
-    pub info: Vec<String>,
+pub struct NodeInfoView {
     pub index: usize,
 }
 
-impl NodeInfo {
-    pub fn new() -> NodeInfo {
-        let info = Self::make_info();
+impl NodeInfoView {
+    pub fn new(infos: Vec<NodeInfo>) -> NodeInfoView {
         let index = 0;
-        NodeInfo { info, index }
+        NodeInfoView { index }
     }
 
-    // set content
-    fn make_info() -> Vec<String> {
-        let mut node_info = Vec::new();
-        for num in 1..3 {
-            let new_info = format!(
-                "Connections: {}
-                                   ",
-                num
-            );
-            node_info.push(new_info.to_string());
-        }
-        node_info
-    }
+    //pub fn make_info() -> Vec<String> {
+    //    let mut node_info = Vec::new();
+    //    for num in 1..10 {
+    //        let new_info = format!("Connections: {}", num);
+    //        node_info.push(new_info.to_string());
+    //    }
+    //    node_info
+    //}
 
     pub fn next(&mut self) {
-        self.index = (self.index + 1) % self.info.len();
+        //self.index = (self.index + 1) % self.info.len();
     }
 
     pub fn previous(&mut self) {
-        if self.index > 0 {
-            self.index -= 1;
-        } else {
-            self.index = self.info.len() - 1;
-        }
+        //if self.index > 0 {
+        //    self.index -= 1;
+        //} else {
+        //    self.index = self.info.len() - 1;
+        //}
     }
 }
 
 #[derive(Clone)]
-pub struct NodeId {
-    pub id: Vec<String>,
+pub struct NodeInfo {
+    pub id: String,
+    pub connections: usize,
+    pub is_active: bool,
+    pub last_message: String,
 }
 
-impl NodeId {
-    pub fn new() -> NodeId {
-        let id = Self::get_node_id();
-        NodeId { id }
-    }
-    fn get_node_id() -> Vec<String> {
-        let mut node_list = Vec::new();
-        for num in 1..10 {
-            let mut rng = rand::thread_rng();
-            let new_nodes = format!("Node: {}", rng.gen::<u32>());
-            node_list.push(new_nodes);
-        }
-        node_list
+impl NodeInfo {
+    pub fn new() -> NodeInfo {
+        let connections = 0;
+        let is_active = false;
+        NodeInfo { id: String::new(), connections, is_active, last_message: String::new() }
     }
+
+    //pub fn make_info() -> Vec<String> {
+    //    let mut node_info = Vec::new();
+    //    for num in 1..10 {
+    //        let new_info = format!("Connections: {}", num);
+    //        node_info.push(new_info.to_string());
+    //    }
+    //    node_info
+    //}
+
+    //pub fn next(&mut self) {
+    //    self.index = (self.index + 1) % self.info.len();
+    //}
+
+    //pub fn previous(&mut self) {
+    //    if self.index > 0 {
+    //        self.index -= 1;
+    //    } else {
+    //        self.index = self.info.len() - 1;
+    //    }
+    //}
 }
 
+// Make a string
+// Type alias
+//#[derive(Clone)]
+//pub struct NodeId {
+//    // TODO: make this plural
+//    pub id: Vec<String>,
+//}
+//
+//impl NodeId {
+//    pub fn new() -> NodeId {
+//        let id = Self::get_node_id();
+//        NodeId { id }
+//    }
+//    fn get_node_id() -> Vec<String> {
+//        let mut node_list = Vec::new();
+//        for num in 1..100 {
+//            let mut rng = rand::thread_rng();
+//            let new_nodes = format!("Node: {}", rng.gen::<u32>());
+//            node_list.push(new_nodes);
+//        }
+//        node_list
+//    }
+//}
+
 // fn get_node_info() -> Vec<String> {
 //     let mut node_info = Vec::new();
 //     for _num in 1..100 {

+ 9 - 9
bin/map/src/ui.rs

@@ -1,6 +1,6 @@
 use crate::{
     app::App,
-    node::{NodeId, NodeInfo},
+    node::{NodeInfo, NodeInfoView},
 };
 use tui::{
     backend::Backend,
@@ -11,7 +11,6 @@ use tui::{
     Frame,
 };
 
-// pass node info
 pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
     let slice = Layout::default()
         .direction(Direction::Horizontal)
@@ -21,8 +20,7 @@ pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
 
     let nodes: Vec<ListItem> = app
         .node_list
-        .nodes
-        .id
+        .node_id
         .iter()
         .map(|(id)| {
             let line1 = Spans::from(id.to_string());
@@ -36,10 +34,12 @@ pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
 
     f.render_stateful_widget(nodes, slice[0], &mut app.node_list.state);
 
-    let text: Vec<Spans> = app.node_info.info.iter().map(|i| Spans::from(i.to_string())).collect();
-    let graph = Paragraph::new(text)
-        .block(Block::default().borders(Borders::ALL))
-        .style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD));
+    //// call make info here
+    //let text: Vec<Spans> = app.node_info.info.iter().map(|i| Spans::from(i.to_string())).collect();
+
+    //let graph = Paragraph::new(text)
+    //    .block(Block::default().borders(Borders::ALL))
+    //    .style(Style::default().fg(Color::LightCyan).add_modifier(Modifier::BOLD));
 
-    f.render_widget(graph, slice[1]);
+    //f.render_widget(graph, slice[1]);
 }