diff --git a/src/main.rs b/src/main.rs index 36fbf7b..82db5da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{collections::BTreeMap, io, net::SocketAddr}; use joecalsend::{Config, JoecalState, RunningState, error, models::Device}; use local_ip_address::local_ip; @@ -14,6 +14,8 @@ use ratatui::{ widgets::{Block, Paragraph, Widget}, }; +mod ui; + #[tokio::main] async fn main() -> error::Result<()> { let device = Device::default(); @@ -60,6 +62,8 @@ struct App { state: JoecalState, rstate: RunningState, screen: CurrentScreen, + // addr to (alias, fingerprint) + peers: BTreeMap, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -75,6 +79,7 @@ impl App { state: state.clone(), rstate: *state.running_state.lock().await, screen: CurrentScreen::Main, + peers: Default::default(), } } @@ -87,6 +92,14 @@ impl App { if *rstate == RunningState::Stopping { break; } + let peers = self.state.peers.lock().await; + peers.iter().for_each(|(k, v)| { + // k is fingerprint, v is addr, device + let addr = v.0; + let alias = v.1.alias.clone(); + let fingerprint = k.clone(); + self.peers.insert(addr, (alias, fingerprint)); + }); } Ok(()) } diff --git a/src/models.rs b/src/models.rs index f08e60f..4b3db83 100644 --- a/src/models.rs +++ b/src/models.rs @@ -103,7 +103,7 @@ pub enum Protocol { impl Default for Device { fn default() -> Self { Self { - alias: "RustSend".to_string(), + alias: "Joecalsend".to_string(), version: "2.1".to_string(), device_model: None, device_type: Some(DeviceType::Headless), diff --git a/src/ui/mod.rs b/src/ui/mod.rs new file mode 100644 index 0000000..7f42f29 --- /dev/null +++ b/src/ui/mod.rs @@ -0,0 +1,25 @@ +use ratatui::layout::{Constraint, Direction, Layout, Rect}; + +// helper function to create a centered rect using up certain percentage of the +// available rect `r` +fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { + // Cut the given rectangle into three vertical pieces + let popup_layout = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Percentage((100 - percent_y) / 2), + Constraint::Percentage(percent_y), + Constraint::Percentage((100 - percent_y) / 2), + ]) + .split(r); + + // Then cut the middle vertical piece into three width-wise pieces + Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage((100 - percent_x) / 2), + Constraint::Percentage(percent_x), + Constraint::Percentage((100 - percent_x) / 2), + ]) + .split(popup_layout[1])[1] // Return the middle chunk +}