par_term/session/mod.rs
1//! Session state types for save/restore on startup.
2//!
3//! This module provides **automatic session persistence**: save the current window
4//! layout, tabs, and pane splits on clean exit, then restore them on next launch.
5//!
6//! # Relationship to `crate::arrangements`
7//!
8//! par-term has two overlapping session persistence mechanisms:
9//!
10//! | Feature | Module | Trigger | Scope |
11//! |---------|--------|---------|-------|
12//! | Auto session restore | `crate::session` (this module) | Automatic on clean exit / next launch | Last-session state only (single slot) |
13//! | Named arrangements | `crate::arrangements` | User-initiated save/restore via UI | Multiple named snapshots, monitor-aware |
14//!
15//! Both capture window positions, sizes, tab CWDs, and tab titles using similar
16//! serialization patterns (serde JSON via `storage` submodule). The key distinction
17//! is lifecycle: session state is ephemeral (overwritten on each clean exit), while
18//! arrangements are user-named and persist indefinitely.
19//!
20//! # Crash recovery
21//!
22//! Neither of those survives a panic: both are written on the way out, and a
23//! panicking process never gets there. [`crash_guard`] adds a third file,
24//! `crash_session.yaml`, written from a panic hook out of a snapshot the event
25//! loop publishes periodically. It is deliberately a separate file — a process
26//! that has just panicked must not overwrite the good session — and
27//! `WindowManager::restore_session` prefers it when present, then deletes it.
28//! Read that module's header before changing anything here: it is explicit about
29//! which panic classes it covers and which it cannot.
30//!
31//! # Shared types
32//!
33//! The common per-tab fields (`cwd`, `title`, `custom_color`, `user_title`,
34//! `custom_icon`) are defined once in [`par_term_config::snapshot_types::TabSnapshot`]
35//! and are embedded into [`SessionTab`] via `#[serde(flatten)]`. The arrangements
36//! module re-exports the same type directly, eliminating the previous duplication.
37//! Existing YAML session files are fully backward-compatible — all fields remain at
38//! the same nesting level.
39
40pub mod capture;
41pub mod crash_guard;
42pub mod restore;
43pub mod storage;
44
45// Re-export TabSnapshot so session consumers can use `crate::session::TabSnapshot`.
46use crate::pane::SplitDirection;
47pub use par_term_config::snapshot_types::TabSnapshot;
48use serde::{Deserialize, Serialize};
49
50/// Top-level session state: all windows at the time of save
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct SessionState {
53 /// Timestamp when the session was saved (ISO 8601)
54 pub saved_at: String,
55 /// All windows in the session
56 pub windows: Vec<SessionWindow>,
57}
58
59/// A single window in the saved session
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct SessionWindow {
62 /// Window position (x, y) in physical pixels
63 pub position: (i32, i32),
64 /// Window size (width, height) in physical pixels
65 pub size: (u32, u32),
66 /// Tabs in this window
67 pub tabs: Vec<SessionTab>,
68 /// Index of the active tab
69 pub active_tab_index: usize,
70 /// tmux session name to auto-reconnect on restore (if any)
71 #[serde(default, skip_serializing_if = "Option::is_none")]
72 pub tmux_session_name: Option<String>,
73}
74
75/// A single tab in a saved session.
76///
77/// The common tab fields (`cwd`, `title`, `custom_color`, `user_title`,
78/// `custom_icon`) are inherited from [`TabSnapshot`] via `#[serde(flatten)]`
79/// so that the serialized YAML layout is unchanged from before this refactor.
80/// The session-specific field `pane_layout` is appended alongside the flattened
81/// fields in the output.
82#[derive(Debug, Clone, Serialize, Deserialize, Default)]
83pub struct SessionTab {
84 /// Common tab snapshot fields shared with the arrangements module
85 #[serde(flatten)]
86 pub snapshot: TabSnapshot,
87 /// Pane layout tree (None = single pane, use cwd above)
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub pane_layout: Option<SessionPaneNode>,
90}
91
92/// Recursive pane tree node for session persistence
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub enum SessionPaneNode {
95 /// A terminal pane leaf
96 Leaf {
97 /// Working directory of this pane
98 cwd: Option<String>,
99 },
100 /// A split containing two children
101 Split {
102 /// Split direction
103 direction: SplitDirection,
104 /// Split ratio (0.0-1.0)
105 ratio: f32,
106 /// First child (top/left)
107 first: Box<SessionPaneNode>,
108 /// Second child (bottom/right)
109 second: Box<SessionPaneNode>,
110 },
111}