1use crate::error::CoreError;
2use std::env;
3use std::fs;
4use std::path::PathBuf;
5use std::process;
6
7pub fn get_session_file_path() -> Result<PathBuf, CoreError> {
9 let pid = process::id();
10 let temp_dir = env::temp_dir();
11 Ok(temp_dir.join(format!("jtool-session-{pid}.txt")))
12}
13
14pub fn save_recent_notebook(notebook_path: &str, server: Option<&str>) -> Result<(), CoreError> {
16 let session_file = get_session_file_path()?;
17
18 let content = if let Some(srv) = server {
20 format!("{notebook_path}|{srv}")
21 } else {
22 notebook_path.to_string()
23 };
24
25 fs::write(session_file, content)?;
26 Ok(())
27}
28
29pub fn get_recent_notebook() -> Result<(String, Option<String>), CoreError> {
32 let session_file = get_session_file_path()?;
33
34 if !session_file.exists() {
35 return Err(CoreError::Session(
36 "No recent notebook in this terminal session".to_string(),
37 ));
38 }
39
40 let content = fs::read_to_string(session_file)?;
41
42 if let Some((notebook, server)) = content.split_once('|') {
44 Ok((notebook.to_string(), Some(server.to_string())))
45 } else {
46 Ok((content, None))
47 }
48}