diff --git a/Cargo.lock b/Cargo.lock index fd0d85a..21a429d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1847,6 +1847,9 @@ dependencies = [ [[package]] name = "insanity-core" version = "1.1.9" +dependencies = [ + "bon", +] [[package]] name = "insanity-tui-adapter" @@ -1854,6 +1857,7 @@ version = "1.1.9" dependencies = [ "built", "crossterm 0.23.2", + "insanity-core", "textwrap 0.15.2", "tokio", "tui", @@ -1878,6 +1882,7 @@ dependencies = [ "ecoji", "ed25519-dalek", "fern", + "insanity-core", "insanity-tui-adapter", "itertools", "log", diff --git a/Cargo.toml b/Cargo.toml index 6803ac7..9f2f6e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,3 @@ [workspace] members = ["insanity-core", "insanity-native-tui-app", "insanity-tui-adapter"] -default-members = ["insanity-native-tui-app"] resolver = "2" diff --git a/insanity-core/Cargo.toml b/insanity-core/Cargo.toml index bc6abe2..7b0b6f5 100644 --- a/insanity-core/Cargo.toml +++ b/insanity-core/Cargo.toml @@ -4,3 +4,4 @@ version = "1.1.9" edition = "2021" [dependencies] +bon = "2.1.1" diff --git a/insanity-core/src/lib.rs b/insanity-core/src/lib.rs index b93cf3f..56bf247 100644 --- a/insanity-core/src/lib.rs +++ b/insanity-core/src/lib.rs @@ -1,14 +1 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod user_input_event; diff --git a/insanity-core/src/user_input_event.rs b/insanity-core/src/user_input_event.rs new file mode 100644 index 0000000..461fd4a --- /dev/null +++ b/insanity-core/src/user_input_event.rs @@ -0,0 +1,10 @@ +#[derive(Debug, PartialEq, Eq)] +pub enum UserInputEvent { + DisablePeer(String), + EnablePeer(String), + DisableDenoise(String), + EnableDenoise(String), + SetVolume(String, usize), + SendMessage(String), + SetMuteSelf(bool), +} diff --git a/insanity-native-tui-app/Cargo.toml b/insanity-native-tui-app/Cargo.toml index 567a8bb..487632f 100644 --- a/insanity-native-tui-app/Cargo.toml +++ b/insanity-native-tui-app/Cargo.toml @@ -3,6 +3,7 @@ name = "insanity-tui-app" version = "1.1.9" authors = ["Nicolas Chan "] edition = "2018" +default-run = "insanity" [[bin]] name = "insanity" @@ -24,6 +25,7 @@ uuid = { version = "1.7", features = ["serde", "v4"] } dirs = "5.0.1" serde_json = "1.0.114" rubato = "0.10" +insanity-core = { path = "../insanity-core" } insanity-tui-adapter = { path = "../insanity-tui-adapter" } sha2 = "0.10.8" whoami = "1.5.1" diff --git a/insanity-native-tui-app/src/connection_manager.rs b/insanity-native-tui-app/src/connection_manager.rs index b4bc0b6..96a3ecd 100644 --- a/insanity-native-tui-app/src/connection_manager.rs +++ b/insanity-native-tui-app/src/connection_manager.rs @@ -8,7 +8,8 @@ use std::{ }, }; -use insanity_tui_adapter::{AppEvent, UserAction}; +use insanity_core::user_input_event::UserInputEvent; +use insanity_tui_adapter::AppEvent; use sha2::{Digest, Sha256}; use veq::{snow_types::SnowKeypair, veq::VeqSocket}; @@ -38,7 +39,7 @@ pub struct AugmentedInfo { pub struct ConnectionManager { socket: VeqSocket, cancellation_token: CancellationToken, - user_action_tx: mpsc::UnboundedSender, + user_action_tx: mpsc::UnboundedSender, } impl ConnectionManager { @@ -54,7 +55,7 @@ impl ConnectionManager { self.cancellation_token.cancel(); } - pub fn send_user_action(&self, action: UserAction) -> anyhow::Result<()> { + pub fn send_user_action(&self, action: UserInputEvent) -> anyhow::Result<()> { self.user_action_tx.send(action)?; Ok(()) } @@ -66,7 +67,7 @@ impl ConnectionManager { base_dir: PathBuf, display_name: Option, app_event_tx: Option>, - user_action_rx: mpsc::UnboundedReceiver, + user_action_rx: mpsc::UnboundedReceiver, ) -> anyhow::Result<()> { let connection_info = self.socket.connection_info(); log::debug!("Connection info: {:?}", connection_info); @@ -216,7 +217,7 @@ impl ConnectionManagerBuilder { fn manage_peers( socket: veq::veq::VeqSocket, app_event_tx: Option>, - mut user_action_rx: mpsc::UnboundedReceiver, + mut user_action_rx: mpsc::UnboundedReceiver, cancellation_token: CancellationToken, ) -> mpsc::UnboundedSender { // Channel for the manage_peers task to receive updated peers info. @@ -310,50 +311,50 @@ fn update_peer_info( } fn handle_user_action( - user_action: UserAction, + user_action: UserInputEvent, sender_is_muted: Arc, app_event_tx: Option>, managed_peers: &mut HashMap, ) -> anyhow::Result<()> { match user_action { - UserAction::DisableDenoise(id) => { + UserInputEvent::DisableDenoise(id) => { let id = uuid::Uuid::from_str(&id)?; if let Some(peer) = managed_peers.get(&id) { peer.set_denoise(false)?; } } - UserAction::EnableDenoise(id) => { + UserInputEvent::EnableDenoise(id) => { let id = uuid::Uuid::from_str(&id)?; if let Some(peer) = managed_peers.get(&id) { peer.set_denoise(true)?; } } - UserAction::DisablePeer(id) => { + UserInputEvent::DisablePeer(id) => { let id = uuid::Uuid::from_str(&id)?; if let Some(peer) = managed_peers.get(&id) { peer.disable()?; } } - UserAction::EnablePeer(id) => { + UserInputEvent::EnablePeer(id) => { let id = uuid::Uuid::from_str(&id)?; if let Some(peer) = managed_peers.get(&id) { peer.enable(); } } - UserAction::SetVolume(id, volume) => { + UserInputEvent::SetVolume(id, volume) => { let id = uuid::Uuid::from_str(&id)?; if let Some(peer) = managed_peers.get(&id) { peer.set_volume(volume)?; } } - UserAction::SendMessage(message) => { + UserInputEvent::SendMessage(message) => { for (_, peer) in managed_peers.iter() { if let Err(e) = peer.send_message(message.clone()) { log::debug!("Failed to send message to a peer: {:?}", e); } } } - UserAction::SetMuteSelf(is_muted) => { + UserInputEvent::SetMuteSelf(is_muted) => { sender_is_muted.store(is_muted, Ordering::Relaxed); if let Some(app_event_tx) = app_event_tx { if let Err(e) = app_event_tx.send(AppEvent::MuteSelf(is_muted)) { diff --git a/insanity-tui-adapter/Cargo.toml b/insanity-tui-adapter/Cargo.toml index 3ade740..1d36ed4 100644 --- a/insanity-tui-adapter/Cargo.toml +++ b/insanity-tui-adapter/Cargo.toml @@ -14,6 +14,7 @@ path = "src/main.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +insanity-core = { path = "../insanity-core" } crossterm = "0.23" tokio = { version = "1.16.1", features = [ "sync", diff --git a/insanity-tui-adapter/src/lib.rs b/insanity-tui-adapter/src/lib.rs index b720d2b..6d708d9 100644 --- a/insanity-tui-adapter/src/lib.rs +++ b/insanity-tui-adapter/src/lib.rs @@ -3,6 +3,7 @@ use crossterm::{ execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; +use insanity_core::user_input_event::UserInputEvent; use std::collections::BTreeMap; use std::{error::Error, io, io::Stdout}; use tokio::{ @@ -117,19 +118,8 @@ pub enum AppEvent { MuteSelf(bool), } -#[derive(Debug, PartialEq, Eq)] -pub enum UserAction { - DisablePeer(String), - EnablePeer(String), - DisableDenoise(String), - EnableDenoise(String), - SetVolume(String, usize), - SendMessage(String), - SetMuteSelf(bool), -} - pub struct App { - pub user_action_sender: UnboundedSender, + pub user_action_sender: UnboundedSender, pub tabs: [&'static str; NUM_TABS], pub tab_index: usize, pub killed: bool, @@ -148,7 +138,7 @@ pub struct App { } impl App { - pub fn new(sender: UnboundedSender) -> App { + pub fn new(sender: UnboundedSender) -> App { App { user_action_sender: sender, tabs: TAB_NAMES, @@ -331,11 +321,11 @@ impl App { if let Some(peer) = self.selected_peer() { if peer.state == PeerState::Disabled { self.user_action_sender - .send(UserAction::EnablePeer(peer.id.clone())) + .send(UserInputEvent::EnablePeer(peer.id.clone())) .unwrap(); } else { self.user_action_sender - .send(UserAction::DisablePeer(peer.id.clone())) + .send(UserInputEvent::DisablePeer(peer.id.clone())) .unwrap(); } } @@ -357,7 +347,7 @@ impl App { let own_address = self.own_public_key.clone().unwrap_or(default); self.add_message((own_address, message.clone())); self.user_action_sender - .send(UserAction::SendMessage(message)) + .send(UserInputEvent::SendMessage(message)) .unwrap(); } } @@ -366,11 +356,11 @@ impl App { if let Some(peer) = self.selected_peer() { if peer.denoised { self.user_action_sender - .send(UserAction::DisableDenoise(peer.id.clone())) + .send(UserInputEvent::DisableDenoise(peer.id.clone())) .unwrap(); } else { self.user_action_sender - .send(UserAction::EnableDenoise(peer.id.clone())) + .send(UserInputEvent::EnableDenoise(peer.id.clone())) .unwrap(); } } @@ -379,7 +369,7 @@ impl App { fn adjust_volume(&mut self, delta: isize) { if let Some(peer) = self.selected_peer() { self.user_action_sender - .send(UserAction::SetVolume( + .send(UserInputEvent::SetVolume( peer.id.clone(), add_in_bounds(peer.volume, 0, 999, delta), )) @@ -399,7 +389,7 @@ impl App { fn toggle_mute_self(&mut self) { self.mute_self = !self.mute_self; self.user_action_sender - .send(UserAction::SetMuteSelf(self.mute_self)) + .send(UserInputEvent::SetMuteSelf(self.mute_self)) .unwrap(); } @@ -506,7 +496,7 @@ pub async fn handle_input(sender: UnboundedSender) -> JoinHandle<()> { pub async fn start_tui() -> Result< ( UnboundedSender, - UnboundedReceiver, + UnboundedReceiver, JoinHandle>>, ), Box, diff --git a/insanity-tui-adapter/src/main.rs b/insanity-tui-adapter/src/main.rs index cb515c4..cfab30b 100644 --- a/insanity-tui-adapter/src/main.rs +++ b/insanity-tui-adapter/src/main.rs @@ -1,4 +1,5 @@ -use insanity_tui_adapter::{start_tui, stop_tui, AppEvent, Peer, PeerState, UserAction}; +use insanity_core::user_input_event::UserInputEvent; +use insanity_tui_adapter::{start_tui, stop_tui, AppEvent, Peer, PeerState}; use std::{collections::BTreeMap, error::Error}; #[tokio::main] @@ -47,17 +48,17 @@ async fn main() -> Result<(), Box> { tokio::spawn(async move { while let Some(event) = user_action_receiver.recv().await { match event { - UserAction::EnableDenoise(peer_id) => { + UserInputEvent::EnableDenoise(peer_id) => { sender .send(AppEvent::SetPeerDenoise(peer_id, true)) .unwrap(); } - UserAction::DisableDenoise(peer_id) => { + UserInputEvent::DisableDenoise(peer_id) => { sender .send(AppEvent::SetPeerDenoise(peer_id, false)) .unwrap(); } - UserAction::DisablePeer(peer_id) => { + UserInputEvent::DisablePeer(peer_id) => { sender .send(AppEvent::AddPeer( peers @@ -68,7 +69,7 @@ async fn main() -> Result<(), Box> { )) .unwrap(); } - UserAction::EnablePeer(peer_id) => { + UserInputEvent::EnablePeer(peer_id) => { sender .send(AppEvent::AddPeer( peers @@ -79,13 +80,13 @@ async fn main() -> Result<(), Box> { )) .unwrap(); } - UserAction::SetVolume(peer_id, volume) => { + UserInputEvent::SetVolume(peer_id, volume) => { sender .send(AppEvent::SetPeerVolume(peer_id, volume)) .unwrap(); } - UserAction::SendMessage(_message) => {} - UserAction::SetMuteSelf(_) => todo!(), + UserInputEvent::SendMessage(_message) => {} + UserInputEvent::SetMuteSelf(_) => todo!(), } } });