fswatch 0.6.1

a cross-platform folder & file watching library using win32, inotify or std.file


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


This package provides sub packages which can be used individually:

fswatch:watch - FSWatch library usage example.

FSWatch

CI Dub version Dub downloads

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

PlatformWatching DirectoryWatching File
WindowsReadDirectoryChangesW (very fast, accurate)polling using std.file.timeLastModified and std.file.exists (fast, not accurate for quick write)
Linuxinotify (very fast, accurate)inotify (very fast, accurate)
Otherpolling 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

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();
	}
}
Authors:
  • webfreak
Sub packages:
fswatch:watch
Dependencies:
none
Versions:
0.6.1 2023-Sep-07
0.6.0 2021-Jul-20
0.5.2 2021-Jul-20
0.5.1 2020-Aug-03
0.5.0 2019-Jun-13
Show all 11 versions
Download Stats:
  • 0 downloads today

  • 6 downloads this week

  • 6 downloads this month

  • 149003 downloads total

Score:
1.8
Short URL:
fswatch.dub.pm