Skip to content

Latest commit

History

History
146 lines (112 loc) 路 5.18 KB

README.md

File metadata and controls

146 lines (112 loc) 路 5.18 KB

Notify

禄 Crate 禄 Docs 禄 CI 禄 Downloads 禄 Conduct 禄 Public Domain

Cross-platform filesystem notification library for Rust.

Caution! This is unstable code!

You likely want either the latest 4.0 release or 5.0.0-pre.14.

(Looking for desktop notifications instead? Have a look at notify-rust or alert-after!)

As used by: alacritty, cargo watch, cobalt, docket, mdBook, pax, rdiff, rust-analyzer, timetrack, watchexec, xi-editor, and others.

Installation

[dependencies]
crossbeam-channel = "0.4.0"
notify = "5.0.0-pre.14"

Usage (5.0.0-pre.14)

The examples below are aspirational only, to preview what the final release may have looked like. They may not work. Refer to the API documentation instead.

use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;

fn main() -> notify::Result<()> {
    let (tx, rx) = std::sync::mpsc::channel();

    // Automatically select the best implementation for your platform.
    // You can also access each implementation directly e.g. INotifyWatcher.
    // Note that dropping this `watcher` will exit the Watcher-Loop itself.
    let mut watcher = RecommendedWatcher::new(tx)?;

    // Add a path to be watched. All files and directories at that path and
    // below will be monitored for changes.
    watcher.watch(Path::new("."), RecursiveMode::Recursive)?;

    for res in rx {
        match res {
            Ok(event) => println!("changed: {:?}", event),
            Err(e) => println!("watch error: {:?}", e),
        }
    }

    Ok(())
}

Missing in 5.0.0-pre.14 currently

The debouncer can currently only be found in v4.

Serde

Events can be serialisable via serde. To enable the feature:

notify = { version = "5.0.0-pre.14", features = ["serde"] }

Platforms

  • Linux / Android: inotify
  • macOS: FSEvents
  • Windows: ReadDirectoryChangesW
  • FreeBSD / NetBSD / OpenBSD / DragonflyBSD: kqueue
  • All platforms: polling

FSEvents

Due to the inner security model of FSEvents (see FileSystemEventSecurity), some event cannot be observed easily when trying to follow files that do not belong to you. In this case, reverting to the pollwatcher can fix the issue, with a slight performance cost.

License

Notify was undergoing a transition to using the Artistic License 2.0 from CC Zero 1.0. A part of the code is only under CC0, and another part, including all new code since commit 3378ac5a, is under both CC0 and Artistic. When the project was to be entirely free of CC0 code, the license would be formally changed (and that would have incurred a major version bump). As part of this, contributions to Notify since would agree to release under both.

Origins

Inspired by Go's fsnotify and Node.js's Chokidar, born out of need for cargo watch, and general frustration at the non-existence of C/Rust cross-platform notify libraries.

Written by F茅lix Saparelli and awesome contributors.