From 9b4909963d6a84f3d2a9afc6df5a726ad55d5122 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 17 Jan 2021 21:36:44 -0800 Subject: [PATCH] skeleton code for fetching channel data Ready for open_channel to be implemented --- tui/src/http.rs | 13 ++++++++++++- tui/src/main.rs | 33 ++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/tui/src/http.rs b/tui/src/http.rs index 3f8329f..6e1f639 100644 --- a/tui/src/http.rs +++ b/tui/src/http.rs @@ -1,8 +1,9 @@ +use cursive::Cursive; use reqwest; use crate::types::Channel; pub async fn fetch_channels(domain: &str) -> Option>{ - if let Ok(resp) = reqwest::get(format!("{}/channels/list", domain)).await { + if let Ok(resp) = reqwest::get(&format!("{}/channels/list", domain)).await { let bytes = resp.bytes().await.unwrap(); let res: Result, serde_json::Error> = serde_json::from_slice(&bytes); return match res { @@ -11,4 +12,14 @@ pub async fn fetch_channels(domain: &str) -> Option>{ }; } return None; +} + + +pub mod sync { + use cursive::Cursive; + use std::net::Ipv4Addr; + + pub fn open_channel(ip: Ipv4Addr, name: &str, s: &mut Cursive) { + } + } \ No newline at end of file diff --git a/tui/src/main.rs b/tui/src/main.rs index a54bbba..bbecf55 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -10,7 +10,8 @@ use std::path::PathBuf; use clap::{Arg, App}; use cursive::Cursive; -use cursive::views::Dialog; +use cursive::views::{Dialog, TextView}; +use cursive::menu::MenuTree; use cursive::event::Key; use serde_json; @@ -66,6 +67,7 @@ async fn main() { app.add_global_callback('q', Cursive::quit); app.add_global_callback(Key::Esc, |s| s.select_menubar()); + app.set_autohide_menu(false); // don't hide the menubar all the time @@ -73,23 +75,24 @@ async fn main() { for server in config.servers.iter() { let name = match &server.name { Some(name) => name.to_string(), - None => String::from("None") + //None => String::from(&format!("{}", server.ip)) + None => String::from("None") }; - match http::fetch_channels(&server.ip).await { - Some(channels) => { - // add the channels to the current sub tree - for channel in channels { - app.menubar().add_leaf(&name, move |appref| { - let address = format!("{}", channel.ip); - appref.add_layer(Dialog::info(address)); - }); - } - }, - _ => {} - } + app.menubar().add_subtree(&name, MenuTree::new()); // add server name + // on action: + // open up search able list of channels + // choose from that list of channels which one you want to see - app.menubar().add_leaf(name, |_| {/* */}); + if let Some(channels) = http::fetch_channels(&server.ip).await { + // add a bunch of actionable leafs to our sub tree + for channel in channels { + app.menubar().find_subtree(name.as_ref()).unwrap().add_leaf(channel.name.clone(), move |s| { + let (ip, name) = channel.parts(); + http::sync::open_channel(ip, name, s); + }); + } + } } app.run();