2025-07-05 14:42:45 -04:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2025-07-22 22:26:48 -04:00
|
|
|
pub fn compute_content_hash(content: &str) -> u64 {
|
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
|
content.hash(&mut hasher);
|
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
hash
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Tab {
|
|
|
|
|
pub content: String,
|
|
|
|
|
pub original_content_hash: u64,
|
|
|
|
|
pub file_path: Option<PathBuf>,
|
|
|
|
|
pub is_modified: bool,
|
|
|
|
|
pub title: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tab {
|
|
|
|
|
pub fn new_empty(tab_number: usize) -> Self {
|
|
|
|
|
let content = String::new();
|
2025-07-22 22:26:48 -04:00
|
|
|
let hash = compute_content_hash(&content);
|
2025-07-05 14:42:45 -04:00
|
|
|
Self {
|
|
|
|
|
original_content_hash: hash,
|
|
|
|
|
content,
|
|
|
|
|
file_path: None,
|
|
|
|
|
is_modified: false,
|
2025-07-15 00:42:01 -04:00
|
|
|
title: format!("new_{tab_number}"),
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_with_file(content: String, file_path: PathBuf) -> Self {
|
|
|
|
|
let title = file_path
|
|
|
|
|
.file_name()
|
|
|
|
|
.and_then(|n| n.to_str())
|
2025-07-22 22:26:48 -04:00
|
|
|
.unwrap_or("UNKNOWN")
|
2025-07-05 14:42:45 -04:00
|
|
|
.to_string();
|
|
|
|
|
|
2025-07-22 22:26:48 -04:00
|
|
|
let hash = compute_content_hash(&content);
|
2025-07-05 14:42:45 -04:00
|
|
|
Self {
|
|
|
|
|
original_content_hash: hash,
|
|
|
|
|
content,
|
|
|
|
|
file_path: Some(file_path),
|
|
|
|
|
is_modified: false,
|
|
|
|
|
title,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_display_title(&self) -> String {
|
|
|
|
|
let modified_indicator = if self.is_modified { "*" } else { "" };
|
|
|
|
|
format!("{}{}", self.title, modified_indicator)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_modified_state(&mut self) {
|
|
|
|
|
if self.title.starts_with("new_") {
|
|
|
|
|
self.is_modified = !self.content.is_empty();
|
|
|
|
|
} else {
|
2025-07-22 22:26:48 -04:00
|
|
|
let current_hash = compute_content_hash(&self.content);
|
|
|
|
|
self.is_modified = current_hash != self.original_content_hash;
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn mark_as_saved(&mut self) {
|
2025-07-22 22:26:48 -04:00
|
|
|
self.original_content_hash = compute_content_hash(&self.content);
|
2025-07-05 14:42:45 -04:00
|
|
|
self.is_modified = false;
|
|
|
|
|
}
|
|
|
|
|
}
|