feat: copy file path, with multi-selection support (#72)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-19 21:07:29 +08:00 committed by GitHub
parent 970d36b069
commit 2180f08289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 12 deletions

View file

@ -106,7 +106,7 @@ impl Executor {
}
"create" => cx.manager.create(),
"rename" => cx.manager.rename(),
"copy" => cx.manager.copy(),
"copy" => cx.manager.copy(exec.args.get(0).map(|s| s.as_str()).unwrap_or("")),
"shell" => cx.manager.active().shell(
exec.args.get(0).map(|e| e.as_str()).unwrap_or(""),
exec.named.contains_key("block"),

View file

@ -1,4 +1,4 @@
use std::process::Stdio;
use std::{ffi::OsStr, os::unix::prelude::OsStrExt, process::Stdio};
use anyhow::{bail, Result};
use tokio::{io::AsyncWriteExt, process::Command};
@ -14,12 +14,12 @@ pub async fn clipboard_get() -> Result<String> {
bail!("failed to get clipboard")
}
pub async fn clipboard_set(s: &str) -> Result<()> {
pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
for cmd in &["pbcopy", "wl-copy"] {
let mut child =
Command::new(cmd).stdin(Stdio::piped()).stdout(Stdio::null()).kill_on_drop(true).spawn()?;
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(s.as_bytes()).await?;
stdin.write_all(s.as_ref().as_bytes()).await?;
}
if child.wait().await?.success() {
return Ok(());

View file

@ -246,14 +246,13 @@ impl Input {
self.handle_op(self.snap().cursor, true);
}
let str =
futures::executor::block_on(async { external::clipboard_get().await }).unwrap_or_default();
if str.is_empty() {
let s = futures::executor::block_on(external::clipboard_get()).unwrap_or_default();
if s.is_empty() {
return false;
}
self.insert(!before);
for c in str.chars() {
for c in s.chars() {
self.type_(c);
}
self.escape();
@ -274,7 +273,7 @@ impl Input {
let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>();
if cut {
futures::executor::block_on(async { external::clipboard_set(&drain).await.ok() });
futures::executor::block_on(external::clipboard_set(&drain)).ok();
}
snap.op = InputOp::None;
@ -287,7 +286,7 @@ impl Input {
let yanked = &snap.value[start.unwrap()..end.unwrap()];
snap.op = InputOp::None;
futures::executor::block_on(async { external::clipboard_set(yanked).await.ok() });
futures::executor::block_on(external::clipboard_set(yanked)).ok();
}
};

View file

@ -1,4 +1,4 @@
use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, ffi::OsStr, io::{stdout, BufWriter, Write}, mem, path::{Path, PathBuf}};
use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, ffi::{OsStr, OsString}, io::{stdout, BufWriter, Write}, mem, path::{Path, PathBuf}};
use anyhow::{anyhow, bail, Error, Result};
use config::{BOOT, OPEN};
@ -319,7 +319,25 @@ impl Manager {
Ok(())
}
pub fn copy(&self) -> bool { false }
pub fn copy(&self, type_: &str) -> bool {
let mut s = OsString::new();
let mut it = self.selected().into_iter().peekable();
while let Some(f) = it.next() {
s.push(match type_ {
"path" => f.path.as_os_str(),
"dirname" => f.path.parent().map_or(OsStr::new(""), |p| p.as_os_str()),
"filename" => f.path.file_name().unwrap_or(OsStr::new("")),
"name_without_ext" => f.path.file_stem().unwrap_or(OsStr::new("")),
_ => return false,
});
if it.peek().is_some() {
s.push("\n");
}
}
futures::executor::block_on(external::clipboard_set(s)).ok();
false
}
pub fn update_read(&mut self, op: FilesOp) -> bool {
let path = op.path();