futrix/src/command_history.rs

71 lines
1.6 KiB
Rust
Raw Normal View History

2025-09-28 23:39:13 +03:00
// src/command_history.rs
use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};
use std::collections::VecDeque;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommandEntry {
pub timestamp: DateTime<Utc>,
pub command: String,
pub parameters: serde_json::Value,
pub success: bool,
}
pub struct CommandHistory {
entries: VecDeque<CommandEntry>,
max_size: usize,
}
impl CommandHistory {
pub fn new() -> Self {
Self {
entries: VecDeque::new(),
max_size: 1000, // Keep last 1000 commands
}
}
pub fn add_entry(&mut self, command: String, parameters: serde_json::Value, success: bool) {
let entry = CommandEntry {
timestamp: Utc::now(),
command,
parameters,
success,
};
self.entries.push_back(entry);
// Remove oldest entries if we exceed max size
while self.entries.len() > self.max_size {
self.entries.pop_front();
}
}
pub fn get_recent(&self, count: usize) -> Vec<&CommandEntry> {
let start = if self.entries.len() > count {
self.entries.len() - count
} else {
0
};
self.entries.range(start..).collect()
}
pub fn clear(&mut self) {
self.entries.clear();
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
impl Default for CommandHistory {
fn default() -> Self {
Self::new()
}
}