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)),
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
}
}
}
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;
}
}