Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pta2002/gleam-filespy
Get notified of filesystem changes in Gleam
https://github.com/pta2002/gleam-filespy
gleam
Last synced: 27 days ago
JSON representation
Get notified of filesystem changes in Gleam
- Host: GitHub
- URL: https://github.com/pta2002/gleam-filespy
- Owner: pta2002
- License: apache-2.0
- Created: 2023-10-21T13:42:55.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-12T09:07:34.000Z (5 months ago)
- Last Synced: 2024-09-16T02:38:08.266Z (about 2 months ago)
- Topics: gleam
- Language: Gleam
- Homepage:
- Size: 26.4 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# filespy
[![Package Version](https://img.shields.io/hexpm/v/filespy)](https://hex.pm/packages/filespy)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/filespy/)Filespy is a small library and versatile library that allows you to watch for filesystem events from Gleam! It's a type-safe wrapper around the erlang [FS](https://github.com/5HT/fs) library.
## Quick start
Add the library to your requirements with `gleam add`:
```sh
gleam add filespy
```**Note**: If you use Linux or a BSD, you need to install `inotify-tools`!
## Usage
The library is configured via a simple builder:
```gleam
import filespy
import gleam/io
import gleam/erlang/processfn main() {
let _res = filespy.new() // Create the builder
|> filespy.add_dir(".") // Watch the current directory
|> filespy.add_dir("/mnt") // Watch the /mnt directory
|> filespy.set_handler(fn (path, event) {
// This callback will be run every time a filesystem event is detected
// in the specified directories
io.debug(#(path, event))
Nil
})
|> filespy.start() // Start the watcher!process.sleep_forever()
}
```If you want to, you can also go lower level, and configure the underlying actor:
```gleam
import filespy
import gleam/iofn main() {
let _res = filespy.new() // Create the builder
|> filespy.add_dir(".") // Watch the current directory
|> filespy.add_dir("/mnt") // Watch the /mnt directory
|> filespy.set_initial_state(0) // Initial state for the actor, this is required!
|> filespy.set_actor_handler(fn (message, state) {
// This callback will be run every time a filesystem event is detected
// in the specified directories// In the actor handler, multiple events might be sent at once.
let filespy.Change(path: path, events: events) = message
io.debug(#(path, events, state))
actor.continue(state + 1)
})
|> filespy.start() // Start the watcher!process.sleep_forever()
}
```You can go even lower level, and get the actor start spec to configure it yourself:
```gleam
import filespy
import gleam/iofn main() {
let start_spec = filespy.new() // Create the builder
|> filespy.add_dir(".") // Watch the current directory
|> filespy.add_dir("/mnt") // Watch the /mnt directory
|> filespy.set_initial_state(0) // Initial state for the actor, this is required!
|> filespy.set_actor_handler(fn (message, state) {
// This callback will be run every time a filesystem event is detected
// in the specified directories// In the actor handler, multiple events might be sent at once.
let filespy.Change(path: path, events: events) = message
io.debug(#(path, events, state))
actor.continue(state + 1)
})
|> filespy.spec() // Get the specactor.start_spec(start_spec)
process.sleep_forever()
}
```## Documentation
If you have any more questions, check out the [documentation](https://hexdocs.pm/filespy)!