lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! History management system for the enhanced REPL.

#![allow(dead_code, missing_docs)]

use crate::{Result, Error};
use std::collections::VecDeque;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Represents a single history entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry {
    pub command: String,
    pub timestamp: DateTime<Utc>,
    pub session_id: Option<String>,
    pub result: Option<String>,
    pub error: Option<String>,
}

impl HistoryEntry {
    pub fn new(command: String) -> Self {
        Self {
            command,
            timestamp: Utc::now(),
            session_id: None,
            result: None,
            error: None,
        }
    }

    pub fn with_result(mut self, result: String) -> Self {
        self.result = Some(result);
        self
    }

    pub fn with_error(mut self, error: String) -> Self {
        self.error = Some(error);
        self
    }

    pub fn with_session(mut self, session_id: String) -> Self {
        self.session_id = Some(session_id);
        self
    }
}

/// Search functionality for command history
pub struct HistorySearch {
    query: String,
    case_sensitive: bool,
    regex_mode: bool,
}

impl HistorySearch {
    pub fn new(query: String) -> Self {
        Self {
            query,
            case_sensitive: false,
            regex_mode: false,
        }
    }

    pub fn case_sensitive(mut self) -> Self {
        self.case_sensitive = true;
        self
    }

    pub fn regex_mode(mut self) -> Self {
        self.regex_mode = true;
        self
    }

    pub fn matches(&self, entry: &HistoryEntry) -> bool {
        let command = if self.case_sensitive {
            entry.command.as_str()
        } else {
            &entry.command.to_lowercase()
        };

        let query = if self.case_sensitive {
            self.query.as_str()
        } else {
            &self.query.to_lowercase()
        };

        if self.regex_mode {
            // TODO: Implement regex matching
            command.contains(query)
        } else {
            command.contains(query)
        }
    }
}

/// Manages command history with persistence and search capabilities
pub struct HistoryManager {
    entries: VecDeque<HistoryEntry>,
    max_entries: usize,
    history_file: PathBuf,
    current_session_id: String,
    auto_save: bool,
}

impl HistoryManager {
    pub fn new(max_entries: usize) -> Result<Self> {
        let history_file = Self::default_history_file()?;
        let current_session_id = Self::generate_session_id();
        
        let mut manager = Self {
            entries: VecDeque::new(),
            max_entries,
            history_file,
            current_session_id,
            auto_save: true,
        };

        // Load existing history
        if manager.history_file.exists() {
            if let Err(e) = manager.load_from_file() {
                eprintln!("Warning: Failed to load history: {e}");
            }
        }

        Ok(manager)
    }

    pub fn with_file<P: AsRef<Path>>(max_entries: usize, history_file: P) -> Result<Self> {
        let history_file = history_file.as_ref().to_path_buf();
        let current_session_id = Self::generate_session_id();
        
        let mut manager = Self {
            entries: VecDeque::new(),
            max_entries,
            history_file,
            current_session_id,
            auto_save: true,
        };

        // Load existing history
        if manager.history_file.exists() {
            manager.load_from_file()?;
        }

        Ok(manager)
    }

    fn default_history_file() -> Result<PathBuf> {
        let mut path = dirs::home_dir()
            .ok_or_else(|| Error::io_error("Could not determine home directory"))?;
        path.push(".lambdust_history");
        Ok(path)
    }

    fn generate_session_id() -> String {
        let timestamp = Utc::now().timestamp(); format!("session_{timestamp}")
    }

    pub fn add_entry(&mut self, command: String) -> Result<()> {
        if command.trim().is_empty() {
            return Ok(());
        }

        let entry = HistoryEntry::new(command)
            .with_session(self.current_session_id.clone());

        self.entries.push_back(entry);

        // Maintain max entries limit
        while self.entries.len() > self.max_entries {
            self.entries.pop_front();
        }

        if self.auto_save {
            self.save_to_file()?;
        }

        Ok(())
    }

    pub fn add_entry_with_result(&mut self, command: String, result: String) -> Result<()> {
        if command.trim().is_empty() {
            return Ok(());
        }

        let entry = HistoryEntry::new(command)
            .with_session(self.current_session_id.clone())
            .with_result(result);

        self.entries.push_back(entry);

        // Maintain max entries limit
        while self.entries.len() > self.max_entries {
            self.entries.pop_front();
        }

        if self.auto_save {
            self.save_to_file()?;
        }

        Ok(())
    }

    pub fn add_entry_with_error(&mut self, command: String, error: String) -> Result<()> {
        if command.trim().is_empty() {
            return Ok(());
        }

        let entry = HistoryEntry::new(command)
            .with_session(self.current_session_id.clone())
            .with_error(error);

        self.entries.push_back(entry);

        // Maintain max entries limit
        while self.entries.len() > self.max_entries {
            self.entries.pop_front();
        }

        if self.auto_save {
            self.save_to_file()?;
        }

        Ok(())
    }

    pub fn search(&self, query: &str) -> Result<Vec<&HistoryEntry>> {
        let search = HistorySearch::new(query.to_string());
        let matches: Vec<&HistoryEntry> = self.entries
            .iter()
            .filter(|entry| search.matches(entry))
            .collect();

        if matches.is_empty() {
            println!("No matches found for: {query}");
        } else {
            let count = matches.len(); println!("Found {count} matches for: {query}");
            for (i, entry) in matches.iter().enumerate() {
                let index = i + 1;
                let command = &entry.command;
                let timestamp = entry.timestamp.format("%Y-%m-%d %H:%M:%S");
                println!("  {index}: {command} ({timestamp})");
            }
        }

        Ok(matches)
    }

    pub fn reverse_search(&self, query: &str) -> Result<Option<&HistoryEntry>> {
        let search = HistorySearch::new(query.to_string());
        
        // Search from most recent to oldest
        for entry in self.entries.iter().rev() {
            if search.matches(entry) {
                return Ok(Some(entry));
            }
        }

        Ok(None)
    }

    pub fn show_recent(&self, count: usize) -> Result<()> {
        let recent_count = count.min(self.entries.len());
        
        if recent_count == 0 {
            println!("No history entries");
            return Ok(());
        }

        println!("Recent history ({recent_count} entries):");
        
        let start_index = self.entries.len().saturating_sub(recent_count);
        for (i, entry) in self.entries.iter().skip(start_index).enumerate() {
            let display_index = start_index + i + 1;
            let command = &entry.command;
            let timestamp = entry.timestamp.format("%H:%M:%S");
            println!("  {display_index}: {command} ({timestamp})");
        }

        Ok(())
    }

    pub fn get_entry(&self, index: usize) -> Option<&HistoryEntry> {
        if index == 0 || index > self.entries.len() {
            None
        } else {
            self.entries.get(index - 1)
        }
    }

    pub fn replay_session(&self, session_id: &str) -> Result<Vec<String>> {
        let commands: Vec<String> = self.entries
            .iter()
            .filter(|entry| entry.session_id.as_deref() == Some(session_id))
            .map(|entry| entry.command.clone())
            .collect();

        if commands.is_empty() {
            println!("No commands found for session: {session_id}");
        } else {
            let count = commands.len(); println!("Found {count} commands for session: {session_id}");
            for (i, command) in commands.iter().enumerate() {
                println!("  {}: {}", i + 1, command);
            }
        }

        Ok(commands)
    }

    pub fn clear(&mut self) -> Result<()> {
        self.entries.clear();
        if self.auto_save {
            self.save_to_file()?;
        }
        Ok(())
    }

    pub fn save_to_file(&self) -> Result<()> {
        // Create parent directories if they don't exist
        if let Some(parent) = self.history_file.parent() {
            fs::create_dir_all(parent)
                .map_err(|e| Error::io_error(format!("Failed to create history directory: {e}")))?;
        }

        let mut file = File::create(&self.history_file)
            .map_err(|e| Error::io_error(format!("Failed to create history file: {e}")))?;

        for entry in &self.entries {
            let json = serde_json::to_string(entry)
                .map_err(|e| Error::io_error(format!("Failed to serialize history entry: {e}")))?;
            writeln!(file, "{json}")
                .map_err(|e| Error::io_error(format!("Failed to write history entry: {e}")))?;
        }

        Ok(())
    }

    pub fn load_from_file(&mut self) -> Result<()> {
        let file = File::open(&self.history_file)
            .map_err(|e| Error::io_error(format!("Failed to open history file: {e}")))?;

        let reader = BufReader::new(file);
        self.entries.clear();

        for line in reader.lines() {
            let line = line
                .map_err(|e| Error::io_error(format!("Failed to read history line: {e}")))?;
            
            if line.trim().is_empty() {
                continue;
            }

            let entry: HistoryEntry = serde_json::from_str(&line)
                .map_err(|e| Error::io_error(format!("Failed to parse history entry: {e}")))?;
            
            self.entries.push_back(entry);
        }

        // Maintain max entries limit
        while self.entries.len() > self.max_entries {
            self.entries.pop_front();
        }

        Ok(())
    }

    pub fn export_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let mut file = File::create(path.as_ref())
            .map_err(|e| Error::io_error(format!("Failed to create export file: {e}")))?;

        writeln!(file, "# Lambdust REPL History Export")?;
        writeln!(file, "# Generated at: {}", Utc::now().format("%Y-%m-%d %H:%M:%S UTC"))?;
        writeln!(file, "# Total entries: {}", self.entries.len())?;
        writeln!(file)?;

        for (i, entry) in self.entries.iter().enumerate() {
            writeln!(file, "# Entry {}: {}", i + 1, entry.timestamp.format("%Y-%m-%d %H:%M:%S"))?;
            writeln!(file, "{}", entry.command)?;
            if let Some(ref result) = entry.result {
                writeln!(file, "# Result: {result}")?;
            }
            if let Some(ref error) = entry.error {
                writeln!(file, "# Error: {error}")?;
            }
            writeln!(file)?;
        }

        Ok(())
    }

    pub fn get_stats(&self) -> HistoryStats {
        let mut command_counts = std::collections::HashMap::new();
        let mut session_counts = std::collections::HashMap::new();
        let mut error_count = 0;

        for entry in &self.entries {
            // Count command prefixes (first word)
            if let Some(first_word) = entry.command.split_whitespace().next() {
                *command_counts.entry(first_word.to_string()).or_insert(0) += 1;
            }

            // Count sessions
            if let Some(ref session) = entry.session_id {
                *session_counts.entry(session.clone()).or_insert(0) += 1;
            }

            // Count errors
            if entry.error.is_some() {
                error_count += 1;
            }
        }

        HistoryStats {
            total_entries: self.entries.len(),
            error_count,
            session_count: session_counts.len(),
            most_used_commands: {
                let mut commands: Vec<_> = command_counts.into_iter().collect();
                commands.sort_by(|a, b| b.1.cmp(&a.1));
                commands.into_iter().take(10).collect()
            },
        }
    }

    pub fn set_auto_save(&mut self, auto_save: bool) {
        self.auto_save = auto_save;
    }

    pub fn current_session_id(&self) -> &str {
        &self.current_session_id
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// Statistics about the command history
#[derive(Debug)]
pub struct HistoryStats {
    pub total_entries: usize,
    pub error_count: usize,
    pub session_count: usize,
    pub most_used_commands: Vec<(String, usize)>,
}

impl HistoryStats {
    pub fn print(&self) {
        println!("History Statistics:");
        println!("  Total entries: {}", self.total_entries);
        println!("  Errors: {}", self.error_count);
        println!("  Sessions: {}", self.session_count);
        
        if !self.most_used_commands.is_empty() {
            println!("  Most used commands:");
            for (command, count) in &self.most_used_commands {
                println!("    {command}: {count} times");
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_history_manager_basic() -> Result<()> {
        let dir = tempdir().unwrap();
        let history_file = dir.path().join("test_history");
        
        let mut manager = HistoryManager::with_file(10, &history_file)?;
        
        // Test adding entries
        manager.add_entry("(+ 1 2)".to_string())?;
        manager.add_entry("(define x 42)".to_string())?;
        
        assert_eq!(manager.len(), 2);
        
        // Test search
        let matches = manager.search("+")?;
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].command, "(+ 1 2)");
        
        // Test recent history
        manager.show_recent(5)?;
        
        Ok(())
    }

    #[test]
    fn test_history_entry() {
        let entry = HistoryEntry::new("test command".to_string())
            .with_result("42".to_string())
            .with_session("test_session".to_string());
        
        assert_eq!(entry.command, "test command");
        assert_eq!(entry.result, Some("42".to_string()));
        assert_eq!(entry.session_id, Some("test_session".to_string()));
        assert!(entry.error.is_none());
    }

    #[test]
    fn test_history_search() {
        let entry1 = HistoryEntry::new("(+ 1 2)".to_string());
        let entry2 = HistoryEntry::new("(define x 42)".to_string());
        
        let search = HistorySearch::new("+".to_string());
        assert!(search.matches(&entry1));
        assert!(!search.matches(&entry2));
        
        let search = HistorySearch::new("define".to_string());
        assert!(!search.matches(&entry1));
        assert!(search.matches(&entry2));
    }
}