move implement of slash and backslash path complete to Windows specific code

This commit is contained in:
Nguyen Duc Toan 2024-04-14 12:44:30 +07:00
parent debae675dc
commit d9f48a90f9
No known key found for this signature in database
GPG key ID: B96F4A160C6B3251
2 changed files with 19 additions and 7 deletions

View file

@ -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()),
}
}
}

View file

@ -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();