yazi/yazi-core/src/input/commands/complete.rs
Nguyễn Đức Toàn f442ae2adf
feat: trigger path completion with both / and \ on Windows (#909)
Co-authored-by: sxyazi <sxyazi@gmail.com>
2024-04-14 23:16:23 +08:00

53 lines
1.1 KiB
Rust

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,
}
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
word: c.take_first().unwrap_or_default(),
ticket: c.take_name("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
}
}
}
impl Input {
pub fn complete(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if self.ticket != opt.ticket {
return;
}
let [before, after] = self.partition();
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).replace(SEPARATOR, MAIN_SEPARATOR_STR)
};
let snap = self.snaps.current_mut();
if new == snap.value {
return;
}
let delta = new.chars().count() as isize - snap.value.chars().count() as isize;
snap.value = new;
self.move_(delta);
self.flush_value();
render!();
}
}