use crate::error::CoreError;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;
pub fn get_session_file_path() -> Result<PathBuf, CoreError> {
let pid = process::id();
let temp_dir = env::temp_dir();
Ok(temp_dir.join(format!("jtool-session-{pid}.txt")))
}
pub fn save_recent_notebook(notebook_path: &str, server: Option<&str>) -> Result<(), CoreError> {
let session_file = get_session_file_path()?;
let content = if let Some(srv) = server {
format!("{notebook_path}|{srv}")
} else {
notebook_path.to_string()
};
fs::write(session_file, content)?;
Ok(())
}
pub fn get_recent_notebook() -> Result<(String, Option<String>), CoreError> {
let session_file = get_session_file_path()?;
if !session_file.exists() {
return Err(CoreError::Session(
"No recent notebook in this terminal session".to_string(),
));
}
let content = fs::read_to_string(session_file)?;
if let Some((notebook, server)) = content.split_once('|') {
Ok((notebook.to_string(), Some(server.to_string())))
} else {
Ok((content, None))
}
}