Code Monkey home page Code Monkey logo

Comments (14)

GithubMachineUser avatar GithubMachineUser commented on June 12, 2024

I also want the function

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

Hi @svenax, @GithubMachineUser, the reason of this change is keeping things simple and flexible and it got into fswatch when it merged with fsw.

The typical use case will be feeding notifications emitted by fswatch to other programs and I believe the current approach is simpler and more flexible than the previous implementation. In fact, you can run any command when a notification is found piping the output of fswatch to the command you want to run:

$ fswatch [opts] [args] | do-something

That's also why the -0 option was implemented: so that fswatch output can be parsed without worrying about file names containing characters such as spaces and new lines.

Probably, the easiest way to get what you want is using fswatch together with xargs this way:

$ fswatch -0 [args] | xargs -0 -n 1 [opts] [args] 

Basically, this is what's going on:

  • -0 tells fswatch to end a notification record with a NUL character.
  • -0 tells xargs to parse the output accordingly.
  • -n 1 tells xargs to execute the specified command every argument is read from the output.

Another approach is using the shell read builtin specifying the delimiter character (check the documentation of the shell you're using):

$ fswatch -0 [opts] [args] | while read -d "" event \
  do \
    [do something with the $event] \
  done

Hope this helps.

Cheers,
-- Enrico

from fswatch.

svenax avatar svenax commented on June 12, 2024

That helps. Maybe it is obvious, but the examples I've seen for inotifyd and similar all use terribly complex while read ... constructs that I don't want to deal with.

My main use for fswatch is to sync changes to a remote server. For this I call rsync to do the real work. Works great. However, If I do something like

fswatch -0 . | xargs -0 -n1 ./sync-files.sh

my sync script will unnecessarily be called for every file changed, instead of just once for all files in the change batch. I guess I need something that throws away the paths printed by fswatch so I then can call my script. I tried

fswatch . | cat - > /dev/null | ./sync-files.sh

but that hangs after the first invocation. I guess cat does not get an EOF so it knows when to stop reading from stdin.

Any tips on how to do this? Thanks.

from fswatch.

svenax avatar svenax commented on June 12, 2024

I went ahead and hacked fswatch to have a one-set option that just prints a single throw-away message when a set of changes has been detected. This is probably not useful for any one but me, and I'm sure there are better ways of doing it anyways. But here it is if any one is interested:
https://github.com/svenax/fswatch/compare/alandipert:master...one-set

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

Hi @svenax, yeah, I've seen people doing terrible things with while read.

Since xargs knows nothing about fswatch, it can't possibly have a knowledge of a "change batch".

I can only think of one "quick" options at the moment, that is updating sync-files.sh so that:

  • It writes a lock file and a sentinel file when it starts.
  • It checks whether the lock file exists: if it exists, it touches the sentinel file it and exits, otherwise, it creates both the lock and the sentinel.
  • Before exiting, it checks whether the sentinel is newer than the lock file: if it is, it restarts, otherwise it exits.

I recognise that this use case translates to more complex scripting, but since fswatch can already watch multiple directories, I still see drawbacks in giving the option of emitting one fake event every time the FSEvents callback sends a group of changes:

  • On the one hand, the use case you propose would "work" out of the box.
  • On the other hand the fake event would defy the ability of watching multiple directories.

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

@svenax, it seems we were commenting at the same time.

As explained in my previous comment, I thought about the possibility of emitting a "fake" event, but I think it has some drawbacks. I'll have a look at this use case: thanks for the changeset.

from fswatch.

svenax avatar svenax commented on June 12, 2024

Right, I thought of that too, but my present use-case uses only one directory so that is not a problem. It would of course be possible to emit a single event for each watched directory; the directory name for instance. That is probably reasonably simple to do. I may take a look at that unless you feel like doing it yourself. :)

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

That could be a reasonable solution. I'll have a look at the events of the FSEvents API to be sure I'm taking into account all the event flags. Thanks for the offering, I'll have a look myself anyway but if you'd like to contribute, please feel free to do so.

Reopening this issue.

from fswatch.

slindberg avatar slindberg commented on June 12, 2024

👍 for an option to only emit some kind of batch change event.

from fswatch.

bwiklund avatar bwiklund commented on June 12, 2024

👍

While I like that you're making it a more flexible tool, running a script / rebooting something in development is probably 90% of the users of this script, and that should be dead simple to do. Thanks!

from fswatch.

nobeans avatar nobeans commented on June 12, 2024

👍
Though the change looks good because it's according to UNIX philosophy, I'm pleased if there is the option which allows specifying a command.

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

Hi @svenax, @GithubMachineUser, @slindberg, @bwiklund, @nobeans and everybody else.

I've just released v. 1.3.3 where:

  • The -o option instructs fswatch to only print one line with the number of the change events received in the last "batch". This way, you can use this event to trigger the execution of another program, as discussed in this issue and in Issue 24.
  • The fswatch-run script has been added to ease the transition from v. 0.x to 1.x. This scripts accepts the older fswatch path command syntax and invokes fswatch -o piping its output to xargs for you. Being a one-liner, I still strongly suggest adapting your script to the new behaviour.

Cheers,
-- Enrico

P.S.: @svenax, thanks for your changeset.

from fswatch.

slindberg avatar slindberg commented on June 12, 2024

Awesome, thanks!

For some reason it looks like the source code tarball in that release doesn't have the 1.3.3 changes 😕

I'm attempting to upgrade the Homebrew Formula, but before I make a PR, is it safe to use the release version with the configure script already autogen'd?

from fswatch.

emcrisostomo avatar emcrisostomo commented on June 12, 2024

Hi @slindberg,

Thanks, you're right, the tag was set but develop was not merged into master. I've updated the tags and the release and now the source code tarball you linked in your comment is correct.

By the way, since you're asking, I use to attach the distribution tarball made by make distcheck in the release and that should be the way to go. Ironically, in this case it was "the" correct one.

Cheers and let me know if everything's ok,
-- Enrico

from fswatch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.