From d9f48a90f920285b1ea33e214aa8e07da21d649e Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Sun, 14 Apr 2024 12:44:30 +0700 Subject: [PATCH] move implement of slash and backslash path complete to Windows specific code --- yazi-core/src/completion/commands/trigger.rs | 7 +++++++ yazi-core/src/input/commands/complete.rs | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index c87d231c..f3c585c0 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -67,10 +67,17 @@ impl Completion { #[inline] fn split_path(s: &str) -> (String, String) { + #[cfg(target_os = "windows")] match s.rsplit_once(['/', '\\']) { Some((p, c)) => (format!("{p}{}", MAIN_SEPARATOR), c.to_owned()), None => (".".to_owned(), s.to_owned()), } + + #[cfg(not(target_os = "windows"))] + match s.rsplit_once(MAIN_SEPARATOR) { + Some((p, c)) => (format!("{p}{}", MAIN_SEPARATOR), c.to_owned()), + None => (".".to_owned(), s.to_owned()), + } } } diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index 4aabdd55..c4d6850b 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -1,5 +1,3 @@ -use std::path::MAIN_SEPARATOR_STR; - use yazi_shared::{event::Cmd, render}; use crate::input::Input; @@ -26,21 +24,28 @@ impl Input { } let [before, after] = self.partition(); + + #[cfg(target_os = "windows")] let new = if let Some((prefix, _)) = before.rsplit_once(['/', '\\']) { + format!("{prefix}/{}{after}", opt.word).replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR) + } else { + format!("{}{after}", opt.word).replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR) + }; + + #[cfg(not(target_os = "windows"))] + let new = if let Some((prefix, _)) = before.rsplit_once(std::path::MAIN_SEPARATOR) { format!("{prefix}/{}{after}", opt.word) } else { format!("{}{after}", opt.word) }; - let normalized_new = new.replace(['/', '\\'], MAIN_SEPARATOR_STR); - let snap = self.snaps.current_mut(); - if normalized_new == snap.value { + if new == snap.value { return; } - let delta = normalized_new.chars().count() as isize - snap.value.chars().count() as isize; - snap.value = normalized_new; + let delta = new.chars().count() as isize - snap.value.chars().count() as isize; + snap.value = new; self.move_(delta); self.flush_value();