diff --git a/yazi-core/src/completion/commands/show.rs b/yazi-core/src/completion/commands/show.rs index 1102d225..d31d5c05 100644 --- a/yazi-core/src/completion/commands/show.rs +++ b/yazi-core/src/completion/commands/show.rs @@ -3,15 +3,15 @@ use yazi_config::keymap::Exec; use crate::completion::Completion; pub struct Opt<'a> { - cands: &'a Vec, - version: usize, + cands: &'a Vec, + ticket: usize, } impl<'a> From<&'a Exec> for Opt<'a> { fn from(e: &'a Exec) -> Self { Self { - cands: &e.args, - version: e.named.get("version").and_then(|v| v.parse().ok()).unwrap_or(0), + cands: &e.args, + ticket: e.named.get("ticket").and_then(|v| v.parse().ok()).unwrap_or(0), } } } @@ -19,13 +19,13 @@ impl<'a> From<&'a Exec> for Opt<'a> { impl Completion { pub fn show<'a>(&mut self, opt: impl Into>) -> bool { let opt = opt.into(); - if self.version != opt.version { + if self.ticket != opt.ticket { return false; } self.close(false); self.items = opt.cands.clone(); - self.version = opt.version; + self.ticket = opt.ticket; self.visible = true; true } diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index ac977004..1b3be274 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -4,15 +4,15 @@ use yazi_config::keymap::Exec; use crate::completion::Completion; pub struct Opt<'a> { - word: &'a str, - version: usize, + word: &'a str, + ticket: usize, } impl<'a> From<&'a Exec> for Opt<'a> { fn from(e: &'a Exec) -> Self { Self { - word: e.args.get(0).map(|w| w.as_str()).unwrap_or_default(), - version: e.named.get("version").and_then(|v| v.parse().ok()).unwrap_or(0), + word: e.args.get(0).map(|w| w.as_str()).unwrap_or_default(), + ticket: e.named.get("ticket").and_then(|v| v.parse().ok()).unwrap_or(0), } } } @@ -20,12 +20,12 @@ impl<'a> From<&'a Exec> for Opt<'a> { impl Completion { pub fn trigger<'a>(&mut self, opt: impl Into>) -> bool { let opt = opt.into(); - if self.version >= opt.version { + if self.ticket >= opt.ticket { return false; } self.close(false); - self.version = opt.version; + self.ticket = opt.ticket; info!("trigger completion: {}", opt.word); tokio::spawn(async move {}); diff --git a/yazi-core/src/completion/completion.rs b/yazi-core/src/completion/completion.rs index d25198cf..5a7f6668 100644 --- a/yazi-core/src/completion/completion.rs +++ b/yazi-core/src/completion/completion.rs @@ -3,7 +3,7 @@ pub struct Completion { pub items: Vec, pub cursor: usize, - pub version: usize, + pub ticket: usize, pub visible: bool, } diff --git a/yazi-core/src/input/input.rs b/yazi-core/src/input/input.rs index fbe39bba..2e62fa53 100644 --- a/yazi-core/src/input/input.rs +++ b/yazi-core/src/input/input.rs @@ -12,9 +12,10 @@ use crate::{emit, external, Position}; #[derive(Default)] pub struct Input { snaps: InputSnaps, + pub ticket: usize, pub visible: bool, - title: String, + pub title: String, pub position: Position, // Typing @@ -30,6 +31,7 @@ impl Input { pub fn show(&mut self, opt: InputOpt, tx: UnboundedSender>) { self.close(false); self.snaps.reset(opt.value); + self.ticket = self.ticket.wrapping_add(1); self.visible = true; self.title = opt.title; @@ -210,8 +212,9 @@ impl Input { snap.value.insert_str(snap.idx(snap.cursor).unwrap(), s); } + self.move_(s.chars().count() as isize); self.flush_value(); - self.move_(s.chars().count() as isize) + true } pub fn backspace(&mut self) -> bool { @@ -222,8 +225,9 @@ impl Input { snap.value.remove(snap.idx(snap.cursor - 1).unwrap()); } + self.move_(-1); self.flush_value(); - self.move_(-1) + true } pub fn delete(&mut self, cut: bool, insert: bool) -> bool { @@ -322,14 +326,18 @@ impl Input { } #[inline] - fn flush_value(&self) { + fn flush_value(&mut self) { + self.ticket = self.ticket.wrapping_add(1); + if self.realtime { let value = self.snap().value.clone(); self.callback.as_ref().unwrap().send(Err(InputError::Typed(value))).ok(); } if self.completion { emit!(Call( - Exec::call("complete", vec!["".to_string()]).with("version", 1).vec(), + Exec::call("complete", vec![self.partition()[0].to_owned()]) + .with("ticket", self.ticket) + .vec(), KeymapLayer::Input )); } @@ -337,9 +345,6 @@ impl Input { } impl Input { - #[inline] - pub fn title(&self) -> String { self.title.clone() } - #[inline] pub fn value(&self) -> &str { self.snap().slice(self.snap().window()) } @@ -366,6 +371,13 @@ impl Input { Some(s..s + snap.slice(start..end).width() as u16) } + #[inline] + pub fn partition(&self) -> [&str; 2] { + let snap = self.snap(); + let idx = snap.idx(snap.cursor).unwrap(); + [&snap.value[..idx], &snap.value[idx..]] + } + #[inline] fn snap(&self) -> &InputSnap { self.snaps.current() } diff --git a/yazi-fm/src/input/input.rs b/yazi-fm/src/input/input.rs index 0aab7ea0..71590d10 100644 --- a/yazi-fm/src/input/input.rs +++ b/yazi-fm/src/input/input.rs @@ -33,7 +33,7 @@ impl<'a> Widget for Input<'a> { .border_type(BorderType::Rounded) .border_style(THEME.input.border.into()) .title({ - let mut line = Line::from(input.title()); + let mut line = Line::from(input.title.as_str()); line.patch_style(THEME.input.title.into()); line }),