mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
..
This commit is contained in:
parent
bcefbfd990
commit
510884287b
4 changed files with 45 additions and 13 deletions
|
|
@ -8,10 +8,6 @@ impl From<&Exec> for Opt {
|
||||||
fn from(e: &Exec) -> Self { Self(e.named.contains_key("submit")) }
|
fn from(e: &Exec) -> Self { Self(e.named.contains_key("submit")) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<bool> for Opt {
|
|
||||||
fn from(b: bool) -> Self { Self(b) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Completion {
|
impl Completion {
|
||||||
pub fn close(&mut self, opt: impl Into<Opt>) -> bool {
|
pub fn close(&mut self, opt: impl Into<Opt>) -> bool {
|
||||||
let submit = opt.into().0;
|
let submit = opt.into().0;
|
||||||
|
|
@ -22,7 +18,7 @@ impl Completion {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cursor = 0;
|
self.caches.clear();
|
||||||
self.visible = false;
|
self.visible = false;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,6 @@ impl Completion {
|
||||||
ControlFlow::Continue(v)
|
ControlFlow::Continue(v)
|
||||||
});
|
});
|
||||||
|
|
||||||
self.close(false);
|
|
||||||
self.cands = match flow {
|
self.cands = match flow {
|
||||||
ControlFlow::Continue(v) => v,
|
ControlFlow::Continue(v) => v,
|
||||||
ControlFlow::Break(v) => v,
|
ControlFlow::Break(v) => v,
|
||||||
|
|
@ -54,6 +53,8 @@ impl Completion {
|
||||||
self.ticket = opt.ticket;
|
self.ticket = opt.ticket;
|
||||||
|
|
||||||
if !self.cands.is_empty() {
|
if !self.cands.is_empty() {
|
||||||
|
self.offset = 0;
|
||||||
|
self.cursor = 0;
|
||||||
self.visible = true;
|
self.visible = true;
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use yazi_config::keymap::{Exec, KeymapLayer};
|
use yazi_config::keymap::{Exec, KeymapLayer};
|
||||||
|
|
||||||
|
|
@ -18,17 +20,24 @@ impl<'a> From<&'a Exec> for Opt<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Completion {
|
impl Completion {
|
||||||
|
#[inline]
|
||||||
|
fn split_path(s: &str) -> (String, String) {
|
||||||
|
match s.rsplit_once(|c| c == '/' || c == '\\') {
|
||||||
|
Some((p, c)) => (format!("{p}/"), c.to_owned()),
|
||||||
|
None => (".".to_owned(), s.to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn trigger<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool {
|
pub fn trigger<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool {
|
||||||
let opt = opt.into();
|
let opt = opt.into();
|
||||||
if self.ticket >= opt.ticket {
|
if opt.ticket < self.ticket {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.close(false);
|
|
||||||
self.ticket = opt.ticket;
|
self.ticket = opt.ticket;
|
||||||
|
let (parent, child) = Self::split_path(opt.before);
|
||||||
|
|
||||||
let (parent, child) = opt.before.rsplit_once('/').unwrap_or((".", opt.before));
|
if self.caches.contains_key(&parent) {
|
||||||
if self.caches.contains_key(parent) {
|
|
||||||
return self.show(
|
return self.show(
|
||||||
&Exec::call("show", vec![])
|
&Exec::call("show", vec![])
|
||||||
.with("cache-name", parent)
|
.with("cache-name", parent)
|
||||||
|
|
@ -38,7 +47,6 @@ impl Completion {
|
||||||
}
|
}
|
||||||
|
|
||||||
let ticket = self.ticket;
|
let ticket = self.ticket;
|
||||||
let (parent, child) = (parent.to_owned(), child.to_owned());
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut dir = fs::read_dir(&parent).await?;
|
let mut dir = fs::read_dir(&parent).await?;
|
||||||
let mut cache = Vec::new();
|
let mut cache = Vec::new();
|
||||||
|
|
@ -70,6 +78,33 @@ impl Completion {
|
||||||
|
|
||||||
Ok::<(), anyhow::Error>(())
|
Ok::<(), anyhow::Error>(())
|
||||||
});
|
});
|
||||||
false
|
|
||||||
|
mem::replace(&mut self.visible, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_explode() {
|
||||||
|
assert_eq!(Completion::split_path(""), (".".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path(" "), (".".to_owned(), " ".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("/"), ("/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("//"), ("//".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("/foo"), ("/".to_owned(), "foo".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("/foo/"), ("/foo/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("/foo/bar"), ("/foo/".to_owned(), "bar".to_owned()));
|
||||||
|
|
||||||
|
// Windows
|
||||||
|
assert_eq!(Completion::split_path("foo"), (".".to_owned(), "foo".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("foo\\"), ("foo/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("foo\\bar"), ("foo/".to_owned(), "bar".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("foo\\bar\\"), ("foo\\bar/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("C:\\"), ("C:/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("C:\\foo"), ("C:/".to_owned(), "foo".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("C:\\foo\\"), ("C:\\foo/".to_owned(), "".to_owned()));
|
||||||
|
assert_eq!(Completion::split_path("C:\\foo\\bar"), ("C:\\foo/".to_owned(), "bar".to_owned()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ impl<'a> From<&'a Exec> for Opt<'a> {
|
||||||
fn from(e: &'a Exec) -> Self {
|
fn from(e: &'a Exec) -> Self {
|
||||||
Self {
|
Self {
|
||||||
word: e.args.first().map(|w| w.as_str()).unwrap_or_default(),
|
word: e.args.first().map(|w| w.as_str()).unwrap_or_default(),
|
||||||
ticket: e.named.get("ticket").and_then(|v| v.parse().ok()).unwrap_or(0),
|
ticket: e.named.get("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue