Skip to content

A file watcher

This program checks a file at a fixed interval. When the file has changed, it prints the new content.

<?nvs
// This program checks a file again and again, and prints a line each time the file changes.
class Watcher
{
public string $path;
public Core\Time\Instant $last;
public function constructor(string $path)
{
$this->path = $path;
// `Core\IO::modifiedAt` returns the time of the last change to the file.
$this->last = Core\IO::modifiedAt($path);
}
// Returns `true` when the file changed since the last call.
public function changed(): bool
{
Core\Time\Instant $now = Core\IO::modifiedAt($this->path);
if ($now->compareTo($this->last) == 0) {
return false;
}
$this->last = $now;
return true;
}
}
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'notes.txt');
Core\IO::write($path, "first version\n");
Watcher $watcher = new Watcher($path);
// A real watcher loops forever. This one checks five times, 50 milliseconds apart.
// The program changes the file itself after the second check, so that you see a change.
for (int $check = 1; $check <= 5; $check++) {
Core\Time::sleep(Core\Time\Duration::milliseconds(50));
if ($check == 3) {
Core\IO::write($path, "second version\n");
}
if ($watcher->changed()) {
echo 'check ', $check, ': changed, the file now says: ', Core\Str::trim(Core\IO::readText($path)), "\n";
} else {
echo 'check ', $check, ": no change\n";
}
}
Output
check 1: no change
check 2: no change
check 3: changed, the file now says: second version
check 4: no change
check 5: no change

Run it with nvs run file-watcher.nvs. It needs no nvs.toml.

Core\IO::modifiedAt returns the time of the last change to a file, as a Core\Time\Instant. The Watcher object stores the last time it read. compareTo returns 0 when two times are equal, so changed() returns false until the time is different.

Core\Time::sleep waits before each check. A short wait finds a change sooner, but the program reads the file system more often.

To watch your own file, replace the path and the for loop with while (true). Remove the lines that write the file. Stop the program with Ctrl+C.