use crate::core::EditorEngine;
#[derive(Debug, Clone)]
pub struct EditorTabState {
pub id: usize,
pub file_name: String,
pub is_modified: bool,
pub engine: EditorEngine,
}
impl EditorTabState {
pub fn new(id: usize, file_name: impl Into<String>) -> Self {
let file_name = file_name.into();
Self {
id,
file_name: file_name.clone(),
is_modified: false,
engine: EditorEngine::new(id),
}
}
pub fn title(&self) -> String {
if self.is_modified {
format!("{}*", self.file_name)
} else {
self.file_name.clone()
}
}
#[allow(dead_code)]
pub fn mark_modified(&mut self) {
self.is_modified = true;
}
#[allow(dead_code)]
pub fn mark_saved(&mut self) {
self.is_modified = false;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_tab() {
let tab = EditorTabState::new(0, "test.md");
assert_eq!(tab.id, 0);
assert_eq!(tab.file_name, "test.md");
assert!(!tab.is_modified);
}
#[test]
fn test_title_without_modification() {
let tab = EditorTabState::new(0, "test.md");
assert_eq!(tab.title(), "test.md");
}
#[test]
fn test_title_with_modification() {
let mut tab = EditorTabState::new(0, "test.md");
tab.mark_modified();
assert_eq!(tab.title(), "test.md*");
}
#[test]
fn test_mark_modified_and_saved() {
let mut tab = EditorTabState::new(0, "test.md");
assert!(!tab.is_modified);
tab.mark_modified();
assert!(tab.is_modified);
tab.mark_saved();
assert!(!tab.is_modified);
}
}