cleanup, add message when saving file

This commit is contained in:
Crispy 2023-03-12 20:44:37 +01:00
parent 19742c38df
commit 2bc0cde14b
2 changed files with 6 additions and 20 deletions

View file

@ -58,7 +58,7 @@ impl Editor {
})
}
pub fn new_empty(clipboard: Clipboard) -> Self {
pub fn new(clipboard: Clipboard, path: Option<PathBuf>) -> Self {
Editor {
text: String::new(),
lines: vec![0..0],
@ -66,22 +66,7 @@ impl Editor {
cursor: Cursor { line: 0, column: 0 },
marker: None,
clipboard,
path: None,
active: false,
unsaved_changes: true,
message: None,
}
}
pub fn new_named(clipboard: Clipboard, path: PathBuf) -> Self {
Editor {
text: String::new(),
lines: vec![0..0],
scroll: 0,
cursor: Cursor { line: 0, column: 0 },
marker: None,
clipboard,
path: Some(path),
path,
active: false,
unsaved_changes: true,
message: None,
@ -468,6 +453,7 @@ impl Editor {
if let Some(path) = &self.path {
match File::create(path) {
Ok(mut file) => {
self.message(format!("Saved file as '{}'", path.display()));
file.write_all(self.text.as_bytes()).unwrap();
self.unsaved_changes = false;
}

View file

@ -53,11 +53,11 @@ impl Navigator {
editors.push(editor);
}
} else {
editors.push(Editor::new_named(clipboard.clone(), arg));
editors.push(Editor::new(clipboard.clone(), Some(arg)));
}
}
if args.is_empty() {
editors.push(Editor::new_empty(clipboard.clone()));
editors.push(Editor::new(clipboard.clone(), None));
}
let immediate_open = editors.len() == 1;
Self {
@ -200,7 +200,7 @@ impl Navigator {
fn new_editor(&mut self) {
self.selected = self.editors.len();
self.editors.push(Editor::new_empty(self.clipboard.clone()));
self.editors.push(Editor::new(self.clipboard.clone(), None));
self.open_selected();
}