detached-shell 0.1.4

Noras.tech's minimalist detachable shell solution · zero configuration · not a complex multiplexer, just persistent sessions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use crate::error::{NdsError, Result};
use crate::session::Session;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SessionEvent {
    Created,
    Attached,
    Detached,
    Killed,
    Crashed,
    Renamed { from: Option<String>, to: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry {
    pub session_id: String,
    pub session_name: Option<String>,
    pub event: SessionEvent,
    pub timestamp: DateTime<Utc>,
    pub pid: i32,
    pub shell: String,
    pub working_dir: String,
    pub duration_seconds: Option<i64>, // For Killed/Crashed events
}

// Individual session history stored in separate files
#[derive(Debug, Serialize, Deserialize)]
pub struct SessionHistoryFile {
    pub session_id: String,
    pub created_at: DateTime<Utc>,
    pub entries: Vec<HistoryEntry>,
}

// Main history manager
pub struct SessionHistory;

impl SessionHistory {
    // Directory structure: ~/.nds/history/active/ and ~/.nds/history/archived/
    pub fn history_dir() -> Result<PathBuf> {
        let dir = if let Ok(nds_home) = std::env::var("NDS_HOME") {
            PathBuf::from(nds_home).join("history")
        } else {
            directories::BaseDirs::new()
                .ok_or_else(|| {
                    NdsError::DirectoryCreationError("Could not find home directory".to_string())
                })?
                .home_dir()
                .join(".nds")
                .join("history")
        };

        if !dir.exists() {
            fs::create_dir_all(&dir)
                .map_err(|e| NdsError::DirectoryCreationError(e.to_string()))?;
        }

        Ok(dir)
    }

    pub fn active_history_dir() -> Result<PathBuf> {
        let dir = Self::history_dir()?.join("active");
        if !dir.exists() {
            fs::create_dir_all(&dir)
                .map_err(|e| NdsError::DirectoryCreationError(e.to_string()))?;
        }
        Ok(dir)
    }

    pub fn archived_history_dir() -> Result<PathBuf> {
        let dir = Self::history_dir()?.join("archived");
        if !dir.exists() {
            fs::create_dir_all(&dir)
                .map_err(|e| NdsError::DirectoryCreationError(e.to_string()))?;
        }
        Ok(dir)
    }

    // Get history file path for a session
    fn session_history_path(session_id: &str, archived: bool) -> Result<PathBuf> {
        let dir = if archived {
            Self::archived_history_dir()?
        } else {
            Self::active_history_dir()?
        };
        Ok(dir.join(format!("{}.json", session_id)))
    }

    // Load history for a specific session
    pub fn load_session_history(session_id: &str) -> Result<SessionHistoryFile> {
        // Try active first, then archived
        let active_path = Self::session_history_path(session_id, false)?;
        let archived_path = Self::session_history_path(session_id, true)?;

        let path = if active_path.exists() {
            active_path
        } else if archived_path.exists() {
            archived_path
        } else {
            // Create new history file for this session
            let history = SessionHistoryFile {
                session_id: session_id.to_string(),
                created_at: Utc::now(),
                entries: Vec::new(),
            };
            let json = serde_json::to_string_pretty(&history)?;
            fs::write(&active_path, json)?;
            return Ok(history);
        };

        let content = fs::read_to_string(&path)?;
        let history: SessionHistoryFile = serde_json::from_str(&content)?;
        Ok(history)
    }

    // Save history for a specific session
    fn save_session_history(history: &SessionHistoryFile, archived: bool) -> Result<()> {
        let path = Self::session_history_path(&history.session_id, archived)?;
        let json = serde_json::to_string_pretty(history)?;
        fs::write(path, json)?;
        Ok(())
    }

    // Add an entry to a session's history
    fn add_entry_to_session(session_id: &str, entry: HistoryEntry) -> Result<()> {
        let mut history = Self::load_session_history(session_id)?;
        history.entries.push(entry);

        // Determine if this should be archived (session ended)
        let should_archive = history
            .entries
            .iter()
            .any(|e| matches!(e.event, SessionEvent::Killed | SessionEvent::Crashed));

        Self::save_session_history(&history, should_archive)?;

        // If archived, remove from active directory
        if should_archive {
            let active_path = Self::session_history_path(session_id, false)?;
            if active_path.exists() {
                let _ = fs::remove_file(active_path);
            }
        }

        Ok(())
    }

    // Record session events
    pub fn record_session_created(session: &Session) -> Result<()> {
        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: session.name.clone(),
            event: SessionEvent::Created,
            timestamp: session.created_at,
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: None,
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    pub fn record_session_attached(session: &Session) -> Result<()> {
        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: session.name.clone(),
            event: SessionEvent::Attached,
            timestamp: Utc::now(),
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: None,
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    pub fn record_session_detached(session: &Session) -> Result<()> {
        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: session.name.clone(),
            event: SessionEvent::Detached,
            timestamp: Utc::now(),
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: None,
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    pub fn record_session_killed(session: &Session) -> Result<()> {
        let history = Self::load_session_history(&session.id)?;
        let duration = if let Some(first_entry) = history.entries.first() {
            (Utc::now() - first_entry.timestamp).num_seconds()
        } else {
            (Utc::now() - session.created_at).num_seconds()
        };

        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: session.name.clone(),
            event: SessionEvent::Killed,
            timestamp: Utc::now(),
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: Some(duration),
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    pub fn record_session_crashed(session: &Session) -> Result<()> {
        let history = Self::load_session_history(&session.id)?;
        let duration = if let Some(first_entry) = history.entries.first() {
            (Utc::now() - first_entry.timestamp).num_seconds()
        } else {
            (Utc::now() - session.created_at).num_seconds()
        };

        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: session.name.clone(),
            event: SessionEvent::Crashed,
            timestamp: Utc::now(),
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: Some(duration),
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    pub fn record_session_renamed(
        session: &Session,
        old_name: Option<String>,
        new_name: String,
    ) -> Result<()> {
        let entry = HistoryEntry {
            session_id: session.id.clone(),
            session_name: Some(new_name.clone()),
            event: SessionEvent::Renamed {
                from: old_name,
                to: new_name,
            },
            timestamp: Utc::now(),
            pid: session.pid,
            shell: session.shell.clone(),
            working_dir: session.working_dir.clone(),
            duration_seconds: None,
        };
        Self::add_entry_to_session(&session.id, entry)
    }

    // Get all history entries (from all sessions)
    pub fn load_all_history(
        include_archived: bool,
        limit: Option<usize>,
    ) -> Result<Vec<HistoryEntry>> {
        let mut all_entries = Vec::new();

        // Load from active sessions
        let active_dir = Self::active_history_dir()?;
        if active_dir.exists() {
            for entry in fs::read_dir(active_dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("json") {
                    if let Ok(content) = fs::read_to_string(&path) {
                        if let Ok(history) = serde_json::from_str::<SessionHistoryFile>(&content) {
                            all_entries.extend(history.entries);
                        }
                    }
                }
            }
        }

        // Load from archived sessions if requested
        if include_archived {
            let archived_dir = Self::archived_history_dir()?;
            if archived_dir.exists() {
                for entry in fs::read_dir(archived_dir)? {
                    let entry = entry?;
                    let path = entry.path();
                    if path.extension().and_then(|s| s.to_str()) == Some("json") {
                        if let Ok(content) = fs::read_to_string(&path) {
                            if let Ok(history) =
                                serde_json::from_str::<SessionHistoryFile>(&content)
                            {
                                all_entries.extend(history.entries);
                            }
                        }
                    }
                }
            }
        }

        // Sort by timestamp (newest first)
        all_entries.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

        // Apply limit if specified
        if let Some(limit) = limit {
            all_entries.truncate(limit);
        }

        Ok(all_entries)
    }

    // Get history for a specific session
    pub fn get_session_history(session_id: &str) -> Result<Vec<HistoryEntry>> {
        let history = Self::load_session_history(session_id)?;
        Ok(history.entries)
    }

    // Clean up old archived history (older than specified days)
    pub fn cleanup_old_history(days_to_keep: i64) -> Result<usize> {
        let archived_dir = Self::archived_history_dir()?;
        let cutoff = Utc::now() - Duration::days(days_to_keep);
        let mut removed_count = 0;

        if archived_dir.exists() {
            for entry in fs::read_dir(archived_dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("json") {
                    if let Ok(content) = fs::read_to_string(&path) {
                        if let Ok(history) = serde_json::from_str::<SessionHistoryFile>(&content) {
                            // Check if all entries are older than cutoff
                            if history.entries.iter().all(|e| e.timestamp < cutoff) {
                                fs::remove_file(&path)?;
                                removed_count += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok(removed_count)
    }

    pub fn format_duration(seconds: i64) -> String {
        let hours = seconds / 3600;
        let minutes = (seconds % 3600) / 60;
        let secs = seconds % 60;

        if hours > 0 {
            format!("{}h {}m {}s", hours, minutes, secs)
        } else if minutes > 0 {
            format!("{}m {}s", minutes, secs)
        } else {
            format!("{}s", secs)
        }
    }

    // Migrate from old single-file format to new per-session format
    pub fn migrate_from_single_file() -> Result<()> {
        let old_file = directories::BaseDirs::new()
            .ok_or_else(|| {
                NdsError::DirectoryCreationError("Could not find home directory".to_string())
            })?
            .home_dir()
            .join(".nds")
            .join("history.json");

        if !old_file.exists() {
            return Ok(()); // Nothing to migrate
        }

        // Read old format
        let content = fs::read_to_string(&old_file)?;
        if let Ok(old_history) = serde_json::from_str::<crate::history::SessionHistory>(&content) {
            // Group entries by session ID
            let mut sessions: HashMap<String, Vec<HistoryEntry>> = HashMap::new();

            for old_entry in old_history.entries {
                let entry = HistoryEntry {
                    session_id: old_entry.session_id.clone(),
                    session_name: old_entry.session_name,
                    event: match old_entry.event {
                        crate::history::SessionEvent::Created => SessionEvent::Created,
                        crate::history::SessionEvent::Attached => SessionEvent::Attached,
                        crate::history::SessionEvent::Detached => SessionEvent::Detached,
                        crate::history::SessionEvent::Killed => SessionEvent::Killed,
                        crate::history::SessionEvent::Crashed => SessionEvent::Crashed,
                        crate::history::SessionEvent::Renamed { from, to } => {
                            SessionEvent::Renamed { from, to }
                        }
                    },
                    timestamp: old_entry.timestamp,
                    pid: old_entry.pid,
                    shell: old_entry.shell,
                    working_dir: old_entry.working_dir,
                    duration_seconds: old_entry.duration_seconds,
                };

                sessions
                    .entry(old_entry.session_id.clone())
                    .or_insert_with(Vec::new)
                    .push(entry);
            }

            // Save each session to its own file
            for (session_id, entries) in sessions {
                let is_terminated = entries
                    .iter()
                    .any(|e| matches!(e.event, SessionEvent::Killed | SessionEvent::Crashed));

                let created_at = entries
                    .first()
                    .map(|e| e.timestamp)
                    .unwrap_or_else(Utc::now);

                let history_file = SessionHistoryFile {
                    session_id: session_id.clone(),
                    created_at,
                    entries,
                };

                Self::save_session_history(&history_file, is_terminated)?;
            }

            // Rename old file to .backup
            let backup_path = old_file.with_extension("json.backup");
            fs::rename(old_file, backup_path)?;
        }

        Ok(())
    }
}