Indicate when file has changed on disk

This commit is contained in:
2025-07-09 13:29:22 +02:00
parent 38d26f0028
commit 3a2f058456
8 changed files with 467 additions and 42 deletions

View File

@ -1,16 +1,23 @@
use std::{
fs,
io::Read,
path::PathBuf,
sync::{Arc, mpsc},
thread::JoinHandle,
time::{Duration, Instant},
};
use crate::{file_editor::FileEditor, folder::Folder, preferences::Preferences, util::GuiSender};
use crate::{
file_editor::{FileEditor, SaveStatus},
folder::Folder,
preferences::Preferences,
util::{GuiSender, file_mtime, log_error},
};
use egui::{
Align, Button, Context, FontData, FontDefinitions, Image, Key, Modifiers, PointerButton,
RichText, ScrollArea, Widget, include_image,
};
use eyre::eyre;
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
@ -40,7 +47,7 @@ pub struct Jobs {
}
impl Jobs {
fn start(&mut self, ctx: &Context, job: impl FnOnce() -> Option<Action> + Send + 'static) {
pub fn start(&mut self, ctx: &Context, job: impl FnOnce() -> Option<Action> + Send + 'static) {
let ctx = ctx.clone();
let actions_tx = self.actions_tx.clone();
self.handles.push(std::thread::spawn(move || {
@ -73,9 +80,21 @@ impl Tab {
}
}
pub fn is_dirty(&self) -> bool {
pub fn notice_symbol(&self) -> Option<&'static str> {
match self {
Tab::File(file_editor) => file_editor.is_dirty,
Tab::File(file_editor) => match file_editor.save_status() {
SaveStatus::Synced => None,
SaveStatus::NoFile => Some("?"),
SaveStatus::FileOutdated => Some("*"),
SaveStatus::BufferOutdated => Some("!"),
SaveStatus::Desynced => Some("!!"),
},
}
}
pub fn save(&mut self, ctx: &Context, jobs: &mut Jobs) {
match self {
Tab::File(file_editor) => file_editor.save(ctx, jobs),
}
}
}
@ -268,11 +287,18 @@ impl eframe::App for App {
self.jobs.start(ui.ctx(), move || {
let file_path = rfd::FileDialog::new().pick_file()?;
let text = fs::read_to_string(&file_path)
let mut file = fs::File::open(&file_path)
.inspect_err(|e| log::error!("Failed to open {file_path:?}: {e}"))
.ok()?;
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
let mut text = String::new();
file.read_to_string(&mut text)
.inspect_err(|e| log::error!("Failed to read {file_path:?}: {e}"))
.ok()?;
let editor = FileEditor::from_file(file_path, &text);
let editor = FileEditor::from_file(file_path, &text, mtime);
Some(Action::OpenFile(editor))
});
}
@ -364,8 +390,8 @@ impl eframe::App for App {
let selected = self.open_tab_index == Some(i);
let mut button = Button::new(tab.title()).selected(selected);
if tab.is_dirty() {
button = button.right_text(RichText::new("*").strong())
if let Some(symbol) = tab.notice_symbol() {
button = button.right_text(RichText::new(symbol).strong())
}
let response = ui.add(button);
@ -396,13 +422,22 @@ impl eframe::App for App {
if let Some(file_path) = response.open_file {
let file_path = file_path.to_owned();
self.jobs.start(ui.ctx(), move || {
let text = fs::read_to_string(&file_path)
let mut file = fs::File::open(&file_path)
.inspect_err(|e| {
log::error!("Failed to open {file_path:?}: {e}")
})
.ok()?;
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
let mut text = String::new();
file.read_to_string(&mut text)
.inspect_err(|e| {
log::error!("Failed to read {file_path:?}: {e}")
})
.ok()?;
let editor = FileEditor::from_file(file_path, &text);
let editor = FileEditor::from_file(file_path, &text, mtime);
Some(Action::OpenFile(editor))
});
}
@ -448,29 +483,13 @@ impl App {
}
fn save_active_tab(&mut self, ctx: &Context) {
let open_file = self
let open_tab = self
.open_tab_index
.and_then(|i| self.tabs.get_mut(i))
.map(|(id, tab)| match tab {
Tab::File(file_editor) => (*id, file_editor),
})
.and_then(|(_, file_editor)| {
file_editor
.path()
.map(ToOwned::to_owned)
.zip(Some(file_editor))
});
.map(|(_id, tab)| tab);
if let Some((file_path, file_editor)) = open_file {
file_editor.is_dirty = false;
let text = file_editor.to_string();
let file_path = file_path.to_owned();
self.jobs.start(ctx, move || {
if let Err(e) = fs::write(file_path, text.as_bytes()) {
log::error!("{e}");
};
None
});
if let Some(open_tab) = open_tab {
open_tab.save(ctx, &mut self.jobs);
}
}
}