map: fixed list iterate error

This commit is contained in:
lunar-mining
2022-02-19 13:11:38 +01:00
parent ecdf01583a
commit a57a3c4e78
4 changed files with 24 additions and 16 deletions

View File

@@ -10,7 +10,7 @@ use darkfi::{
use async_std::sync::Arc;
use easy_parallel::Parallel;
use log::{debug, info};
use log::{debug, info, trace};
use serde_json::{json, Value};
use simplelog::*;
use smol::Executor;
@@ -86,6 +86,8 @@ async fn main() -> Result<()> {
let file = File::create(&*options.log_path).unwrap();
WriteLogger::init(lvl, cfg, file)?;
info!("Log level: {}", lvl);
let config_path = join_config_path(&PathBuf::from("map_config.toml"))?;
spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
@@ -142,7 +144,6 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
info!("RPC connected to {}", client.url);
loop {
let reply = client.get_info().await?;
info!("RPC reply: {}", reply);
if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
let id = reply.as_object().unwrap().get("id").unwrap();
@@ -200,7 +201,7 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
println!("Reply is an error");
}
async_util::sleep(2).await;
async_util::sleep(20).await;
}
}
@@ -217,13 +218,12 @@ async fn render<B: Backend>(
let info_list = InfoListView::new(Vec::new());
let mut view = View::new(id_list.clone(), info_list.clone());
view.id_list.state.select(Some(1));
view.info_list.index = 0;
view.id_list.state.select(Some(0));
//view.info_list.index = 0;
loop {
// on first run, add available nodes
// every time run the program, simply update nodes
let mut view = view.clone();
view.update(
model.id_list.node_id.lock().await.clone(),
model.info_list.infos.lock().await.clone(),
@@ -243,15 +243,15 @@ async fn render<B: Backend>(
match k.unwrap() {
Key::Char('q') => {
terminal.clear()?;
return Ok(())
return Ok(());
}
Key::Char('j') => {
view.id_list.next();
view.info_list.next().await;
//view.info_list.next().await;
}
Key::Char('k') => {
view.id_list.previous();
view.info_list.previous().await;
//view.info_list.previous().await;
}
_ => (),
}

View File

@@ -1,5 +1,6 @@
use async_std::sync::Mutex;
use darkfi::error::Result;
use log::debug;
use tui::widgets::ListState;
pub struct Model {

View File

@@ -1,4 +1,5 @@
use crate::view::View;
use log::debug;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
@@ -31,10 +32,10 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
let index = view.info_list.index;
//let index = view.info_list.index;
render_info_left(view.clone(), f, index);
render_info_right(view.clone(), f, index, slice);
//render_info_left(view.clone(), f, index);
//render_info_right(view.clone(), f, index, slice);
}
fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>, index: usize) {

View File

@@ -1,4 +1,5 @@
use crate::model::NodeInfo;
use log::debug;
use tui::widgets::ListState;
#[derive(Clone)]
@@ -13,8 +14,12 @@ impl View {
}
pub fn update(&mut self, node_id: Vec<String>, infos: Vec<NodeInfo>) {
self.id_list.update(node_id);
self.info_list.update(infos);
for node in node_id.clone() {
if !self.id_list.node_id.contains(&node) {
self.id_list.update(node_id.clone());
self.info_list.update(infos.clone());
}
}
}
}
@@ -45,9 +50,12 @@ impl IdListView {
pub fn previous(&mut self) {
let i = match self.state.selected() {
Some(i) => {
debug!("if {} == 0", i);
if i == 0 {
debug!("{} -1 ", self.node_id.len());
self.node_id.len() - 1
} else {
debug!("else {} -1 ", i);
i - 1
}
}
@@ -61,9 +69,7 @@ impl IdListView {
}
pub fn update(&mut self, node_id: Vec<String>) {
let index = 0;
for id in node_id {
self.state.select(Some(index));
self.node_id.push(id)
}
}