| 42 | } |
| 43 | |
| 44 | pub fn get_diff(&self, path: &str, current_content: &str) -> Option<Vec<DiffLine>> { |
| 45 | let snap = self.snapshots.get(path)?; |
| 46 | let old_lines: Vec<&str> = snap.content.lines().collect(); |
| 47 | let new_lines: Vec<&str> = current_content.lines().collect(); |
| 48 | |
| 49 | let mut diff = Vec::new(); |
| 50 | let max_len = old_lines.len().max(new_lines.len()); |
| 51 | for i in 0..max_len { |
| 52 | match (old_lines.get(i), new_lines.get(i)) { |
| 53 | (Some(o), Some(n)) if o == n => diff.push(DiffLine::Unchanged(o.to_string())), |
| 54 | (Some(o), Some(n)) => { |
| 55 | diff.push(DiffLine::Removed(o.to_string())); |
| 56 | diff.push(DiffLine::Added(n.to_string())); |
| 57 | } |
| 58 | (Some(o), None) => diff.push(DiffLine::Removed(o.to_string())), |
| 59 | (None, Some(n)) => diff.push(DiffLine::Added(n.to_string())), |
| 60 | (None, None) => {} |
| 61 | } |
| 62 | } |
| 63 | Some(diff) |
| 64 | } |
| 65 | |
| 66 | pub fn save_all_to_disk(&self) { |
| 67 | let mut path = dirs::data_local_dir().unwrap_or_else(|| PathBuf::from(".")); |