use std::{
fs::{create_dir_all, File},
io::BufWriter,
path::{Path, PathBuf},
};
use log::info;
use crate::error::Error;
#[derive(Debug, Clone)]
pub struct Workspace {
pub root: PathBuf,
}
impl Workspace {
pub fn new(output: &Path, session: &str) -> Result<Self, Error> {
let root = output.join(session);
create_dir_all(&root)?;
info!("session workspace: \"{}\"", root.display());
Ok(Self { root })
}
pub fn path(&self, name: &str) -> PathBuf {
self.root.join(name)
}
pub fn subdir(&self, dir: &str) -> Result<PathBuf, Error> {
let path = self.root.join(dir);
create_dir_all(&path)?;
Ok(path)
}
pub fn create_file(&self, name: &str) -> Result<BufWriter<File>, Error> {
let path = self.path(name);
if let Some(parent) = path.parent() {
create_dir_all(parent)?;
}
let fd = File::create(&path)?;
Ok(BufWriter::new(fd))
}
pub fn open(&self, name: &str) {
let path = self.path(name);
let path = path.to_string_lossy().to_string();
#[cfg(target_os = "linux")]
let result = std::process::Command::new("xdg-open").arg(&path).spawn();
#[cfg(target_os = "macos")]
let result = std::process::Command::new("open").arg(&path).spawn();
#[cfg(target_os = "windows")]
let result = std::process::Command::new("cmd")
.args(["/C", "start", "", &path])
.spawn();
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
let result: std::io::Result<()> = Ok(());
if let Err(e) = result {
log::warn!("failed to open \"{}\": {}", path, e);
}
}
}