From f442ae2adf96917cd05d00906632e80b54153789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BB=A9c=20To=C3=A0n?= <33489972+ndtoan96@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:16:23 +0700 Subject: [PATCH] feat: trigger path completion with both `/` and `\` on Windows (#909) Co-authored-by: sxyazi --- yazi-core/src/completion/commands/trigger.rs | 14 ++++++++++---- yazi-core/src/input/commands/complete.rs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index 4fdf9901..d3d7054f 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -5,6 +5,12 @@ use yazi_shared::{emit, event::Cmd, render, Layer}; use crate::completion::Completion; +#[cfg(windows)] +const SEPARATOR: [char; 2] = ['/', '\\']; + +#[cfg(not(windows))] +const SEPARATOR: char = std::path::MAIN_SEPARATOR; + pub struct Opt { word: String, ticket: usize, @@ -59,7 +65,7 @@ impl Completion { )); } - Ok::<(), anyhow::Error>(()) + Ok::<_, anyhow::Error>(()) }); render!(mem::replace(&mut self.visible, false)); @@ -67,7 +73,7 @@ impl Completion { #[inline] fn split_path(s: &str) -> (String, String) { - match s.rsplit_once(MAIN_SEPARATOR) { + match s.rsplit_once(SEPARATOR) { Some((p, c)) => (format!("{p}{}", MAIN_SEPARATOR), c.to_owned()), None => (".".to_owned(), s.to_owned()), } @@ -80,7 +86,7 @@ mod tests { #[cfg(unix)] #[test] - fn test_explode() { + fn test_split() { 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())); @@ -92,7 +98,7 @@ mod tests { #[cfg(windows)] #[test] - fn test_explode() { + fn test_split() { 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())); diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index 64686b92..a14a30fc 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -1,9 +1,15 @@ -use std::path::MAIN_SEPARATOR; +use std::path::MAIN_SEPARATOR_STR; use yazi_shared::{event::Cmd, render}; use crate::input::Input; +#[cfg(windows)] +const SEPARATOR: [char; 2] = ['/', '\\']; + +#[cfg(not(windows))] +const SEPARATOR: char = std::path::MAIN_SEPARATOR; + pub struct Opt { word: String, ticket: usize, @@ -26,10 +32,10 @@ impl Input { } let [before, after] = self.partition(); - let new = if let Some((prefix, _)) = before.rsplit_once(MAIN_SEPARATOR) { - format!("{prefix}/{}{after}", opt.word) + let new = if let Some((prefix, _)) = before.rsplit_once(SEPARATOR) { + format!("{prefix}/{}{after}", opt.word).replace(SEPARATOR, MAIN_SEPARATOR_STR) } else { - format!("{}{after}", opt.word) + format!("{}{after}", opt.word).replace(SEPARATOR, MAIN_SEPARATOR_STR) }; let snap = self.snaps.current_mut();