From 1b3dc37623dbe947596300b811be4f5a8cdb18e4 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Wed, 1 Mar 2023 22:40:57 +0100 Subject: [PATCH] show unsaved changes --- src/editor.rs | 10 ++++++++++ src/main.rs | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/editor.rs b/src/editor.rs index 31b847c..6dedd7b 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -25,6 +25,7 @@ pub struct Editor { clipboard: Clipboard, path: Option, active: bool, + unsaved_changes: bool, } #[derive(Debug)] @@ -48,6 +49,7 @@ impl Editor { clipboard, path: Some(path), active: false, + unsaved_changes: false, }; this.find_lines(); this @@ -63,6 +65,7 @@ impl Editor { clipboard, path: None, active: false, + unsaved_changes: true, } } @@ -70,6 +73,10 @@ impl Editor { self.path.as_ref().map_or("untitled", |s| s) } + pub fn has_unsaved_changes(&self) -> bool { + self.unsaved_changes + } + pub fn open(&mut self) { self.active = true; @@ -258,6 +265,7 @@ impl Editor { } fn insert_char(&mut self, ch: char) { + self.unsaved_changes = true; // eprintln!("inserting {ch} at {}", self.index()); self.text.insert(self.char_index(), ch); self.find_lines(); @@ -305,6 +313,7 @@ impl Editor { } fn paste(&mut self) { + self.unsaved_changes = true; let cursor = self.char_index(); self.text.insert_str(cursor, &self.clipboard.get()); self.find_lines(); @@ -352,6 +361,7 @@ impl Editor { } let mut file = File::create(self.path.as_ref().unwrap()).unwrap(); file.write_all(self.text.as_bytes()).unwrap(); + self.unsaved_changes = false; } } diff --git a/src/main.rs b/src/main.rs index 8c98bde..ddfac64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,12 @@ impl Navigator { if Some(index) == self.selected { print!("{}{}", color::Fg(color::Black), color::Bg(color::White)); } - print!("{}{}", Goto(2, index as u16 + 2), editor.name()); + print!( + "{}{}{}", + Goto(2, index as u16 + 2), + editor.has_unsaved_changes().then_some("*").unwrap_or(" "), + editor.name() + ); print!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset)); }