1
0
Fork 0

Use console and dialoguer

This commit is contained in:
Daniele Tricoli 2023-03-03 05:17:04 +01:00
parent cbe3ab88e0
commit 7c5bbbcd05
3 changed files with 60 additions and 105 deletions

138
Cargo.lock generated
View file

@ -264,31 +264,6 @@ dependencies = [
"libc",
]
[[package]]
name = "crossterm"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67"
dependencies = [
"bitflags",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c"
dependencies = [
"winapi",
]
[[package]]
name = "dashmap"
version = "5.4.0"
@ -327,10 +302,16 @@ dependencies = [
]
[[package]]
name = "dyn-clone"
version = "1.0.11"
name = "dialoguer"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b0cf012f1230e43cd00ebb729c6bb58707ecfa8ad08b52ef3a4ccd2697fc30"
checksum = "af3c796f3b0b408d9fd581611b47fa850821fcb84aa640b83a3c1a5be2d691f2"
dependencies = [
"console",
"shell-words",
"tempfile",
"zeroize",
]
[[package]]
name = "either"
@ -378,6 +359,15 @@ dependencies = [
"libc",
]
[[package]]
name = "fastrand"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [
"instant",
]
[[package]]
name = "foreign-types"
version = "0.3.2"
@ -541,19 +531,12 @@ dependencies = [
]
[[package]]
name = "inquire"
version = "0.5.3"
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a94f0659efe59329832ba0452d3ec753145fc1fb12a8e1d60de4ccf99f5364"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"bitflags",
"crossterm",
"dyn-clone",
"lazy_static",
"newline-converter",
"thiserror",
"unicode-segmentation",
"unicode-width",
"cfg-if",
]
[[package]]
@ -674,9 +657,10 @@ version = "0.1.0"
dependencies = [
"btleplug",
"clap",
"console",
"dialoguer",
"futures",
"indicatif",
"inquire",
"pretty_env_logger",
"tokio",
"uuid",
@ -709,15 +693,6 @@ dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "newline-converter"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f71d09d5c87634207f894c6b31b6a2b2c64ea3bdcf71bd5599fdbbe1600c00f"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
@ -755,16 +730,6 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.7"
@ -948,34 +913,10 @@ dependencies = [
]
[[package]]
name = "signal-hook"
version = "0.3.15"
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "slab"
@ -1025,6 +966,19 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af18f7ae1acd354b992402e9ec5864359d693cd8a79dcbef59f76891701c1e95"
dependencies = [
"cfg-if",
"fastrand",
"redox_syscall",
"rustix",
"windows-sys 0.42.0",
]
[[package]]
name = "termcolor"
version = "1.2.0"
@ -1114,12 +1068,6 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.10"
@ -1287,3 +1235,9 @@ name = "xml-rs"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
[[package]]
name = "zeroize"
version = "1.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f"

View file

@ -9,9 +9,10 @@ edition = "2021"
[dependencies]
btleplug = "0.10.4"
clap = { version = "4.1.6", features = ["derive"] }
console = "0.15.5"
dialoguer = "0.10.3"
futures = "0.3.26"
indicatif = { version = "0.17.3", features = ["tokio"] }
inquire = "0.5.3"
pretty_env_logger = "0.4.0"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
uuid = "1.3.0"

View file

@ -6,9 +6,9 @@ use btleplug::api::{
BDAddr, Central, CentralEvent, Manager as _, Peripheral as _, ScanFilter, WriteType,
};
use btleplug::platform::{Adapter, Manager, Peripheral};
use console::Term;
use dialoguer::{theme::ColorfulTheme, Input};
use futures::stream::StreamExt;
use inquire::ui::{Color, RenderConfig, Styled};
use inquire::Text;
use tokio::time;
use uuid::Uuid;
@ -168,11 +168,12 @@ pub async fn send(
}
pub async fn repl(adapter_name: String, address: String) -> Result<(), Box<dyn Error>> {
let term = Term::buffered_stdout();
let device = find_device_by_address(adapter_name, address).await?;
device.connect().await?;
if device.is_connected().await? {
device.discover_services().await?;
tokio::spawn(get_input(device.clone()));
tokio::spawn(get_input(device.clone(), term.clone()));
let chars = device.characteristics();
let rx_char = chars
.iter()
@ -182,14 +183,15 @@ pub async fn repl(adapter_name: String, address: String) -> Result<(), Box<dyn E
let mut notification_stream = device.notifications().await?;
while let Some(data) = notification_stream.next().await {
let text = str::from_utf8(&data.value)?;
println!("{}", text.trim_end());
term.write_line(text.trim_end())?;
term.flush()?;
}
device.disconnect().await?;
}
Ok(())
}
async fn get_input(device: Peripheral) {
async fn get_input(device: Peripheral, t: Term) {
loop {
let chars = device.characteristics();
let tx_char = chars
@ -197,16 +199,14 @@ async fn get_input(device: Peripheral) {
.find(|c| c.uuid == NORDIC_UART_TX_CHAR_UUID)
.ok_or("Unable to find TX characteric")
.unwrap();
let prompt_prefix = Styled::new("Φ]").with_fg(Color::White);
let default: RenderConfig = RenderConfig::default();
let mine = default.with_prompt_prefix(prompt_prefix);
let text = Text::new("")
.with_render_config(mine)
.prompt()
.unwrap_or("".to_string());
let text: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Φ]")
.interact_on(&t)
.unwrap();
device
.write(&tx_char, text.as_bytes(), WriteType::WithoutResponse)
.await
.unwrap();
t.flush().unwrap();
}
}