Closed fennewald closed 1 year ago
looking into the golang file watching ecosystem, it seems the list of events above is likely more verbose than needed.
I plan on using fsnotify, which means this will be cross-platform, but will only support Create, Write, Rename, Remove, Chmod
events
Regarding the design itself:
There's precedent in accepting duration strings (as accepted by time.ParseDuration) in sleep
.
There's no precedent in outputting timestamps though. It should probably be another basic data type, like numbers.
debounce
can be a general-purpose command, so you'd do something like watch ... | debounce 1s
.
But...
However, developing the behavior above as a separate command, that implements all the logic defined above, and simply outputs json, could be wrapped relatively easily by elvish, and might make more sense than an addition to the stdlib.
I agree with this :) Developing a command that is based on fsnotify and outputs JSON makes a lot of sense.
I do like the idea of having this well integrated into Elvish - the output is structured, so it's quite clumsy in traditional shells but very easy in Elvish. If you'd like to build an Elvish module that wraps it, that's something I'm thinking about in #1607 (case 3).
Makes sense :). I have a few thoughts about #1607 I'll leave there.
Note that there is no reason this needs to be limited to working on Linux. The fswatch command shows that it is possible to support Linux, the various BSDs (including macOS), and Windows. In fact, if you're going to write a module that wraps an external command to make using the external command easier to use with Elvish I would argue that fswatch
is a better choice given that it also works on all the platforms supported by Elvish.
This is a feature request for an
inotify
module to be added to the elvish stdlib. If accepted, I would develop this feature. I am primarily looking for @xiaq 's blessing tobloatadd to the stdlib.This module would provide two commands,
watch
andwait
. They would function identically, with the only different beingwait
would exit after a single event was caught, andwatch
would continue indefinitely.Function signatures
events
would be a list of events to be caught. By default, it would consist ofcreate
,delete
, andmodify
events, but the full list of valid events would be(stolen directly from inotifywatch):debounce
would be a duration, in milliseconds, to debounce events by. If two events are registered withindebounce
time of each other, they are combined and sent as one event.timeout
is a timeout of the maximum duration without edits. Iftimeout
passes without any events being received, the command will exit (without error). If set to 0, no timeout is used.recursive
is a flag to determine if directories should be recursively watched.rich-output
is a flag to determine if the rich output format should be used. Whenfalse
, each event sends only the modified filename on the value channel. For example:If true, each event sends a key-value object, with the filename, list of captured events, and timestamp of the events. For combined, debounced events, the latest timestamp is sent:
@files
is a list of files to apply watches to.Example usage
Running a lambda each time a file was modified
Decision points
There are a few decisions made that are arbitrary, and may not be the best choice. Open to suggestions
debounce
andtimeout
will be provided as millisecondstimestamp
for caught events will be provided as a unix timestampCaveats
Alternatives
One solution would be to wrap the already existing inotify-tools, available on most linux package managers. I don't think this is the best approach. These tools lack critical features, like debouncing and output parsing. These additional features are complex enough that implementing them in an elvish wrapper would be non-trivial.
However, developing the behavior above as a separate command, that implements all the logic defined above, and simply outputs json, could be wrapped relatively easily by elvish, and might make more sense than an addition to the stdlib.