https://github.com/webfreak001/fswatch
A cross-platform folder & file watching library using win32, inotify or std.file
https://github.com/webfreak001/fswatch
Last synced: 5 months ago
JSON representation
A cross-platform folder & file watching library using win32, inotify or std.file
- Host: GitHub
- URL: https://github.com/webfreak001/fswatch
- Owner: WebFreak001
- Created: 2016-07-18T20:04:38.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-07T10:13:01.000Z (almost 3 years ago)
- Last Synced: 2025-02-28T14:48:12.858Z (over 1 year ago)
- Language: D
- Size: 32.2 KB
- Stars: 34
- Watchers: 5
- Forks: 8
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FSWatch
[](https://github.com/WebFreak001/FSWatch/actions/workflows/test.yml) [ ](https://code.dlang.org/packages/fswatch)
A cross platform library simplifying watching for file changes using a non-blocking interface using the Win32 API, `inotify` or `std.file.dirEntries` as fallback.
## Comparison between implementations
|Platform|Watching Directory|Watching File|
|---|---|---|
|Windows|`ReadDirectoryChangesW` (very fast, accurate)|polling using `std.file.timeLastModified` and `std.file.exists` (fast, not accurate for quick write)|
|Linux|`inotify` (very fast, accurate)|`inotify` (very fast, accurate)|
|Other|polling using `std.file.dirEntries` (slow, not accurate)|polling using `std.file.timeLastModified` and `std.file.exists` (fast, not accurate for quick write)|
To force usage of the `std.file` polling implementation you can just add `version = FSWForcePoll;`
Don't do this unless you have a specific reason as you will lose speed and accuracy!
## Example
```d
void main()
{
// Initializes a FileWatch instance to watch on `project1/` (first argument) recursively (second argument)
auto watcher = FileWatch("project1/", true);
while (true)
{
// This will fetch all queued events or an empty array if there are none
foreach (event; watcher.getEvents())
{
// paths are relative to the watched directory in most cases
if (event.path == "dub.json" || event.path == "dub.sdl")
{
if (event.type == FileChangeEventType.create)
{
// dub.json or dub.sdl got created
}
else if (event.type == FileChangeEventType.modify)
{
// dub.json or dub.sdl got modified
}
else if (event.type == FileChangeEventType.remove)
{
// dub.json or dub.sdl got deleted
}
}
if (event.type == FileChangeEventType.rename)
{
// The file `event.path` has been removed to `event.newPath`
}
else if (event.type == FileChangeEventType.createSelf)
{
// the folder we are watching has been created
}
else if (event.type == FileChangeEventType.removeSelf)
{
// the folder we are watching has been deleted or renamed
}
}
// ...
myapp.handleInput();
myapp.update();
myapp.draw();
}
}
```