feat: Support entry renaming #3
3 changed files with 29 additions and 7 deletions
|
@ -34,17 +34,18 @@ impl<W: Write> EventQueue<W> {
|
|||
(Mode::Normal, k) if k == Key::from(&ck.rename_mode) => self.do_enter_rename_mode(),
|
||||
(Mode::Rename(..), k) if k == Key::from(&ck.quit) => self.do_enter_normal_mode(),
|
||||
(Mode::Rename(new_name), k) if k == Key::from("return") => {
|
||||
if new_name.is_empty() {
|
||||
return self.do_enter_normal_mode();
|
||||
if !new_name.chars().all(char::is_whitespace) {
|
||||
self.do_rename_current_file(&new_name);
|
||||
self.do_reload();
|
||||
}
|
||||
self.do_rename_current_file(&new_name);
|
||||
self.do_reload();
|
||||
self.do_enter_normal_mode()
|
||||
self.do_enter_normal_mode();
|
||||
Some(())
|
||||
}
|
||||
(Mode::Rename(new_name), k) => {
|
||||
if let Cow::Owned(updated_name) = self.do_handle_rename_input(&new_name, k.inner())
|
||||
{
|
||||
self.pager.mode = Mode::Rename(updated_name);
|
||||
self.update_pager(0);
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
|
|
@ -8,10 +8,19 @@ use crate::{controller::EventQueue, view::Mode};
|
|||
impl<W: Write> EventQueue<W> {
|
||||
pub fn do_enter_normal_mode(&mut self) -> Option<()> {
|
||||
self.pager.mode = Mode::Normal;
|
||||
self.update_pager(0);
|
||||
Some(())
|
||||
}
|
||||
pub fn do_enter_rename_mode(&mut self) -> Option<()> {
|
||||
self.pager.mode = Mode::Rename(String::new());
|
||||
self.pager.mode = Mode::Rename(
|
||||
self.pager
|
||||
.current_entry
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
);
|
||||
self.update_pager(0);
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,19 @@ impl<W: Write> Pager<W> {
|
|||
if index == self.cursor_row {
|
||||
self.current_entry =
|
||||
path::absolute(&entry.path).unwrap_or_else(|_| entry.path.clone());
|
||||
self.print_text_entry_emphasized(&entry.display_text, 1 + spacing_top + i)
|
||||
|
||||
let rename_name = self.mode.new_name().map(|new_name| {
|
||||
let filename = entry.path.file_name().unwrap().to_str().unwrap();
|
||||
let mut new_display_text = entry.display_text.chars();
|
||||
for _ in 0..(filename.chars().count() + entry.path.is_dir() as usize) {
|
||||
new_display_text.next_back();
|
||||
}
|
||||
format!("{}{new_name}", new_display_text.collect::<String>())
|
||||
});
|
||||
self.print_text_entry_emphasized(
|
||||
rename_name.as_ref().unwrap_or(&entry.display_text),
|
||||
1 + spacing_top + i,
|
||||
)
|
||||
} else {
|
||||
self.print_text_entry(&entry.display_text, 1 + spacing_top + i);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue