From 100d62602e83271c168f718c3deea0e289a2e3cb Mon Sep 17 00:00:00 2001 From: Cesar Daniel Date: Tue, 4 Mar 2025 22:19:26 -0500 Subject: [PATCH] feat: new --content option to copy command This change extends the copy command by introducing a new "content" option. When users use cg "content", the command reads the file's contents and copies them to the clipboard instead of just copying file paths or names. --- yazi-config/preset/keymap-default.toml | 1 + yazi-core/src/tab/commands/copy.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index 90f7144c..986179ef 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -98,6 +98,7 @@ keymap = [ { on = [ "c", "d" ], run = "copy dirname", desc = "Copy the directory path" }, { on = [ "c", "f" ], run = "copy filename", desc = "Copy the filename" }, { on = [ "c", "n" ], run = "copy name_without_ext", desc = "Copy the filename without extension" }, + { on = [ "c", "g" ], run = "copy content", desc = "Copy the content" }, # Filter { on = "f", run = "filter --smart", desc = "Filter files" }, diff --git a/yazi-core/src/tab/commands/copy.rs b/yazi-core/src/tab/commands/copy.rs index dc028354..2f82ecd5 100644 --- a/yazi-core/src/tab/commands/copy.rs +++ b/yazi-core/src/tab/commands/copy.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, ffi::{OsStr, OsString}, path::Path}; +use std::{borrow::Cow, ffi::{OsStr, OsString}, fs, path::Path}; use yazi_plugin::CLIPBOARD; use yazi_shared::event::CmdCow; @@ -34,6 +34,13 @@ impl Tab { "dirname" => opt.separator.transform(u.parent().unwrap_or(Path::new(""))), "filename" => opt.separator.transform(u.name()), "name_without_ext" => opt.separator.transform(u.file_stem().unwrap_or_default()), + "content" => match fs::read_to_string(u.as_path()) { + Ok(content) => Cow::Owned(OsString::from(content)), + Err(err) => { + yazi_proxy::AppProxy::notify_error("Error reading content", err); + Cow::Borrowed(OsStr::new("")) + } + }, _ => return, }); if it.peek().is_some() {