use std::path::{Path, PathBuf};
use std::process::Command;
use super::i18n::tr;
use super::BigTextApp;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShellAction {
Reveal,
Terminal,
Default,
}
fn folder_of(path: &Path) -> PathBuf {
if path.is_dir() {
return path.to_path_buf();
}
path.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."))
}
#[cfg(windows)]
pub fn command(action: ShellAction, path: &Path) -> Command {
use std::os::windows::process::CommandExt;
let shown = bigtext::file::display_path(path);
match action {
ShellAction::Reveal if path.exists() => {
let mut c = Command::new("explorer.exe");
c.raw_arg(format!("/select,\"{shown}\""));
c
}
ShellAction::Reveal => {
let mut c = Command::new("explorer.exe");
c.arg(bigtext::file::display_path(&folder_of(path)));
c
}
ShellAction::Terminal => {
let mut c = Command::new("cmd.exe");
c.arg("/K")
.current_dir(folder_of(path))
.creation_flags(0x10);
c
}
ShellAction::Default => {
let mut c = Command::new("rundll32.exe");
c.arg("url.dll,FileProtocolHandler").arg(shown);
c
}
}
}
#[cfg(target_os = "macos")]
pub fn command(action: ShellAction, path: &Path) -> Command {
let mut c = Command::new("open");
match action {
ShellAction::Reveal if path.exists() => c.arg("-R").arg(path),
ShellAction::Reveal => c.arg(folder_of(path)),
ShellAction::Terminal => c
.args(["-a", "Terminal"])
.arg(folder_of(path))
.current_dir(folder_of(path)),
ShellAction::Default => c.arg(path),
};
c
}
#[cfg(all(unix, not(target_os = "macos")))]
pub fn command(action: ShellAction, path: &Path) -> Command {
match action {
ShellAction::Reveal => {
let mut c = Command::new("xdg-open");
c.arg(folder_of(path));
c
}
ShellAction::Terminal => {
let term = std::env::var("TERMINAL")
.ok()
.filter(|t| !t.is_empty())
.unwrap_or_else(|| "x-terminal-emulator".into());
let mut c = Command::new(term);
c.current_dir(folder_of(path));
c
}
ShellAction::Default => {
let mut c = Command::new("xdg-open");
c.arg(path);
c
}
}
}
#[cfg(windows)]
pub fn move_to_trash(path: &Path) -> Result<(), String> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::UI::Shell::{
SHFileOperationW, FOF_ALLOWUNDO, FOF_NOCONFIRMATION, FOF_NOERRORUI, FOF_SILENT, FO_DELETE,
SHFILEOPSTRUCTW,
};
let full = std::path::absolute(path).map_err(|e| e.to_string())?;
let mut from: Vec<u16> = std::ffi::OsStr::new(&bigtext::file::display_path(&full))
.encode_wide()
.collect();
from.extend([0, 0]);
let code = unsafe {
let mut op: SHFILEOPSTRUCTW = std::mem::zeroed();
op.wFunc = FO_DELETE as _;
op.pFrom = from.as_ptr();
op.fFlags = (FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI) as _;
let code = SHFileOperationW(&mut op);
if code == 0 && op.fAnyOperationsAborted != 0 {
-1
} else {
code
}
};
match code {
0 => Ok(()),
-1 => Err(tr("The operation was cancelled.").into()),
c => Err(format!(
"{} (0x{c:X})",
tr("Windows could not move it to the Recycle Bin")
)),
}
}
#[cfg(target_os = "macos")]
pub fn move_to_trash(path: &Path) -> Result<(), String> {
let full = std::path::absolute(path).map_err(|e| e.to_string())?;
let p = full
.to_string_lossy()
.replace('\\', "\\\\")
.replace('"', "\\\"");
let out = Command::new("osascript")
.arg("-e")
.arg(format!(
"tell application \"Finder\" to delete POSIX file \"{p}\""
))
.output()
.map_err(|e| e.to_string())?;
if out.status.success() {
Ok(())
} else {
Err(String::from_utf8_lossy(&out.stderr).trim().to_string())
}
}
#[cfg(all(unix, not(target_os = "macos")))]
pub fn move_to_trash(path: &Path) -> Result<(), String> {
let full = std::path::absolute(path).map_err(|e| e.to_string())?;
if Command::new("gio")
.arg("trash")
.arg("--")
.arg(&full)
.output()
.is_ok_and(|o| o.status.success())
{
return Ok(());
}
let data = std::env::var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local").join("share")))
.ok_or("no home folder")?;
let trash = data.join("Trash");
let (files, info) = (trash.join("files"), trash.join("info"));
std::fs::create_dir_all(&files)
.and_then(|_| std::fs::create_dir_all(&info))
.map_err(|e| e.to_string())?;
let name = full
.file_name()
.ok_or("no file name")?
.to_string_lossy()
.into_owned();
let mut n = 0;
let target = loop {
let candidate = if n == 0 {
name.clone()
} else {
format!("{name}.{n}")
};
if !files.join(&candidate).exists() && !info.join(format!("{candidate}.trashinfo")).exists()
{
break candidate;
}
n += 1;
};
let encoded: String = full
.to_string_lossy()
.bytes()
.map(|b| {
if b.is_ascii_alphanumeric() || b"/-_.~".contains(&b) {
(b as char).to_string()
} else {
format!("%{b:02X}")
}
})
.collect();
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let date = bigtext::timestamp::format_key(secs as i64 * 1000, true, true).replace(' ', "T");
std::fs::write(
info.join(format!("{target}.trashinfo")),
format!("[Trash Info]\nPath={encoded}\nDeletionDate={date}\n"),
)
.map_err(|e| e.to_string())?;
std::fs::rename(&full, files.join(&target)).map_err(|e| {
let _ = std::fs::remove_file(info.join(format!("{target}.trashinfo")));
e.to_string()
})
}
pub fn run(action: ShellAction, path: &Path) -> std::io::Result<()> {
let mut child = command(action, path).spawn()?;
bigtext::jobs::spawn("shell", move || {
let _ = child.wait();
});
Ok(())
}
impl BigTextApp {
pub(crate) fn shell_path(&self, id: u64) -> Option<PathBuf> {
let d = self.docs.iter().find(|d| d.id == id)?;
d.unpacked_from.clone().or_else(|| d.path.clone())
}
pub(crate) fn shell_doc(&mut self, id: u64, action: ShellAction) {
let Some(p) = self.shell_path(id) else { return };
self.shell_file(&p, action);
}
pub(crate) fn shell_file(&mut self, path: &Path, action: ShellAction) {
if let Err(e) = run(action, path) {
let what = match action {
ShellAction::Reveal => tr("Could not show the file in its folder"),
ShellAction::Terminal => {
tr("Could not open a terminal (set the TERMINAL variable to yours)")
}
ShellAction::Default => tr("Could not open the file with its default app"),
};
self.error = Some(format!("{what}: {e}"));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn argv(c: &Command) -> (String, Vec<String>) {
(
c.get_program().to_string_lossy().into_owned(),
c.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect(),
)
}
#[test]
fn commands_for_this_system() {
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("a b.log");
std::fs::write(&f, "x").unwrap();
let (prog, args) = argv(&command(ShellAction::Default, &f));
let (rprog, rargs) = argv(&command(ShellAction::Reveal, &f));
let gone = argv(&command(ShellAction::Reveal, &dir.path().join("gone.log")));
let term = command(ShellAction::Terminal, &f);
assert_eq!(term.get_current_dir(), Some(dir.path()));
if cfg!(windows) {
assert_eq!(
(prog.as_str(), args[0].as_str()),
("rundll32.exe", "url.dll,FileProtocolHandler")
);
assert!(args[1].ends_with("a b.log"));
assert_eq!(rprog, "explorer.exe");
assert!(
rargs[0].starts_with("/select,\"") && rargs[0].ends_with("a b.log\""),
"{rargs:?}"
);
assert!(!gone.1[0].starts_with("/select"));
} else if cfg!(target_os = "macos") {
assert_eq!((prog.as_str(), rargs[0].as_str()), ("open", "-R"));
} else {
assert_eq!((prog.as_str(), rprog.as_str()), ("xdg-open", "xdg-open"));
assert_eq!(PathBuf::from(&rargs[0]), dir.path());
}
let _ = gone;
}
}