switch to crate rustyline_async

improve console signal and interrupt stack

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-16 19:42:16 +00:00
parent 959fd2e6c4
commit 5aee03d14a
5 changed files with 137 additions and 115 deletions

77
Cargo.lock generated
View file

@ -510,12 +510,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]] [[package]]
name = "cfg_aliases" name = "cfg_aliases"
version = "0.2.1" version = "0.2.1"
@ -580,15 +574,6 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70"
[[package]]
name = "clipboard-win"
version = "5.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79f4473f5144e20d9aceaf2972478f06ddf687831eafeeb434fbaf0acc4144ad"
dependencies = [
"error-code",
]
[[package]] [[package]]
name = "color_quant" name = "color_quant"
version = "1.1.0" version = "1.1.0"
@ -696,7 +681,7 @@ dependencies = [
"itertools 0.13.0", "itertools 0.13.0",
"libloading", "libloading",
"log", "log",
"nix 0.29.0", "nix",
"parking_lot", "parking_lot",
"rand", "rand",
"regex", "regex",
@ -796,7 +781,7 @@ dependencies = [
"reqwest", "reqwest",
"ruma", "ruma",
"ruma-identifiers-validation", "ruma-identifiers-validation",
"rustyline", "rustyline-async",
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
@ -990,6 +975,7 @@ checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"crossterm_winapi", "crossterm_winapi",
"futures-core",
"libc", "libc",
"mio", "mio",
"parking_lot", "parking_lot",
@ -1166,12 +1152,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "error-code"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0474425d51df81997e2f90a21591180b38eccf27292d755f3e30750225c175b"
[[package]] [[package]]
name = "fallible-iterator" name = "fallible-iterator"
version = "0.3.0" version = "0.3.0"
@ -2341,18 +2321,6 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nix"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"cfg_aliases 0.1.1",
"libc",
]
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.29.0" version = "0.29.0"
@ -2361,7 +2329,7 @@ checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"cfg-if", "cfg-if",
"cfg_aliases 0.2.1", "cfg_aliases",
"libc", "libc",
] ]
@ -3420,22 +3388,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]] [[package]]
name = "rustyline" name = "rustyline-async"
version = "14.0.0" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7803e8936da37efd9b6d4478277f4b2b9bb5cdb37a113e8d63222e58da647e63" checksum = "8b6eb06391513b2184f0a5405c11a4a0a5302e8be442f4c5c35267187c2b37d5"
dependencies = [ dependencies = [
"bitflags 2.5.0", "crossterm",
"cfg-if", "futures-channel",
"clipboard-win", "futures-util",
"libc", "pin-project",
"log", "thingbuf",
"memchr", "thiserror",
"nix 0.28.0",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
"utf8parse",
"windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -4024,6 +3989,16 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "thingbuf"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "662b54ef6f7b4e71f683dadc787bbb2d8e8ef2f91b682ebed3164a5a7abca905"
dependencies = [
"parking_lot",
"pin-project",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.61" version = "1.0.61"
@ -4635,12 +4610,6 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.8.0" version = "1.8.0"

View file

@ -415,9 +415,8 @@ features = [
"light", "light",
] ]
[workspace.dependencies.rustyline] [workspace.dependencies.rustyline-async]
version = "14.0.0" version = "0.4.2"
default-features = false
[workspace.dependencies.termimad] [workspace.dependencies.termimad]
version = "0.29.3" version = "0.29.3"

View file

@ -21,7 +21,7 @@ brotli_compression = [
"reqwest/brotli", "reqwest/brotli",
] ]
console = [ console = [
"dep:rustyline", "dep:rustyline-async",
"dep:termimad", "dep:termimad",
] ]
dev_release_log_level = [] dev_release_log_level = []
@ -61,8 +61,8 @@ regex.workspace = true
reqwest.workspace = true reqwest.workspace = true
ruma-identifiers-validation.workspace = true ruma-identifiers-validation.workspace = true
ruma.workspace = true ruma.workspace = true
rustyline.workspace = true rustyline-async.workspace = true
rustyline.optional = true rustyline-async.optional = true
serde_json.workspace = true serde_json.workspace = true
serde.workspace = true serde.workspace = true
serde_yaml.workspace = true serde_yaml.workspace = true

View file

@ -1,44 +1,35 @@
#![cfg(feature = "console")] #![cfg(feature = "console")]
use std::sync::Arc; use std::{
collections::VecDeque,
sync::{Arc, Mutex},
};
use conduit::{debug, defer, error, log, trace}; use conduit::{debug, defer, error, log};
use futures_util::future::{AbortHandle, Abortable}; use futures_util::future::{AbortHandle, Abortable};
use ruma::events::room::message::RoomMessageEventContent; use ruma::events::room::message::RoomMessageEventContent;
use rustyline::{error::ReadlineError, history, Editor}; use rustyline_async::{Readline, ReadlineError, ReadlineEvent};
use termimad::MadSkin; use termimad::MadSkin;
use tokio::{sync::Mutex, task::JoinHandle}; use tokio::task::JoinHandle;
use crate::services; use crate::services;
pub struct Console { pub struct Console {
join: Mutex<Option<JoinHandle<()>>>, worker_join: Mutex<Option<JoinHandle<()>>>,
input: Mutex<Editor<(), history::MemHistory>>, input_abort: Mutex<Option<AbortHandle>>,
abort: std::sync::Mutex<Option<AbortHandle>>, command_abort: Mutex<Option<AbortHandle>>,
history: Mutex<VecDeque<String>>,
output: MadSkin, output: MadSkin,
} }
const PROMPT: &str = "uwu> ";
const HISTORY_LIMIT: usize = 48;
impl Console { impl Console {
#[must_use] #[must_use]
pub fn new() -> Arc<Self> { pub fn new() -> Arc<Self> {
use rustyline::config::{Behavior, BellStyle};
use termimad::{crossterm::style::Color, Alignment, CompoundStyle, LineStyle}; use termimad::{crossterm::style::Color, Alignment, CompoundStyle, LineStyle};
let config = rustyline::Config::builder()
.enable_signals(false)
.behavior(Behavior::PreferTerm)
.bell_style(BellStyle::Visible)
.auto_add_history(true)
.max_history_size(100)
.expect("valid history size")
.indent_size(4)
.tab_stop(4)
.build();
let history = history::MemHistory::with_config(config);
let input = Editor::with_history(config, history).expect("builder configuration succeeded");
let mut output = MadSkin::default_dark(); let mut output = MadSkin::default_dark();
let code_style = CompoundStyle::with_fgbg(Color::AnsiValue(40), Color::AnsiValue(234)); let code_style = CompoundStyle::with_fgbg(Color::AnsiValue(40), Color::AnsiValue(234));
output.inline_code = code_style.clone(); output.inline_code = code_style.clone();
output.code_block = LineStyle { output.code_block = LineStyle {
@ -49,33 +40,63 @@ impl Console {
}; };
Arc::new(Self { Arc::new(Self {
join: None.into(), worker_join: None.into(),
input: Mutex::new(input), input_abort: None.into(),
abort: None.into(), command_abort: None.into(),
history: VecDeque::with_capacity(HISTORY_LIMIT).into(),
output, output,
}) })
} }
pub(super) async fn handle_signal(self: &Arc<Self>, sig: &'static str) {
if !services().server.running() {
self.interrupt();
} else if sig == "SIGINT" {
self.interrupt_command();
self.start().await;
}
}
#[allow(clippy::let_underscore_must_use)] #[allow(clippy::let_underscore_must_use)]
pub async fn start(self: &Arc<Self>) { pub async fn start(self: &Arc<Self>) {
let mut join = self.join.lock().await; let mut worker_join = self.worker_join.lock().expect("locked");
if join.is_none() { if worker_join.is_none() {
let self_ = Arc::clone(self); let self_ = Arc::clone(self);
_ = join.insert(services().server.runtime().spawn(self_.worker())); _ = worker_join.insert(services().server.runtime().spawn(self_.worker()));
} }
} }
#[allow(clippy::let_underscore_must_use)] #[allow(clippy::let_underscore_must_use)]
pub async fn close(self: &Arc<Self>) { pub async fn close(self: &Arc<Self>) {
if let Some(join) = self.join.lock().await.take() { self.interrupt();
_ = join.await; let Some(worker_join) = self.worker_join.lock().expect("locked").take() else {
} return;
};
_ = worker_join.await;
} }
pub fn interrupt(self: &Arc<Self>) { pub fn interrupt(self: &Arc<Self>) {
if let Some(abort) = self.abort.lock().expect("locked").take() { self.interrupt_command();
self.interrupt_readline();
self.worker_join
.lock()
.expect("locked")
.as_ref()
.map(JoinHandle::abort);
}
pub fn interrupt_readline(self: &Arc<Self>) {
if let Some(input_abort) = self.input_abort.lock().expect("locked").take() {
debug!("Interrupting console readline...");
input_abort.abort();
}
}
pub fn interrupt_command(self: &Arc<Self>) {
if let Some(command_abort) = self.command_abort.lock().expect("locked").take() {
debug!("Interrupting console command..."); debug!("Interrupting console command...");
abort.abort(); command_abort.abort();
} }
} }
@ -83,25 +104,48 @@ impl Console {
async fn worker(self: Arc<Self>) { async fn worker(self: Arc<Self>) {
debug!("session starting"); debug!("session starting");
while services().server.running() { while services().server.running() {
let mut input = self.input.lock().await; match self.readline().await {
Ok(event) => match event {
let suppression = log::Suppress::new(&services().server); ReadlineEvent::Line(string) => self.clone().handle(string).await,
let line = tokio::task::block_in_place(|| input.readline("uwu> ")); ReadlineEvent::Interrupted => continue,
drop(suppression); ReadlineEvent::Eof => break,
},
trace!(?line, "input"); Err(error) => match error {
match line { ReadlineError::Closed => break,
Ok(string) => self.clone().handle(string).await, ReadlineError::IO(error) => {
Err(e) => match e { error!("console I/O: {error:?}");
ReadlineError::Interrupted | ReadlineError::Eof => break, break;
ReadlineError::WindowResized => continue, },
_ => error!("console: {e:?}"),
}, },
} }
} }
debug!("session ending"); debug!("session ending");
self.join.lock().await.take(); self.worker_join.lock().expect("locked").take();
}
#[allow(clippy::let_underscore_must_use)]
async fn readline(self: &Arc<Self>) -> Result<ReadlineEvent, ReadlineError> {
let _suppression = log::Suppress::new(&services().server);
let (mut readline, _writer) = Readline::new(PROMPT.to_owned())?;
self.set_history(&mut readline);
let future = readline.readline();
let (abort, abort_reg) = AbortHandle::new_pair();
let future = Abortable::new(future, abort_reg);
_ = self.input_abort.lock().expect("locked").insert(abort);
defer! {{
_ = self.input_abort.lock().expect("locked").take();
}}
let Ok(result) = future.await else {
return Ok(ReadlineEvent::Eof);
};
readline.flush()?;
result
} }
#[allow(clippy::let_underscore_must_use)] #[allow(clippy::let_underscore_must_use)]
@ -110,12 +154,13 @@ impl Console {
return; return;
} }
self.add_history(line.clone());
let future = self.clone().process(line); let future = self.clone().process(line);
let (abort, abort_reg) = AbortHandle::new_pair(); let (abort, abort_reg) = AbortHandle::new_pair();
let future = Abortable::new(future, abort_reg); let future = Abortable::new(future, abort_reg);
_ = self.abort.lock().expect("locked").insert(abort); _ = self.command_abort.lock().expect("locked").insert(abort);
defer! {{ defer! {{
_ = self.abort.lock().expect("locked").take(); _ = self.command_abort.lock().expect("locked").take();
}} }}
_ = future.await; _ = future.await;
@ -133,4 +178,17 @@ impl Console {
let output = self.output.term_text(output_content.body()); let output = self.output.term_text(output_content.body());
println!("{output}"); println!("{output}");
} }
fn set_history(&self, readline: &mut Readline) {
let history = self.history.lock().expect("locked");
for entry in history.iter().rev() {
readline.add_history_entry(entry.clone());
}
}
fn add_history(&self, line: String) {
let mut history = self.history.lock().expect("locked");
history.push_front(line);
history.truncate(HISTORY_LIMIT);
}
} }

View file

@ -130,7 +130,6 @@ impl Service {
let receiver = self.receiver.lock().await; let receiver = self.receiver.lock().await;
let mut signals = services().server.signal.subscribe(); let mut signals = services().server.signal.subscribe();
loop { loop {
debug_assert!(!receiver.is_closed(), "channel closed");
tokio::select! { tokio::select! {
command = receiver.recv_async() => match command { command = receiver.recv_async() => match command {
Ok(command) => self.handle_command(command).await, Ok(command) => self.handle_command(command).await,
@ -146,10 +145,7 @@ impl Service {
async fn handle_signal(&self, #[allow(unused_variables)] sig: &'static str) { async fn handle_signal(&self, #[allow(unused_variables)] sig: &'static str) {
#[cfg(feature = "console")] #[cfg(feature = "console")]
if sig == "SIGINT" && services().server.running() { self.console.handle_signal(sig).await;
self.console.interrupt();
self.console.start().await;
}
} }
async fn handle_command(&self, command: Command) { async fn handle_command(&self, command: Command) {