Skip to content

Commit

Permalink
ignore file events such as "OPEN", just listen to CREATE/REMOVE/MODIFY
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Feb 3, 2025
1 parent 6cf7e2a commit b8d3d50
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/file_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Res;
use ignore::gitignore::GitignoreBuilder;
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::{
path::Path,
sync::{
Expand All @@ -23,8 +23,13 @@ impl FileWatcher {

let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
if let Ok(event) = res {
if !is_changed(&event) {
return;
}

for path in event.paths {
if !gitignore.matched(&path, path.is_dir()).is_ignore() {
log::info!("File changed: {:?}", path);
pending_updates_w.store(true, Ordering::Relaxed);
break;
}
Expand All @@ -44,3 +49,10 @@ impl FileWatcher {
self.pending_updates.swap(false, Ordering::Relaxed)
}
}

fn is_changed(event: &Event) -> bool {
matches!(
event.kind,
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
)
}

2 comments on commit b8d3d50

@altsem
Copy link
Owner Author

@altsem altsem commented on b8d3d50 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillig was meant to be a PR, now its on master. Oh well 😓.
I found that gitu was eating a lot of CPU, some process of mine opening a lot of files.

Hope this'll filter off some unwanted refreshes.

@altsem
Copy link
Owner Author

@altsem altsem commented on b8d3d50 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can think of potential future improvements:

  • Some kind of indicator to show that Gitu is refreshing (and you can't interact with it yet).
  • Perhaps throttle the amount of refreshes if a lot of files are being changed.
    • Refresh immediately if a file is changed.
    • Ensure refreshes are not done more often than N seconds apart.

Please sign in to comment.