diff --git a/src/ble.rs b/src/ble.rs index 9b54659..eba4425 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -1,5 +1,6 @@ use std::error::Error; use std::str; +use std::sync::mpsc::Receiver; use std::time::Duration; use btleplug::api::{ @@ -7,12 +8,11 @@ use btleplug::api::{ }; use btleplug::platform::{Adapter, Manager, Peripheral}; use console::Term; -use dialoguer::{theme::ColorfulTheme, Input}; use futures::stream::StreamExt; use tokio::time; use uuid::Uuid; -use crate::utils::progress_bar; +use crate::utils::{get_stdin_line_channel, progress_bar}; const NORDIC_UART_SERVICE_UUID: Uuid = Uuid::from_u128(0x6e400001_b5a3_f393_e0a9_e50e24dcca9e); const NORDIC_UART_TX_CHAR_UUID: Uuid = Uuid::from_u128(0x6e400002_b5a3_f393_e0a9_e50e24dcca9e); @@ -173,7 +173,11 @@ pub async fn repl(adapter_name: String, address: String) -> Result<(), Box Result<(), Box) { let chars = device.characteristics(); let tx_char = chars .iter() @@ -199,12 +205,13 @@ async fn get_input(device: Peripheral, t: Term) { .ok_or("Unable to find TX characteric") .unwrap(); loop { - let text: String = Input::with_theme(&ColorfulTheme::default()) - .with_prompt("Φ]") - .interact_on(&t) - .unwrap(); + let mut words = String::new(); + if let Ok(text) = text_channel.try_recv() { + words = text; + } + device - .write(&tx_char, text.as_bytes(), WriteType::WithoutResponse) + .write(&tx_char, words.as_bytes(), WriteType::WithoutResponse) .await .unwrap(); time::sleep(Duration::from_millis(100)).await; diff --git a/src/utils.rs b/src/utils.rs index 73916ef..86fac56 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,6 @@ +use std::io; +use std::sync::mpsc; +use std::sync::mpsc::Receiver; use std::thread; use std::time::Duration; @@ -33,3 +36,14 @@ pub fn progress_bar(scan_time: Duration) { rt.block_on(future); }); } + +/// Create a new thread that sends stdin data over a channel. +pub fn get_stdin_line_channel() -> Receiver { + let (tx, rx) = mpsc::channel::(); + thread::spawn(move || loop { + let mut line = String::new(); + io::stdin().read_line(&mut line).unwrap(); + tx.send(line).unwrap(); + }); + rx +}