diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-16 15:47:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-16 15:47:48 -0400 |
commit | f0d1efbd8c83b46af44cb23e265c0afab4dd91b6 (patch) | |
tree | 15d66001586d7dbf6d8712ed226b29f0711e8840 /src | |
parent | f5a387a824780824d2bc159cd8c53fab714865c0 (diff) | |
parent | afb67c32719be3ad488e54c1daf50c5423e54227 (diff) |
Merge pull request #52 from makeefu/main
copying file path and directory to clipboard
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/file_ops.rs | 96 | ||||
-rw-r--r-- | src/commands/key_command.rs | 8 |
2 files changed, 72 insertions, 32 deletions
diff --git a/src/commands/file_ops.rs b/src/commands/file_ops.rs index 831acee..fb6728e 100644 --- a/src/commands/file_ops.rs +++ b/src/commands/file_ops.rs @@ -49,45 +49,77 @@ pub fn paste(context: &mut AppContext, options: IoWorkerOptions) -> JoshutoResul } pub fn copy_filename(context: &mut AppContext) -> JoshutoResult<()> { - let entry_file_name = match context + let entry_file_name = context .tab_context_ref() .curr_tab_ref() .curr_list_ref() .and_then(|c| c.curr_entry_ref()) - { - Some(entry) => Some(entry.file_name().to_string()), - None => None, - }; + .and_then(|entry| Some(entry.file_name().to_string())); + if let Some(file_name) = entry_file_name { - let clipboards = [ - ( - "wl-copy", - format!("printf '%s' {} | {} 2> /dev/null", file_name, "wl-copy"), - ), - ( - "xsel", - format!("printf '%s' {} | {} -ib 2> /dev/null", file_name, "xsel"), - ), - ( - "xclip", - format!( - "printf '%s' {} | {} -selection clipboard 2> /dev/null", - file_name, "xclip" - ), + copy_string_to_buffer(file_name)?; + } + Ok(()) +} + +pub fn copy_filepath(context: &mut AppContext) -> JoshutoResult<()> { + let entry_file_path = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .and_then(|c| c.curr_entry_ref()) + .and_then(|entry| entry.file_path().to_str()) + .and_then(|s| Some(s.to_string())); + + if let Some(file_path) = entry_file_path { + copy_string_to_buffer(file_path)?; + } + Ok(()) +} + +pub fn copy_dirname(context: &mut AppContext) -> JoshutoResult<()> { + let opt_entry = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map(|dirlist| dirlist.file_path()); + + if let Some(pathbuf) = opt_entry { + if let Some(dir) = pathbuf.to_str().map(|s| String::from(s)) { + copy_string_to_buffer(dir)? + } + }; + Ok(()) +} + +fn copy_string_to_buffer(string: String) -> JoshutoResult<()> { + let clipboards = [ + ( + "wl-copy", + format!("printf '%s' {} | {} 2> /dev/null", string, "wl-copy"), + ), + ( + "xsel", + format!("printf '%s' {} | {} -ib 2> /dev/null", string, "xsel"), + ), + ( + "xclip", + format!( + "printf '%s' {} | {} -selection clipboard 2> /dev/null", + string, "xclip" ), - ]; + ), + ]; - for (_, command) in clipboards.iter() { - match Command::new("sh").args(&["-c", command.as_str()]).status() { - Ok(s) if s.success() => return Ok(()), - _ => {} - } + for (_, command) in clipboards.iter() { + match Command::new("sh").args(&["-c", command.as_str()]).status() { + Ok(s) if s.success() => return Ok(()), + _ => {} } - let err = Err(JoshutoError::new( - JoshutoErrorKind::ClipboardError, - "Failed to copy to clipboard".to_string(), - )); - return err; } - Ok(()) + let err = Err(JoshutoError::new( + JoshutoErrorKind::ClipboardError, + "Failed to copy to clipboard".to_string(), + )); + return err; } diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs index 9a2b371..87c1b17 100644 --- a/src/commands/key_command.rs +++ b/src/commands/key_command.rs @@ -24,6 +24,8 @@ pub enum KeyCommand { CopyFiles, PasteFiles(IoWorkerOptions), CopyFileName, + CopyFilePath, + CopyDirName, CursorMoveUp(usize), CursorMoveDown(usize), @@ -83,6 +85,8 @@ impl KeyCommand { Self::CopyFiles => "copy_files", Self::PasteFiles(_) => "paste_files", Self::CopyFileName => "copy_filename", + Self::CopyFilePath => "copy_filepath", + Self::CopyDirName => "copy_dirname", Self::CursorMoveUp(_) => "cursor_move_up", Self::CursorMoveDown(_) => "cursor_move_down", @@ -155,6 +159,8 @@ impl KeyCommand { "close_tab" => Ok(Self::CloseTab), "copy_files" => Ok(Self::CopyFiles), "copy_filename" => Ok(Self::CopyFileName), + "copy_filepath" => Ok(Self::CopyFilePath), + "copy_dirname" => Ok(Self::CopyDirName), "cursor_move_home" => Ok(Self::CursorMoveHome), "cursor_move_end" => Ok(Self::CursorMoveEnd), "cursor_move_page_up" => Ok(Self::CursorMovePageUp), @@ -354,6 +360,8 @@ impl AppExecute for KeyCommand { Self::CopyFiles => file_ops::copy(context), Self::PasteFiles(options) => file_ops::paste(context, options.clone()), Self::CopyFileName => file_ops::copy_filename(context), + Self::CopyFilePath => file_ops::copy_filepath(context), + Self::CopyDirName => file_ops::copy_dirname(context), Self::CursorMoveUp(u) => cursor_move::up(context, *u), Self::CursorMoveDown(u) => cursor_move::down(context, *u), |