feat: add options to replace file name on rename action

This commit is contained in:
Akmadan23 2024-01-14 22:42:20 +01:00 committed by sxyazi
parent 85334457f3
commit 3879e228e9
No known key found for this signature in database
4 changed files with 35 additions and 4 deletions

View file

@ -9,6 +9,7 @@ pub struct InputCfg {
pub realtime: bool, pub realtime: bool,
pub completion: bool, pub completion: bool,
pub highlight: bool, pub highlight: bool,
pub cursor_at: Option<usize>,
} }
#[derive(Default)] #[derive(Default)]
@ -130,6 +131,12 @@ impl InputCfg {
self.value = value.into(); self.value = value.into();
self self
} }
#[inline]
pub fn with_cursor_at(mut self, cursor_at: Option<usize>) -> Self {
self.cursor_at = cursor_at;
self
}
} }
impl SelectCfg { impl SelectCfg {

View file

@ -41,7 +41,7 @@ impl Input {
self.highlight = opt.cfg.highlight; self.highlight = opt.cfg.highlight;
// Reset snaps // Reset snaps
self.snaps.reset(opt.cfg.value, self.limit()); self.snaps.reset(opt.cfg.value, self.limit(), opt.cfg.cursor_at);
render!(); render!();
} }
} }

View file

@ -11,11 +11,15 @@ pub(super) struct InputSnaps {
impl InputSnaps { impl InputSnaps {
#[inline] #[inline]
pub(super) fn reset(&mut self, value: String, limit: usize) { pub(super) fn reset(&mut self, value: String, limit: usize, cursor_at: Option<usize>) {
self.idx = 0; self.idx = 0;
self.versions.clear(); self.versions.clear();
self.versions.push(InputSnap::new(value, limit)); self.versions.push(InputSnap::new(value, limit));
self.current = self.versions[0].clone(); self.current = self.versions[0].clone();
if let Some(cursor) = cursor_at {
self.current.cursor = cursor;
}
} }
pub(super) fn tag(&mut self, limit: usize) -> bool { pub(super) fn tag(&mut self, limit: usize) -> bool {

View file

@ -11,10 +11,18 @@ use crate::{input::Input, manager::Manager};
pub struct Opt { pub struct Opt {
force: bool, force: bool,
replace: bool,
keep_ext: bool,
} }
impl From<&Exec> for Opt { impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { force: e.named.contains_key("force") } } fn from(e: &Exec) -> Self {
Self {
force: e.named.contains_key("force"),
replace: e.named.contains_key("replace"),
keep_ext: e.named.contains_key("keep-ext"),
}
}
} }
impl Manager { impl Manager {
@ -40,8 +48,20 @@ impl Manager {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
tokio::spawn(async move { tokio::spawn(async move {
let full_name = hovered.file_name().unwrap().to_string_lossy();
let (initial_value, cursor_start) = if opt.keep_ext {
let last = full_name.split('.').last();
if last == Some(&full_name) {
("".into(), None)
} else {
(format!(".{}", last.unwrap()), Some(0))
}
} else {
(opt.replace.then(|| "").unwrap_or(&full_name).into(), None)
};
let mut result = let mut result =
Input::_show(InputCfg::rename().with_value(hovered.file_name().unwrap().to_string_lossy())); Input::_show(InputCfg::rename().with_value(initial_value).with_cursor_at(cursor_start));
let Some(Ok(name)) = result.recv().await else { let Some(Ok(name)) = result.recv().await else {
return; return;