aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/sequence/src/ui/fuzzy_complete.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2025-12-11 14:25:42 +0100
committerGitHub <noreply@github.com>2025-12-11 14:25:42 +0100
commit693fa082def5ef0fe030cd7fdc3b1df051b35dc6 (patch)
treeb3f651462bbc1cd2fa5b2d79207343413ef6b83c /default-plugins/sequence/src/ui/fuzzy_complete.rs
parentfix(zellij-server): unblock building on 32bit targets (#4484) (diff)
downloadsl0ppij-693fa082def5ef0fe030cd7fdc3b1df051b35dc6.tar.gz
sl0ppij-693fa082def5ef0fe030cd7fdc3b1df051b35dc6.tar.xz
sl0ppij-693fa082def5ef0fe030cd7fdc3b1df051b35dc6.zip
Command Sequences, Blocking CLI Commands, New Plugin APIs (#4546)
* fix deadlocks * initial logging to plugin implementation * only log actions, include keys as Action::Write * send actions to plugins as a new Event::UserAction * add Action::Write metadata to plugin API * run_action plugin command and permission * block-until-exit/success/failure in the cli * fix tests * add NewBlockingPane to the plugin API * run actions in a different thread so as not to block plugin execution * add custom context to run_action flow * fixes for floating pane z_index sorting and unknown protobuf events not crashing plugins * always render the frame of pinned panes * macros module * add macros to config * KDL macro parsing * thread macros to the plugin thread * get/update/delete macros from plugins * add macros updated event * add ability to rename macros * fix deadlock * fix z_indices with pinned unselectable panes bug * allow plugins to opt-in to show_cursor * change show_cursor(bool) to show_cursor(Option<coordinates>) * test * upgrade strum to allow proper enum iteration and case insensitive fromstr implementation for Actions * better cwd tracking for exited command panes * add initial panes to NewTab Action (no-op for now) * add initial panes to cli newtab action * make initial panes work * add close_on_exit and start_suspended to the cli * properly convert newtab action (potentially breaking for plugins) * allow first pane in tab to block * make distinction between opening panes with the last active client and the current pane in the cli * improve some apis * fix fixed size closing pane bug * fix plugin cursor coordinates * fix issues with launch-or-focus-plugin having to do with in-place and suppressed panes * fix: set affected pane in replace pane * allow setting a pane in place when it is suppressed and being focused (tests failing) * fix tests * some fixes for unsuppression of panes * some fixes for completion_tx in new tab action * allow replace/suppressed rather than replace/close only * fixes for inserting suppressed panes without conflicts * pane focus history and explicit unsuppress plugin api * fix: prefer explicitly provided pane id in placement in actions that provide it * fix floating pane coordinate change and z_index * fix plugin cache dir to persist between sessions * fix some stack focusing issues when adding panes without a client id * make sure pinned panes are always above non-pinned floating panes * allow plugins to copy to clipboard * initial text styling additions implementations - buggy * fix dimming issues * remove extras, stay with just dim and unbold * unbold and dim as indices * allow ui components to render success/error colors * allow plugins to send sigint/sigkill to processes, as well as receive their pid * ui component fixes and send originating plugin context with run_action * originating plugin for new tab too * make plugn show pane command also expand stacked panes * add sequence plugin * remove macros * remove snapshots * add shortcut for sequence plugin * fix plugin tests * plugin tests for new apis * update snapshots for sequence shortcut * cleanups and formatting * Event::CwdChanged * moar cleanups * remove some warnings * remove mroe warnings * remove dead code * remove more dead code * fix minor bugs in sequence plugin * update sequence help and format codebase * fix conditional blocking cli commands * fix initial-command new-tab cli flag * add client_ids to CwdChanged Event * last fixes * add pr
Diffstat (limited to 'default-plugins/sequence/src/ui/fuzzy_complete.rs')
-rw-r--r--default-plugins/sequence/src/ui/fuzzy_complete.rs321
1 files changed, 321 insertions, 0 deletions
diff --git a/default-plugins/sequence/src/ui/fuzzy_complete.rs b/default-plugins/sequence/src/ui/fuzzy_complete.rs
new file mode 100644
index 00000000..0feae7e4
--- /dev/null
+++ b/default-plugins/sequence/src/ui/fuzzy_complete.rs
@@ -0,0 +1,321 @@
+use fuzzy_matcher::skim::SkimMatcherV2;
+use fuzzy_matcher::FuzzyMatcher;
+use std::collections::BTreeMap;
+use std::fs;
+use std::path::PathBuf;
+
+/// Result of a fuzzy completion operation
+#[derive(Debug, Clone)]
+pub struct CompletionResult {
+ pub completed_text: String,
+ pub score: i64,
+ pub is_directory: bool,
+ pub is_prefix_completion: bool, // True if this is the shortest of multiple matches with same prefix
+}
+
+/// Type of completion
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum CompletionType {
+ Command,
+ Path,
+}
+
+/// Fuzzy match executables (commands) from the PATH
+pub fn fuzzy_complete_command(
+ query: &str,
+ executables: &BTreeMap<String, PathBuf>,
+) -> Option<CompletionResult> {
+ if query.is_empty() {
+ return None;
+ }
+
+ let matcher = SkimMatcherV2::default().ignore_case();
+
+ // Collect all matches with their scores
+ let mut all_matches: Vec<(String, i64)> = Vec::new();
+
+ for executable_name in executables.keys() {
+ if let Some((score, _indices)) = matcher.fuzzy_indices(executable_name, query) {
+ all_matches.push((executable_name.clone(), score));
+ }
+ }
+
+ if all_matches.is_empty() {
+ return None;
+ }
+
+ // Sort by score (highest first), then by length (shortest first)
+ all_matches.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.len().cmp(&b.0.len())));
+
+ // Get the best match
+ let (best_name, best_score) = &all_matches[0];
+
+ // Check if there are multiple matches that share the same prefix
+ let is_prefix_completion = check_for_prefix_matches(&all_matches, best_name);
+
+ Some(CompletionResult {
+ completed_text: best_name.clone(),
+ score: *best_score,
+ is_directory: false, // Commands are not directories
+ is_prefix_completion,
+ })
+}
+
+/// Fuzzy match paths (directories and files) relative to the current working directory
+pub fn fuzzy_complete_path(query: &str, cwd: Option<&PathBuf>) -> Option<CompletionResult> {
+ if query.is_empty() {
+ return None;
+ }
+
+ let matcher = SkimMatcherV2::default().ignore_case();
+
+ // Determine the base directory to search in
+ let (base_dir, search_prefix) = if query.starts_with('/') {
+ // Absolute path
+ let parts: Vec<&str> = query.rsplitn(2, '/').collect();
+ if parts.len() == 2 {
+ // Has a directory component
+ let dir = parts[1];
+ let prefix = parts[0];
+ (PathBuf::from(dir), prefix.to_string())
+ } else {
+ // Just "/"
+ (PathBuf::from("/"), String::new())
+ }
+ } else if query.starts_with("~/") {
+ // Home directory
+ let home_dir = std::env::var("HOME").ok()?;
+ let query_without_tilde = &query[2..];
+ let parts: Vec<&str> = query_without_tilde.rsplitn(2, '/').collect();
+ if parts.len() == 2 {
+ let dir = PathBuf::from(&home_dir).join(parts[1]);
+ let prefix = parts[0];
+ (dir, prefix.to_string())
+ } else {
+ // Just "~/" or "~/something"
+ (PathBuf::from(home_dir), query_without_tilde.to_string())
+ }
+ } else {
+ // Relative path
+ let base = cwd?;
+ let parts: Vec<&str> = query.rsplitn(2, '/').collect();
+ if parts.len() == 2 {
+ // Has a directory component
+ let dir = base.join(parts[1]);
+ let prefix = parts[0];
+ (dir, prefix.to_string())
+ } else {
+ // Just a name in the current directory
+ (base.clone(), query.to_string())
+ }
+ };
+
+ // Convert to host path
+ let host_base_dir =
+ PathBuf::from("/host").join(base_dir.strip_prefix("/").unwrap_or(&base_dir));
+
+ // Read directory entries
+ let entries = fs::read_dir(&host_base_dir).ok()?;
+
+ // Collect all matches with their scores and directory status
+ let mut all_matches: Vec<(String, i64, bool)> = Vec::new();
+
+ for entry in entries.flatten() {
+ if let Some(name) = entry.file_name().to_str() {
+ // Skip hidden files unless the query starts with a dot
+ if name.starts_with('.') && !search_prefix.starts_with('.') {
+ continue;
+ }
+
+ if let Some((score, _indices)) = matcher.fuzzy_indices(name, &search_prefix) {
+ // Check if this entry is a directory
+ let is_dir = entry.metadata().ok().map(|m| m.is_dir()).unwrap_or(false);
+ all_matches.push((name.to_string(), score, is_dir));
+ }
+ }
+ }
+
+ if all_matches.is_empty() {
+ return None;
+ }
+
+ // Sort by score (highest first), then by length (shortest first)
+ all_matches.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.len().cmp(&b.0.len())));
+
+ // Get the best match
+ let (name, score, is_directory) = all_matches[0].clone();
+
+ // Check if there are multiple matches that share the same prefix
+ let name_matches: Vec<(String, i64)> = all_matches
+ .iter()
+ .map(|(n, s, _)| (n.clone(), *s))
+ .collect();
+ let is_prefix_completion = check_for_prefix_matches(&name_matches, &name);
+
+ // Reconstruct the full path
+ let completed_text = if query.starts_with('/') {
+ // Absolute path
+ let dir_part = query.rsplitn(2, '/').nth(1).unwrap_or("");
+ if dir_part.is_empty() {
+ format!("/{}", name)
+ } else {
+ format!("{}/{}", dir_part, name)
+ }
+ } else if query.starts_with("~/") {
+ // Home directory
+ let query_without_tilde = &query[2..];
+ let dir_part = query_without_tilde.rsplitn(2, '/').nth(1).unwrap_or("");
+ if dir_part.is_empty() {
+ format!("~/{}", name)
+ } else {
+ format!("~/{}/{}", dir_part, name)
+ }
+ } else {
+ // Relative path
+ let dir_part = query.rsplitn(2, '/').nth(1).unwrap_or("");
+ if dir_part.is_empty() {
+ name.clone()
+ } else {
+ format!("{}/{}", dir_part, name)
+ }
+ };
+
+ Some(CompletionResult {
+ completed_text,
+ score,
+ is_directory,
+ is_prefix_completion,
+ })
+}
+
+/// Check if there are multiple matches that start with the best match's name
+/// Returns true if the best match is a prefix of other matches
+fn check_for_prefix_matches(matches: &[(String, i64)], best_match: &str) -> bool {
+ if matches.len() <= 1 {
+ return false;
+ }
+
+ // Count how many matches start with the best_match text
+ let prefix_count = matches
+ .iter()
+ .filter(|(name, _)| name.starts_with(best_match) && name != best_match)
+ .count();
+
+ prefix_count > 0
+}
+
+/// Perform fuzzy completion on the current input
+/// Returns the best completion (command or path) with appropriate suffix (space or slash)
+pub fn fuzzy_complete(
+ input: &str,
+ executables: &BTreeMap<String, PathBuf>,
+ cwd: Option<&PathBuf>,
+) -> Option<String> {
+ // If input is empty, nothing to complete
+ if input.is_empty() {
+ return None;
+ }
+
+ // Extract the last word/token to complete
+ // For simplicity, we'll complete the entire input if it's a single word,
+ // or the last space-separated token if there are multiple words
+ let tokens: Vec<&str> = input.split_whitespace().collect();
+ let to_complete = if tokens.is_empty() {
+ input
+ } else {
+ tokens.last().unwrap()
+ };
+
+ // Get both command and path completions
+ let command_result = fuzzy_complete_command(to_complete, executables);
+ let path_result = fuzzy_complete_path(to_complete, cwd);
+
+ // Choose the better match based on score
+ match (command_result, path_result) {
+ (Some(cmd), Some(path)) => {
+ // Both matched, use the higher score
+ let (result, completion_type, is_dir, is_prefix) = if cmd.score >= path.score {
+ (
+ cmd.completed_text,
+ CompletionType::Command,
+ false,
+ cmd.is_prefix_completion,
+ )
+ } else {
+ (
+ path.completed_text,
+ CompletionType::Path,
+ path.is_directory,
+ path.is_prefix_completion,
+ )
+ };
+
+ // Add appropriate suffix (skip slash if this is a prefix completion)
+ let suffix = get_completion_suffix(completion_type, is_dir, is_prefix);
+ let completed = format!("{}{}", result, suffix);
+
+ // Replace the last token with the completion
+ Some(replace_last_token(input, &completed))
+ },
+ (Some(cmd), None) => {
+ // Command completion - add space (unless it's a prefix completion)
+ let suffix = if cmd.is_prefix_completion { "" } else { " " };
+ let completed = format!("{}{}", cmd.completed_text, suffix);
+ Some(replace_last_token(input, &completed))
+ },
+ (None, Some(path)) => {
+ // Path completion - add slash if directory (unless it's a prefix completion)
+ let suffix = if path.is_prefix_completion {
+ ""
+ } else if path.is_directory {
+ "/"
+ } else {
+ ""
+ };
+ let completed = format!("{}{}", path.completed_text, suffix);
+ Some(replace_last_token(input, &completed))
+ },
+ (None, None) => None,
+ }
+}
+
+/// Get the appropriate suffix for a completion
+fn get_completion_suffix(
+ completion_type: CompletionType,
+ is_directory: bool,
+ is_prefix_completion: bool,
+) -> &'static str {
+ // No suffix for prefix completions
+ if is_prefix_completion {
+ return "";
+ }
+
+ match completion_type {
+ CompletionType::Command => " ",
+ CompletionType::Path => {
+ if is_directory {
+ "/"
+ } else {
+ ""
+ }
+ },
+ }
+}
+
+/// Replace the last whitespace-separated token in the input with the completion
+fn replace_last_token(input: &str, completion: &str) -> String {
+ let tokens: Vec<&str> = input.split_whitespace().collect();
+ if tokens.is_empty() {
+ return completion.to_string();
+ }
+
+ if tokens.len() == 1 {
+ completion.to_string()
+ } else {
+ // Join all tokens except the last one, then add the completion
+ let mut result = tokens[..tokens.len() - 1].join(" ");
+ result.push(' ');
+ result.push_str(completion);
+ result
+ }
+}