Add stdin command prompt
This commit is contained in:
parent
3333f1a48c
commit
2b325960ba
109
src/main.rs
109
src/main.rs
@ -7,10 +7,11 @@ use matrix_sdk::{
|
||||
},
|
||||
},
|
||||
OwnedRoomId,
|
||||
RoomId
|
||||
},
|
||||
Error, LoopCtrl,
|
||||
Error, LoopCtrl, Client,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::{path::PathBuf, io::{self,BufRead}, sync::Mutex};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
pub mod config;
|
||||
@ -157,6 +158,102 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> any
|
||||
Ok(())
|
||||
}
|
||||
|
||||
static mut SHOULD_STOP: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
|
||||
async fn prompt(client: Client) -> anyhow::Result<()> {
|
||||
loop {
|
||||
use std::io::Write;
|
||||
let mut stream = match std::fs::OpenOptions::new().write(true).open("/dev/tty") {
|
||||
Ok(s) => s,
|
||||
Err(e) => { eprintln!("prompt: {e}"); break; }
|
||||
};
|
||||
stream
|
||||
.write_all("> ".as_bytes())
|
||||
.and_then(|_| stream.flush())?;
|
||||
|
||||
if let Ok(should_stop) = unsafe { SHOULD_STOP.try_lock() } {
|
||||
if should_stop.to_owned() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut buffer = String::new();
|
||||
io::stdin().lock().read_line(&mut buffer)?;
|
||||
|
||||
if buffer.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let args: Vec<&str> = buffer.split_whitespace().collect();
|
||||
if let Some(cmd) = args.get(0) {
|
||||
match cmd.as_ref() {
|
||||
"help" => {
|
||||
println!("help; stop; join room_id; leave room_id");
|
||||
},
|
||||
"stop" => {
|
||||
let should_stop = unsafe { SHOULD_STOP.get_mut() };
|
||||
if let Ok(should_stop) = should_stop {
|
||||
*should_stop = true;
|
||||
eprintln!("Stopping...");
|
||||
}
|
||||
},
|
||||
"join" => {
|
||||
let room = args.get(1);
|
||||
if room.is_none() {
|
||||
eprintln!("join: No room_id given.\nSyntax: join room_id");
|
||||
continue;
|
||||
}
|
||||
|
||||
let id = RoomId::parse(room.unwrap().to_string());
|
||||
if let Err(e) = id {
|
||||
eprintln!("join: {e}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Err(e) = client.join_room_by_id(&id.unwrap()).await {
|
||||
if e.to_string().contains("No known servers") {
|
||||
eprintln!("join: Not a public room and not invited");
|
||||
} else {
|
||||
eprintln!("join: {e}");
|
||||
}
|
||||
} else {
|
||||
println!("join: Joined room!");
|
||||
}
|
||||
},
|
||||
"leave" => {
|
||||
let room = args.get(1);
|
||||
if room.is_none() {
|
||||
eprintln!("leave: no room_id given.\nSyntax: leave room_id");
|
||||
continue;
|
||||
}
|
||||
|
||||
let id = RoomId::parse(room.unwrap().to_string());
|
||||
if let Err(e) = id {
|
||||
eprintln!("leave: {e}");
|
||||
continue;
|
||||
}
|
||||
|
||||
let room = client.get_joined_room(&id.unwrap());
|
||||
if room.is_none() {
|
||||
eprintln!("leave: Not in room");
|
||||
continue;
|
||||
}
|
||||
|
||||
let room = room.unwrap();
|
||||
if let Err(e) = room.leave().await {
|
||||
eprintln!("leave: {e}");
|
||||
} else {
|
||||
println!("leave: Left room!");
|
||||
}
|
||||
}
|
||||
_ => { eprintln!("Unknown command \"{cmd}\""); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
@ -183,8 +280,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
};
|
||||
|
||||
let _stdin_thread = tokio::spawn(prompt(client.to_owned()));
|
||||
client.add_event_handler(on_room_message);
|
||||
|
||||
client
|
||||
.sync_with_result_callback(sync_settings, |sync_result| async move {
|
||||
let response = sync_result?;
|
||||
@ -193,6 +290,12 @@ async fn main() -> anyhow::Result<()> {
|
||||
.await
|
||||
.map_err(|err| Error::UnknownError(err.into()))?;
|
||||
|
||||
if let Ok(should_stop) = unsafe { SHOULD_STOP.try_lock() } {
|
||||
if should_stop.to_owned() {
|
||||
return Ok(LoopCtrl::Break)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(LoopCtrl::Continue)
|
||||
})
|
||||
.await?;
|
||||
|
Loading…
Reference in New Issue
Block a user