Expand description
File system watcher service and other types.
The WATCHER
service can be used to get notifications when a file or directory is modified. It also provides
ways to bind a file to a variable, automatically synchronizing both.
The example below binds the current content of a text file to at text variable using WATCHER.read
.
Any external change made to the text file updates the UI text.
use zng::{prelude::*, fs_watcher::WATCHER};
Text!(WATCHER.read("dump.log", Txt::from(""), |f| f.ok()?.text().ok()))
The next example created a read-write binding with the text file, any external change made to the text file updates the
TextInput!
and any change made using the TextInput!
updates the file contents.
use zng::{prelude::*, fs_watcher::WATCHER};
TextInput!(zng::fs_watcher::WATCHER.sync(
"dump.log",
// initial value
Txt::from(""),
// read, only updates txt if returns Some
|f| f.ok()?.text().ok(),
// write, only change file if commit called.
|txt, f| {
if let Ok(mut f) = f {
if f.write_text(&txt).is_ok() {
// replace actual file with temp that was successfully written.
let _ = f.commit();
} else {
f.cancel();
}
}
},
))
The WATCHER
service abstracts away most of the headache of interacting with the file system. This service
is used internally by the implementations of CONFIG
and L10N
.
§Full API
See zng_ext_fs_watcher
for the full watcher API.
Modules§
- File system change event types.
Structs§
- Represents a single file system change, annotated.
- Handle that holds a
WATCHER.annotate
note. FS_CHANGES_EVENT
arguments.- File system watcher service.
- Represents an open read-only file provided by
WATCHER.read
. - Represents an active file or directory watcher in
WATCHER
. - Represents an open write file provided by
WATCHER.sync
.
Statics§
- Event sent by the
WATCHER
service on directories or files that are watched.
Traits§
- Represents a
FsChange
note. - Represents a status type for
WATCHER
read-only operations. - Represents a status type for
WATCHER.sync_status
.