yazi_core/completion/commands/
trigger.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::{borrow::Cow, mem, path::{MAIN_SEPARATOR_STR, PathBuf}};

use tokio::fs;
use yazi_fs::{CWD, expand_path};
use yazi_macro::{emit, render};
use yazi_shared::{Layer, event::{Cmd, CmdCow, Data}};

use crate::completion::Completion;

struct Opt {
	word:   Cow<'static, str>,
	ticket: usize,
}

impl From<CmdCow> for Opt {
	fn from(mut c: CmdCow) -> Self {
		Self {
			word:   c.take_first_str().unwrap_or_default(),
			ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0),
		}
	}
}

impl Completion {
	#[yazi_codegen::command]
	pub fn trigger(&mut self, opt: Opt) {
		if opt.ticket < self.ticket {
			return;
		}

		self.ticket = opt.ticket;
		let Some((parent, word)) = Self::split_path(&opt.word) else {
			return self.close(false);
		};

		if self.caches.contains_key(&parent) {
			return self.show(
				Cmd::default().with_any("cache-name", parent).with("word", word).with("ticket", opt.ticket),
			);
		}

		let ticket = self.ticket;
		tokio::spawn(async move {
			let mut dir = fs::read_dir(&parent).await?;
			let mut cache = vec![];
			while let Ok(Some(f)) = dir.next_entry().await {
				let Ok(meta) = f.metadata().await else { continue };

				cache.push(format!(
					"{}{}",
					f.file_name().to_string_lossy(),
					if meta.is_dir() { MAIN_SEPARATOR_STR } else { "" },
				));
			}

			if !cache.is_empty() {
				emit!(Call(
					Cmd::new("show")
						.with_any("cache", cache)
						.with_any("cache-name", parent)
						.with("word", word)
						.with("ticket", ticket),
					Layer::Completion
				));
			}

			Ok::<_, anyhow::Error>(())
		});

		render!(mem::replace(&mut self.visible, false));
	}

	fn split_path(s: &str) -> Option<(PathBuf, String)> {
		if s == "~" {
			return None; // We don't autocomplete a `~`, but `~/`
		}

		#[cfg(windows)]
		const SEP: [char; 2] = ['/', '\\'];
		#[cfg(not(windows))]
		const SEP: char = std::path::MAIN_SEPARATOR;

		Some(match s.rsplit_once(SEP) {
			Some(("", c)) => (PathBuf::from(MAIN_SEPARATOR_STR), c.to_owned()),
			Some((p, c)) => (expand_path(p), c.to_owned()),
			None => (CWD.load().to_path_buf(), s.to_owned()),
		})
	}
}

#[cfg(test)]
mod tests {
	use std::path::Path;

	use super::*;

	fn compare(s: &str, parent: &str, child: &str) -> bool {
		let (p, c) = Completion::split_path(s).unwrap();
		let p = p.strip_prefix(yazi_fs::CWD.load().as_ref()).unwrap_or(&p);
		p == Path::new(parent) && c == child
	}

	#[cfg(unix)]
	#[test]
	fn test_split() {
		yazi_fs::init();
		assert!(compare("", "", ""));
		assert!(compare(" ", "", " "));
		assert!(compare("/", "/", ""));
		assert!(compare("//", "//", ""));
		assert!(compare("/foo", "/", "foo"));
		assert!(compare("/foo/", "/foo/", ""));
		assert!(compare("/foo/bar", "/foo/", "bar"));
	}

	#[cfg(windows)]
	#[test]
	fn test_split() {
		yazi_fs::init();
		assert!(compare("foo", "", "foo"));
		assert!(compare("foo\\", "foo\\", ""));
		assert!(compare("foo\\bar", "foo\\", "bar"));
		assert!(compare("foo\\bar\\", "foo\\bar\\", ""));
		assert!(compare("C:\\", "C:\\", ""));
		assert!(compare("C:\\foo", "C:\\", "foo"));
		assert!(compare("C:\\foo\\", "C:\\foo\\", ""));
		assert!(compare("C:\\foo\\bar", "C:\\foo\\", "bar"));
	}
}