From df76e74ce36d7b92a9c471f60ae85df7675396b7 Mon Sep 17 00:00:00 2001 From: qkzk Date: Sat, 6 Jan 2024 23:31:46 +0100 Subject: flagged: copy filename & filepath --- development.md | 1 + src/app/tab.rs | 19 ------------------- src/common/utils.rs | 15 +++++++++++++++ src/event/action_map.rs | 4 ++-- src/event/event_exec.rs | 38 ++++++++++++++++++++++++++++++++------ 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/development.md b/development.md index a019552..c1d44d2 100644 --- a/development.md +++ b/development.md @@ -811,6 +811,7 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. - [x] chmod - [x] disable new nodes (file, dir) - [x] disable all cd since we can't see the directory + - [x] copy filename & filepath - [x] trashmode should display which shortcut erase all trash. - [x] add left tab current dir to right shortcut and vice versa diff --git a/src/app/tab.rs b/src/app/tab.rs index 87bd112..f046406 100644 --- a/src/app/tab.rs +++ b/src/app/tab.rs @@ -213,25 +213,6 @@ impl Tab { } } - /// Copy the selected filename to the clipboard. Only the filename. - pub fn filename_to_clipboard(&self) { - let Ok(file) = self.current_file() else { - return; - }; - set_clipboard(file.filename.to_string()) - } - - /// Copy the selected filepath to the clipboard. The absolute path. - pub fn filepath_to_clipboard(&self) { - let Ok(file) = self.current_file() else { - return; - }; - let Some(path_str) = file.path.to_str() else { - return; - }; - set_clipboard(path_str.to_owned()) - } - /// Refresh everything but the view pub fn refresh_params(&mut self) -> Result<()> { self.preview = Preview::empty(); diff --git a/src/common/utils.rs b/src/common/utils.rs index 6e2104c..5b81871 100644 --- a/src/common/utils.rs +++ b/src/common/utils.rs @@ -132,6 +132,21 @@ pub fn set_clipboard(content: String) { let _ = ctx.get_contents(); } +/// Copy the filename to the clipboard. Only the filename. +pub fn filename_to_clipboard(path: &std::path::Path) { + let Some(filename) = path.file_name() else { + return; + }; + let filename = filename.to_string_lossy().to_string(); + set_clipboard(filename) +} + +/// Copy the filepath to the clipboard. The absolute path. +pub fn filepath_to_clipboard(path: &std::path::Path) { + let path = path.to_string_lossy().to_string(); + set_clipboard(path) +} + /// Convert a row into a `crate::fm::ContentWindow` index. /// Just remove the header rows. pub fn row_to_window_index(row: u16) -> usize { diff --git a/src/event/action_map.rs b/src/event/action_map.rs index a21495a..d61d653 100644 --- a/src/event/action_map.rs +++ b/src/event/action_map.rs @@ -124,8 +124,8 @@ impl ActionMap { Self::CliMenu => EventAction::cli_menu(status), Self::Compress => EventAction::compress(status), Self::Context => EventAction::context(status), - Self::CopyFilename => EventAction::copy_filename(current_tab), - Self::CopyFilepath => EventAction::copy_filepath(current_tab), + Self::CopyFilename => EventAction::copy_filename(status), + Self::CopyFilepath => EventAction::copy_filepath(status), Self::CopyPaste => EventAction::copy_paste(status), Self::CutPaste => EventAction::cut_paste(status), Self::Delete => EventAction::delete(status), diff --git a/src/event/event_exec.rs b/src/event/event_exec.rs index c8974ac..36d901b 100644 --- a/src/event/event_exec.rs +++ b/src/event/event_exec.rs @@ -5,6 +5,8 @@ use anyhow::{Context, Result}; use crate::app::Status; use crate::app::Tab; +use crate::common::filename_to_clipboard; +use crate::common::filepath_to_clipboard; use crate::common::LAZYGIT; use crate::common::NCDU; use crate::common::{is_program_in_path, open_in_current_neovim}; @@ -975,17 +977,41 @@ impl EventAction { } /// Copy the filename of the selected file in normal mode. - pub fn copy_filename(tab: &mut Tab) -> Result<()> { - if let Display::Directory | Display::Tree = tab.display_mode { - tab.filename_to_clipboard(); + pub fn copy_filename(status: &Status) -> Result<()> { + match status.current_tab().display_mode { + Display::Tree | Display::Directory => { + let Ok(file_info) = status.current_tab().current_file() else { + return Ok(()); + }; + filename_to_clipboard(&file_info.path); + } + Display::Flagged => { + let Some(path) = status.menu.flagged.selected() else { + return Ok(()); + }; + filename_to_clipboard(&path); + } + _ => return Ok(()), } Ok(()) } /// Copy the filepath of the selected file in normal mode. - pub fn copy_filepath(tab: &mut Tab) -> Result<()> { - if let Display::Directory | Display::Tree = tab.display_mode { - tab.filepath_to_clipboard(); + pub fn copy_filepath(status: &Status) -> Result<()> { + match status.current_tab().display_mode { + Display::Tree | Display::Directory => { + let Ok(file_info) = status.current_tab().current_file() else { + return Ok(()); + }; + filepath_to_clipboard(&file_info.path); + } + Display::Flagged => { + let Some(path) = status.menu.flagged.selected() else { + return Ok(()); + }; + filepath_to_clipboard(&path); + } + _ => return Ok(()), } Ok(()) } -- cgit v1.2.3