github.com/stephannv/file_watcher
0.1.0 / published Sep 7, 2024 / repository
Listen to file modifications using polling and notifies you about the changes. Pure Crystal implementation, no dependencies.
FileWatcher
Listen to file modifications using polling and notifies you about the changes. Pure Crystal implementation, no dependencies.
Installation
-
Add the dependency to your
shard.yml:dependencies: file_watcher: github: stephannv/file_watcher -
Run
shards install
Usage
require "file_watcher"
Basic usage
FileWatcher.watch("/path/to/folder/**/*.txt") do |event|
puts event.path # Path to file, eg. "path/to/folder/file.txt"
end
Multiple patterns
FileWatcher.watch("/path/to/folder/**/*", "/other/folder/**/*") do |event|
# do something
end
Event types
The type could be FileWatcher::EventType::Added, FileWatcher::EventType::Changed or FileWatcher::EventType::Deleted.
FileWatcher.watch("/path/to/folder/**/*") do |event|
event.type.added?
event.type.changed?
event.type.deleted?
# using case
case event.type
in .added?
# do something
in .changed?
# do something
in .deleted?
# do something
end
end
Using Path instead of String
FileWatcher.watch(Path["~path/to/folder/**/*"].expand(home: true)) do |event|
# do something
end
Using File::MatchOptions
It allows to listen to hidden files, dot files, etc. The default value is File::MatchOptions.glob_default. Read more here: [https://crystal-lang.org/api/File/MatchOptions.html].
FileWatcher.watch("/path/to/folder/**/*", match_options: File::MatchOptions::DotFiles) do |event|
puts event.path # eg. "path/to/folder/.file"
end
Symlinks
You can listen to changes in symlinks using follow_symlinks: true.
FileWatcher.watch("/path/to/folder/**/*", follow_symlinks: true) do |event|
# do something
end
Poll interval
By default the interval between each check is 1 second, you can change it passing interval: your_value.
FileWatcher.watch("/path/to/folder/**/*", interval: 0.10.seconds) do |event|
# do something
end
Development
crystal specto run tests.
Contributing
- Fork it (https://github.com/stephannv/file_watcher/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- stephann - creator and maintainer