fix: Windows directory separator

This commit is contained in:
sxyazi 2023-11-14 11:58:24 +08:00
parent c3446cd941
commit 3caf3ccda5
No known key found for this signature in database
4 changed files with 28 additions and 23 deletions

View file

@ -1,4 +1,4 @@
use std::mem; use std::{mem, path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}};
use tokio::fs; use tokio::fs;
use yazi_config::keymap::{Exec, KeymapLayer}; use yazi_config::keymap::{Exec, KeymapLayer};
@ -22,8 +22,8 @@ impl<'a> From<&'a Exec> for Opt<'a> {
impl Completion { impl Completion {
#[inline] #[inline]
fn split_path(s: &str) -> (String, String) { fn split_path(s: &str) -> (String, String) {
match s.rsplit_once(|c| c == '/' || c == '\\') { match s.rsplit_once(MAIN_SEPARATOR) {
Some((p, c)) => (format!("{p}/"), c.to_owned()), Some((p, c)) => (format!("{p}{}", MAIN_SEPARATOR), c.to_owned()),
None => (".".to_owned(), s.to_owned()), None => (".".to_owned(), s.to_owned()),
} }
} }
@ -55,14 +55,11 @@ impl Completion {
continue; continue;
}; };
let sep = if !meta.is_dir() { cache.push(format!(
"" "{}{}",
} else if cfg!(windows) { f.file_name().to_string_lossy(),
"\\" if meta.is_dir() { MAIN_SEPARATOR_STR } else { "" },
} else { ));
"/"
};
cache.push(format!("{}{sep}", f.file_name().to_string_lossy()));
} }
if !cache.is_empty() { if !cache.is_empty() {
@ -87,6 +84,7 @@ impl Completion {
mod tests { mod tests {
use super::*; use super::*;
#[cfg(unix)]
#[test] #[test]
fn test_explode() { fn test_explode() {
assert_eq!(Completion::split_path(""), (".".to_owned(), "".to_owned())); 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"), ("/".to_owned(), "foo".to_owned()));
assert_eq!(Completion::split_path("/foo/"), ("/foo/".to_owned(), "".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/".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"), (".".to_owned(), "foo".to_owned()));
assert_eq!(Completion::split_path("foo\\"), ("foo/".to_owned(), "".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\\".to_owned(), "bar".to_owned()));
assert_eq!(Completion::split_path("foo\\bar\\"), ("foo\\bar/".to_owned(), "".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:\\"), ("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:\\".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\\"), ("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("C:\\foo\\bar"), ("C:\\foo\\".to_owned(), "bar".to_owned()));
} }
} }

View file

@ -1,3 +1,5 @@
use std::path::MAIN_SEPARATOR;
use yazi_config::keymap::Exec; use yazi_config::keymap::Exec;
use crate::input::Input; use crate::input::Input;
@ -24,7 +26,7 @@ impl Input {
} }
let [before, after] = self.partition(); 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) format!("{prefix}/{}{after}", opt.word)
} else { } else {
format!("{}{after}", opt.word) format!("{}{after}", opt.word)

View file

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::{PathBuf, MAIN_SEPARATOR};
use tokio::fs; use tokio::fs;
use yazi_config::keymap::Exec; 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?; fs::create_dir_all(&path).await?;
} else { } else {
fs::create_dir_all(&path.parent().unwrap()).await.ok(); fs::create_dir_all(&path.parent().unwrap()).await.ok();

View file

@ -1,3 +1,5 @@
use std::path::MAIN_SEPARATOR;
use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, List, ListItem, Widget}}; use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, List, ListItem, Widget}};
use yazi_config::THEME; use yazi_config::THEME;
use yazi_core::{Ctx, Position}; use yazi_core::{Ctx, Position};
@ -19,7 +21,7 @@ impl<'a> Widget for Completion<'a> {
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
let icon = if x.ends_with('/') || x.ends_with('\\') { let icon = if x.ends_with(MAIN_SEPARATOR) {
&THEME.completion.icon_folder &THEME.completion.icon_folder
} else { } else {
&THEME.completion.icon_file &THEME.completion.icon_file