Skip to main content

jtool_core/
session.rs

1use crate::error::CoreError;
2use std::env;
3use std::fs;
4use std::path::PathBuf;
5use std::process;
6
7/// Get the path to the current process's session file
8pub 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
14/// Save the most recent notebook for this terminal session
15pub fn save_recent_notebook(notebook_path: &str, server: Option<&str>) -> Result<(), CoreError> {
16    let session_file = get_session_file_path()?;
17
18    // Format: notebook_path|server (or just notebook_path if no server)
19    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
29/// Get the most recent notebook for this terminal session
30/// Returns (notebook_path, optional_server)
31pub 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    // Parse: notebook_path|server or just notebook_path
43    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}