add basic tab completion to console

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-05 07:52:05 +00:00
parent b31e81a469
commit 5254eb4f72
4 changed files with 134 additions and 52 deletions

View file

@ -118,6 +118,7 @@ impl Console {
let _suppression = log::Suppress::new(&services().server);
let (mut readline, _writer) = Readline::new(PROMPT.to_owned())?;
readline.set_tab_completer(Self::tab_complete);
self.set_history(&mut readline);
let future = readline.readline();
@ -185,6 +186,13 @@ impl Console {
history.push_front(line);
history.truncate(HISTORY_LIMIT);
}
fn tab_complete(line: &str) -> String {
services()
.admin
.complete_command(line)
.unwrap_or_else(|| line.to_owned())
}
}
fn configure_output(mut output: MadSkin) -> MadSkin {

View file

@ -2,7 +2,11 @@ pub mod console;
mod create;
mod grant;
use std::{future::Future, pin::Pin, sync::Arc};
use std::{
future::Future,
pin::Pin,
sync::{Arc, RwLock as StdRwLock},
};
use async_trait::async_trait;
use conduit::{error, utils::mutex_map, Error, Result};
@ -27,12 +31,14 @@ pub type CommandOutput = Option<RoomMessageEventContent>;
pub type CommandResult = Result<CommandOutput, Error>;
pub type HandlerResult = Pin<Box<dyn Future<Output = CommandResult> + Send>>;
pub type Handler = fn(Command) -> HandlerResult;
pub type Completer = fn(&str) -> String;
pub struct Service {
sender: Sender<Command>,
receiver: Mutex<Receiver<Command>>,
handler_join: Mutex<Option<JoinHandle<()>>>,
pub handle: Mutex<Option<Handler>>,
pub complete: StdRwLock<Option<Completer>>,
#[cfg(feature = "console")]
pub console: Arc<console::Console>,
}
@ -52,6 +58,7 @@ impl crate::Service for Service {
receiver: Mutex::new(receiver),
handler_join: Mutex::new(None),
handle: Mutex::new(None),
complete: StdRwLock::new(None),
#[cfg(feature = "console")]
console: console::Console::new(),
}))
@ -127,6 +134,13 @@ impl Service {
.await
}
pub fn complete_command(&self, command: &str) -> Option<String> {
self.complete
.read()
.expect("locked for reading")
.map(|complete| complete(command))
}
async fn send(&self, message: Command) {
debug_assert!(!self.sender.is_closed(), "channel closed");
self.sender.send_async(message).await.expect("message sent");