diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index 23331097..14d06512 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -1,4 +1,4 @@ -use std::mem; +use std::{mem, path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}}; use tokio::fs; use yazi_config::keymap::{Exec, KeymapLayer}; @@ -22,8 +22,8 @@ impl<'a> From<&'a Exec> for Opt<'a> { 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()), + match s.rsplit_once(MAIN_SEPARATOR) { + Some((p, c)) => (format!("{p}{}", MAIN_SEPARATOR), c.to_owned()), None => (".".to_owned(), s.to_owned()), } } @@ -55,14 +55,11 @@ impl Completion { continue; }; - let sep = if !meta.is_dir() { - "" - } else if cfg!(windows) { - "\\" - } else { - "/" - }; - cache.push(format!("{}{sep}", f.file_name().to_string_lossy())); + cache.push(format!( + "{}{}", + f.file_name().to_string_lossy(), + if meta.is_dir() { MAIN_SEPARATOR_STR } else { "" }, + )); } if !cache.is_empty() { @@ -87,6 +84,7 @@ impl Completion { mod tests { use super::*; + #[cfg(unix)] #[test] fn test_explode() { assert_eq!(Completion::split_path(""), (".".to_owned(), "".to_owned())); @@ -96,15 +94,18 @@ mod tests { 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 + #[cfg(windows)] + #[test] + fn test_explode() { 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())); + 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())); } } diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index c6d4dd49..554b157f 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -1,3 +1,5 @@ +use std::path::MAIN_SEPARATOR; + use yazi_config::keymap::Exec; use crate::input::Input; @@ -24,7 +26,7 @@ impl Input { } let [before, after] = self.partition(); - let new = if let Some((prefix, _)) = before.rsplit_once('/') { + let new = if let Some((prefix, _)) = before.rsplit_once(MAIN_SEPARATOR) { format!("{prefix}/{}{after}", opt.word) } else { format!("{}{after}", opt.word) diff --git a/yazi-core/src/manager/commands/create.rs b/yazi-core/src/manager/commands/create.rs index 3548f4b8..e97af987 100644 --- a/yazi-core/src/manager/commands/create.rs +++ b/yazi-core/src/manager/commands/create.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{PathBuf, MAIN_SEPARATOR}; use tokio::fs; use yazi_config::keymap::Exec; @@ -32,7 +32,7 @@ impl Manager { } } - if name.ends_with('/') { + if name.ends_with(MAIN_SEPARATOR) { fs::create_dir_all(&path).await?; } else { fs::create_dir_all(&path.parent().unwrap()).await.ok(); diff --git a/yazi-fm/src/completion/completion.rs b/yazi-fm/src/completion/completion.rs index dc7495ee..5243159f 100644 --- a/yazi-fm/src/completion/completion.rs +++ b/yazi-fm/src/completion/completion.rs @@ -1,3 +1,5 @@ +use std::path::MAIN_SEPARATOR; + use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, List, ListItem, Widget}}; use yazi_config::THEME; use yazi_core::{Ctx, Position}; @@ -19,7 +21,7 @@ impl<'a> Widget for Completion<'a> { .iter() .enumerate() .map(|(i, x)| { - let icon = if x.ends_with('/') || x.ends_with('\\') { + let icon = if x.ends_with(MAIN_SEPARATOR) { &THEME.completion.icon_folder } else { &THEME.completion.icon_file