diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-11-17 17:39:33 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-11-17 17:39:33 +0800 |
| commit | 5055b1081be945f9c4191d0a83330683f6d151d3 (patch) | |
| tree | b3e043331ebfaf4628b6bb05f2c5d93c9d39637b /src/data/ipaddress_history.rs | |
| parent | af034c91f9405816cd08247b5e773595995cc46d (diff) | |
Track recent IP addresses in direct operations
Add IP address history functionality to store and retrieve recently used
IP addresses when performing direct operations. Maintains a history file
with up to 8 most recent IPs for quick access.
Diffstat (limited to 'src/data/ipaddress_history.rs')
| -rw-r--r-- | src/data/ipaddress_history.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/data/ipaddress_history.rs b/src/data/ipaddress_history.rs new file mode 100644 index 0000000..3d2bb5b --- /dev/null +++ b/src/data/ipaddress_history.rs @@ -0,0 +1,34 @@ +use just_enough_vcs::vcs::current::current_doc_dir; + +const IP_HISTORY_NAME: &str = "ip_history.txt"; + +pub struct IpAddressHistory { + pub recent_ip_address: Vec<String>, +} + +pub async fn get_recent_ip_address() -> Vec<String> { + if let Some(local) = current_doc_dir() { + let path = local.join(IP_HISTORY_NAME); + match tokio::fs::read_to_string(path).await { + Ok(content) => content.lines().map(String::from).collect(), + Err(_) => Vec::new(), + } + } else { + Vec::new() + } +} + +pub async fn insert_recent_ip_address(ip: impl Into<String>) { + let ip = ip.into(); + if let Some(local) = current_doc_dir() { + let path = local.join(IP_HISTORY_NAME); + let mut recent_ips = get_recent_ip_address().await; + recent_ips.retain(|existing_ip| existing_ip != &ip); + recent_ips.insert(0, ip); + if recent_ips.len() > 8 { + recent_ips.truncate(8); + } + let content = recent_ips.join("\n"); + let _ = tokio::fs::write(path, content).await; + } +} |
