From 6edf54caba222f2399461452423ffeb0dce9e628 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Thu, 17 Feb 2022 11:02:40 +0100 Subject: [PATCH] map: fixed initalization error and improved formatting initalization error occurred due to render() attempting to draw an empty vector on first run. we now pause for 1 sec if the vector is empty for it to initalize. --- bin/map/src/main.rs | 47 ++++++++++++++++++++++++------------------ bin/map/src/ui.rs | 50 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index b039080a5..418d9368f 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -91,7 +91,7 @@ async fn main() -> Result<()> { terminal.clear()?; - let infos = vec![NodeInfo::new()]; + let infos = Vec::new(); let info_list = InfoList::new(infos.clone()); let ids = Vec::new(); let id_list = IdList::new(ids); @@ -138,38 +138,38 @@ async fn poll(client: Map, model: Arc) -> Result<()> { let outgoing = connections.get("outgoing").unwrap(); let incoming = connections.get("incoming").unwrap(); - let mut out_connections = Vec::new(); - let mut in_connections = Vec::new(); + let mut outconnects = Vec::new(); + let mut inconnects = Vec::new(); let out0 = Connection::new( - outgoing[0].get("id").unwrap().to_string(), - outgoing[0].get("message").unwrap().to_string(), + outgoing[0].get("id").unwrap().as_str().unwrap().to_string(), + outgoing[0].get("message").unwrap().as_str().unwrap().to_string(), ); let out1 = Connection::new( - outgoing[1].get("id").unwrap().to_string(), - outgoing[1].get("message").unwrap().to_string(), + outgoing[1].get("id").unwrap().as_str().unwrap().to_string(), + outgoing[1].get("message").unwrap().as_str().unwrap().to_string(), ); let in0 = Connection::new( - incoming[0].get("id").unwrap().to_string(), - incoming[0].get("message").unwrap().to_string(), + incoming[0].get("id").unwrap().as_str().unwrap().to_string(), + incoming[0].get("message").unwrap().as_str().unwrap().to_string(), ); let in1 = Connection::new( - incoming[1].get("id").unwrap().to_string(), - incoming[1].get("message").unwrap().to_string(), + incoming[1].get("id").unwrap().as_str().unwrap().to_string(), + incoming[1].get("message").unwrap().as_str().unwrap().to_string(), ); - out_connections.push(out0); - out_connections.push(out1); + outconnects.push(out0); + outconnects.push(out1); - in_connections.push(in0); - in_connections.push(in1); + inconnects.push(in0); + inconnects.push(in1); let infos = vec![NodeInfo { // TODO: should never crash id: id.as_str().unwrap().to_string(), - outgoing: out_connections, - incoming: in_connections, + outgoing: outconnects, + incoming: inconnects, }]; for node in infos { @@ -213,9 +213,16 @@ async fn render( model.id_list.node_id.lock().await.clone(), model.info_list.infos.lock().await.clone(), ); - terminal.draw(|f| { - ui::ui(f, view.clone()); - })?; + if view.info_list.infos.is_empty() { + // TODO: make this a loading widget + println!("Initializing..."); + async_util::sleep(1).await; + terminal.clear()?; + } else { + terminal.draw(|f| { + ui::ui(f, view.clone()); + })?; + } for k in asi.by_ref().keys() { match k.unwrap() { Key::Char('q') => { diff --git a/bin/map/src/ui.rs b/bin/map/src/ui.rs index 0bbb483d2..97a082094 100644 --- a/bin/map/src/ui.rs +++ b/bin/map/src/ui.rs @@ -3,7 +3,7 @@ use tui::{ backend::Backend, layout::{Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, - text::Spans, + text::{Span, Spans}, widgets::{Block, Borders, List, ListItem, Paragraph}, Frame, }; @@ -38,16 +38,52 @@ pub fn ui(f: &mut Frame<'_, B>, mut view: View) { fn render_info(view: View, f: &mut Frame<'_, B>, index: usize, slice: Vec) { let info = &view.info_list.infos; - let id = &info[index].id; - let i0 = info[index].incoming.clone(); + let iconnects = info[index].incoming.clone(); + let oconnects = info[index].outgoing.clone(); - let o0 = info[index].outgoing.clone(); + let mut iconnect_ids = Vec::new(); + let mut iconnect_msgs = Vec::new(); + + let mut oconnect_ids = Vec::new(); + let mut oconnect_msgs = Vec::new(); + + if !iconnects.is_empty() { + for connect in iconnects { + iconnect_ids.push(connect.id); + iconnect_msgs.push(connect.message) + } + } else { + // do nothing + } + + if !oconnects.is_empty() { + for connect in oconnects { + oconnect_ids.push(connect.id); + oconnect_msgs.push(connect.message) + } + } else { + // do nothing + } let span = vec![ - Spans::from(format!("NodeId: {}", id)), - Spans::from(format!("Outgoing connections: {:?}", i0)), - Spans::from(format!("Incoming connections: {:?}", o0)), + Spans::from(Span::styled( + "Outgoing connections:", + Style::default().add_modifier(Modifier::BOLD), + )), + Spans::from(format!(" {}", iconnect_ids[0])), + Spans::from(format!(" Last message: {}", oconnect_msgs[0])), + Spans::from(format!(" {}", iconnect_ids[1])), + Spans::from(format!(" Last message: {}", oconnect_msgs[1])), + Spans::from(format!("")), + Spans::from(Span::styled( + "Incoming connections:", + Style::default().add_modifier(Modifier::BOLD), + )), + Spans::from(format!(" {}", oconnect_ids[0])), + Spans::from(format!(" Last message: {}", oconnect_msgs[0])), + Spans::from(format!(" {}", oconnect_ids[1])), + Spans::from(format!(" Last message: {}", oconnect_msgs[1])), ]; let graph = Paragraph::new(span).block(Block::default().borders(Borders::ALL)).style(Style::default());