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

map: remove loading page and initialize empty variables directly

lunar-mining 4 лет назад
Родитель
Сommit
2ad2198183
2 измененных файлов с 143 добавлено и 129 удалено
  1. 89 89
      bin/map/src/main.rs
  2. 54 40
      bin/map/src/ui.rs

+ 89 - 89
bin/map/src/main.rs

@@ -152,76 +152,76 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
     debug!("Attemping to poll: {}", client.url);
     // TODO: fix this! this can lag forever
     // check connect() on net/connector.rs
-    let reply = client.ping().await?;
-    if reply.as_str().is_some() {
-        let mut index = 0;
-        loop {
-            debug!("Connected to: {}", client.url);
-            let reply = client.get_info().await?;
-
-            if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
-                let id = reply.as_object().unwrap().get("id").unwrap();
-
-                let connections = reply.as_object().unwrap().get("connections").unwrap();
-                let outgoing = connections.get("outgoing").unwrap();
-                let incoming = connections.get("incoming").unwrap();
-
-                let mut outconnects = Vec::new();
-                let mut inconnects = Vec::new();
-
-                // here we are simulating new messages by scrolling through a vector
-                let msgs = outgoing[1].get("message").unwrap();
-                if index == 0 {
-                    index += 1;
-                } else if index >= 5 {
-                    index = 0
-                } else {
-                    index = index + 1;
-                }
+    //let reply = client.ping().await?;
+    //if reply.as_str().is_some() {
+    let mut index = 0;
+    loop {
+        //debug!("Connected to: {}", client.url);
+        let reply = client.get_info().await?;
 
-                let out0 = Connection::new(
-                    outgoing[0].get("id").unwrap().as_str().unwrap().to_string(),
-                    msgs[index].as_str().unwrap().to_string(),
-                );
-                let out1 = Connection::new(
-                    outgoing[1].get("id").unwrap().as_str().unwrap().to_string(),
-                    msgs[index].as_str().unwrap().to_string(),
-                );
-
-                let in0 = Connection::new(
-                    incoming[0].get("id").unwrap().as_str().unwrap().to_string(),
-                    msgs[index].as_str().unwrap().to_string(),
-                );
-                let in1 = Connection::new(
-                    incoming[1].get("id").unwrap().as_str().unwrap().to_string(),
-                    msgs[index].as_str().unwrap().to_string(),
-                );
-
-                outconnects.push(out0);
-                outconnects.push(out1);
-
-                inconnects.push(in0);
-                inconnects.push(in1);
-
-                let infos = NodeInfo { outgoing: outconnects, incoming: inconnects };
-
-                let mut node_info = HashMap::new();
-                node_info.insert(id.as_str().unwrap().to_string(), infos);
-
-                for (id, value) in node_info.clone() {
-                    model.id_list.node_id.lock().await.insert(id.clone());
-                    model.info_list.infos.lock().await.insert(id, value);
-                }
+        if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
+            let id = reply.as_object().unwrap().get("id").unwrap();
+
+            let connections = reply.as_object().unwrap().get("connections").unwrap();
+            let outgoing = connections.get("outgoing").unwrap();
+            let incoming = connections.get("incoming").unwrap();
+
+            let mut outconnects = Vec::new();
+            let mut inconnects = Vec::new();
+
+            // here we are simulating new messages by scrolling through a vector
+            let msgs = outgoing[1].get("message").unwrap();
+            if index == 0 {
+                index += 1;
+            } else if index >= 5 {
+                index = 0
             } else {
-                // TODO: error handling
-                debug!("Reply is empty");
+                index = index + 1;
             }
-            async_util::sleep(2).await;
+
+            let out0 = Connection::new(
+                outgoing[0].get("id").unwrap().as_str().unwrap().to_string(),
+                msgs[index].as_str().unwrap().to_string(),
+            );
+            let out1 = Connection::new(
+                outgoing[1].get("id").unwrap().as_str().unwrap().to_string(),
+                msgs[index].as_str().unwrap().to_string(),
+            );
+
+            let in0 = Connection::new(
+                incoming[0].get("id").unwrap().as_str().unwrap().to_string(),
+                msgs[index].as_str().unwrap().to_string(),
+            );
+            let in1 = Connection::new(
+                incoming[1].get("id").unwrap().as_str().unwrap().to_string(),
+                msgs[index].as_str().unwrap().to_string(),
+            );
+
+            outconnects.push(out0);
+            outconnects.push(out1);
+
+            inconnects.push(in0);
+            inconnects.push(in1);
+
+            let infos = NodeInfo { outgoing: outconnects, incoming: inconnects };
+
+            let mut node_info = HashMap::new();
+            node_info.insert(id.as_str().unwrap().to_string(), infos);
+
+            for (id, value) in node_info.clone() {
+                model.id_list.node_id.lock().await.insert(id.clone());
+                model.info_list.infos.lock().await.insert(id, value);
+            }
+        } else {
+            // TODO: error handling
+            debug!("Reply is empty");
         }
-    } else {
-        async_util::sleep(10).await;
-        Err(Error::ConnectTimeout)
+        async_util::sleep(2).await;
     }
+    //} else {
+    //    async_util::sleep(10).await;
+    //    Err(Error::ConnectTimeout)
+    //}
 }
 
 async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> io::Result<()> {
@@ -235,37 +235,37 @@ async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> io
 
     view.id_list.state.select(Some(0));
     view.info_list.index = 0;
-    let mut counter = 0;
+    //let mut counter = 0;
 
     loop {
-        counter = counter + 1;
+        //counter = counter + 1;
         view.update(model.info_list.infos.lock().await.clone());
-        if view.id_list.node_id.is_empty() {
-            // TODO: delete this and display empty data
-            if counter == 1 {
-                let mut progress = 0;
-                while progress < 100 {
-                    terminal.draw(|f| {
-                        ui::init_panel(f, progress);
-                    })?;
-                    Timer::after(Duration::from_millis(1)).await;
-                    progress = progress + 1;
-                }
-            } else if counter == 2 {
-                terminal.clear()?;
-                // TODO: continue to display not, mark as offline
-                println!("Could not connect to node. Are you sure RPC is running?");
-            }
-        } else {
-            terminal.draw(|f| {
-                ui::ui(f, view.clone());
-            })?;
-        }
+        //if view.id_list.node_id.is_empty() {
+        //    // TODO: delete this and display empty data
+        //    if counter == 1 {
+        //        let mut progress = 0;
+        //        while progress < 100 {
+        //            terminal.draw(|f| {
+        //                ui::init_panel(f, progress);
+        //            })?;
+        //            Timer::after(Duration::from_millis(1)).await;
+        //            progress = progress + 1;
+        //        }
+        //    } else if counter == 2 {
+        //        terminal.clear()?;
+        //        // TODO: continue to display not, mark as offline
+        //        println!("Could not connect to node. Are you sure RPC is running?");
+        //    }
+        //} else {
+        terminal.draw(|f| {
+            ui::ui(f, view.clone());
+        })?;
+        //}
         for k in asi.by_ref().keys() {
             match k.unwrap() {
                 Key::Char('q') => {
                     terminal.clear()?;
-                    return Ok(())
+                    return Ok(());
                 }
                 Key::Char('j') => {
                     view.id_list.next();

+ 54 - 40
bin/map/src/ui.rs

@@ -1,4 +1,6 @@
 use crate::view::View;
+use log::debug;
+
 use tui::{
     backend::Backend,
     layout::{Constraint, Direction, Layout, Rect},
@@ -23,29 +25,41 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
         .iter()
         .map(|id| {
             let mut lines = vec![Spans::from(id.to_string())];
-            // TODO: handle the None case
             // TODO: fix formatting (indentation must be margins)
-            let connects = info.get(id).unwrap();
-            for _line in lines.clone() {
-                lines.push(Spans::from(Span::styled("  Outgoing:", Style::default())));
-                lines.push(Spans::from(format!(
-                    "    {}         [R: {}]",
-                    connects.outgoing[0].id, connects.outgoing[0].message
-                )));
-                lines.push(Spans::from(format!(
-                    "    {}         [S: {}]",
-                    connects.outgoing[1].id, connects.outgoing[1].message
-                )));
-                lines.push(Spans::from(Span::styled("  Incoming:", Style::default())));
-                lines.push(Spans::from(format!(
-                    "    {}         [R: {}]",
-                    connects.incoming[0].id, connects.incoming[0].message
-                )));
-                lines.push(Spans::from(format!(
-                    "    {}         [S: {}]",
-                    connects.incoming[1].id, connects.incoming[1].message
-                )));
-            }
+            match info.get(id) {
+                Some(connects) => {
+                    for _line in lines.clone() {
+                        lines.push(Spans::from(Span::styled("  Outgoing:", Style::default())));
+                        lines.push(Spans::from(format!(
+                            "    {}         [R: {}]",
+                            connects.outgoing[0].id, connects.outgoing[0].message
+                        )));
+                        lines.push(Spans::from(format!(
+                            "    {}         [S: {}]",
+                            connects.outgoing[1].id, connects.outgoing[1].message
+                        )));
+                        lines.push(Spans::from(Span::styled("  Incoming:", Style::default())));
+                        lines.push(Spans::from(format!(
+                            "    {}         [R: {}]",
+                            connects.incoming[0].id, connects.incoming[0].message
+                        )));
+                        lines.push(Spans::from(format!(
+                            "    {}         [S: {}]",
+                            connects.incoming[1].id, connects.incoming[1].message
+                        )));
+                    }
+                }
+                None => {
+                    for _line in lines.clone() {
+                        lines.push(Spans::from(Span::styled("  Outgoing:", Style::default())));
+                        lines.push(Spans::from(format!("             [R: ]",)));
+                        lines.push(Spans::from(format!("             [S: ]",)));
+                        lines.push(Spans::from(Span::styled("  Incoming:", Style::default())));
+                        lines.push(Spans::from(format!("            [R: ]",)));
+                        lines.push(Spans::from(format!("             [S: ]",)));
+                    }
+                }
+            };
             ListItem::new(lines).style(Style::default())
         })
         .collect();
@@ -72,21 +86,21 @@ fn render_info_right<B: Backend>(
     f.render_widget(graph, slice[1]);
 }
 
-pub fn init_panel<B: Backend>(f: &mut Frame<'_, B>, progress: u16) {
-    let slice = Layout::default()
-        .direction(Direction::Vertical)
-        .margin(2)
-        .constraints([Constraint::Percentage(7), Constraint::Percentage(100)].as_ref())
-        .split(f.size());
-
-    let gauge = Gauge::default()
-        .block(Block::default().borders(Borders::ALL).title("Initializing..."))
-        .gauge_style(Style::default().fg(Color::Cyan))
-        .percent(progress);
-
-    f.render_widget(gauge, slice[0]);
-}
-
-pub fn error<B: Backend>(f: &mut Frame<'_, B>) {
-    // thing
-}
+//pub fn init_panel<B: Backend>(f: &mut Frame<'_, B>, progress: u16) {
+//    let slice = Layout::default()
+//        .direction(Direction::Vertical)
+//        .margin(2)
+//        .constraints([Constraint::Percentage(7), Constraint::Percentage(100)].as_ref())
+//        .split(f.size());
+//
+//    let gauge = Gauge::default()
+//        .block(Block::default().borders(Borders::ALL).title("Initializing..."))
+//        .gauge_style(Style::default().fg(Color::Cyan))
+//        .percent(progress);
+//
+//    f.render_widget(gauge, slice[0]);
+//}
+//
+//pub fn error<B: Backend>(f: &mut Frame<'_, B>) {
+//    // thing
+//}