rustyline-async/.patchy/0002-add-tab-completion-callback.patch
Jade Ellis cf87411086
All checks were successful
Patch Update / update-patchy (push) Successful in 12s
fix: Rebase patch
2025-06-21 00:46:40 +01:00

69 lines
1.7 KiB
Diff

From 7dc16744844c7be8525cb84c1bae0086e2f8051d Mon Sep 17 00:00:00 2001
From: Jason Volk <jason@zemos.net>
Date: Fri, 5 Jul 2024 00:38:13 +0000
Subject: [PATCH] add tab completion callback
Signed-off-by: Jason Volk <jason@zemos.net>
---
src/lib.rs | 7 +++++++
src/line.rs | 13 +++++++++++++
2 files changed, 20 insertions(+)
diff --git a/src/lib.rs b/src/lib.rs
index dedb45c..3bff93d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -318,6 +318,13 @@ impl Readline {
pub fn clear_history(&mut self) {
self.set_history_entries([]);
}
+
+ pub fn set_tab_completer<F>(&mut self, completer: F)
+ where
+ F: Fn(&str) -> String + Send + Sync + 'static,
+ {
+ let _ = self.line.completer.insert(Box::new(completer));
+ }
}
impl Drop for Readline {
diff --git a/src/line.rs b/src/line.rs
index 9d15ff3..a7d3c83 100644
--- a/src/line.rs
+++ b/src/line.rs
@@ -12,6 +12,8 @@ use unicode_width::UnicodeWidthStr;
use crate::{History, ReadlineError, ReadlineEvent};
+pub type Completer = dyn Fn(&str) -> String + Send + Sync;
+
#[derive(Default)]
pub struct LineState {
// Unicode Line
@@ -33,6 +35,8 @@ pub struct LineState {
term_size: (u16, u16),
pub history: History,
+
+ pub completer: Option<Box<Completer>>,
}
impl LineState {
@@ -404,6 +408,15 @@ impl LineState {
self.render(term)?;
}
}
+ KeyCode::Tab => {
+ // tab completion callback
+ if let Some(completer) = self.completer.as_ref() {
+ self.line = completer(&self.line);
+ self.clear(term)?;
+ self.move_cursor(100000)?;
+ self.render(term)?;
+ }
+ }
// Add character to line and output
KeyCode::Char(c) => {
self.clear(term)?;
--
2.39.5 (Apple Git-154)