hwatch icon indicating copy to clipboard operation
hwatch copied to clipboard

[FR] Watch for file changes instead of refresh every n seconds

Open gonzaarcr opened this issue 3 years ago • 3 comments

It would be cool if there was an option to watch for file/directory chagnes instead of refreasing every n seconds. Like watching .git folder for refreshing git status.

gonzaarcr avatar Feb 15 '22 12:02 gonzaarcr

That's a good idea! I think interval is an exclusive option, but it looks good!

blacknon avatar Feb 16 '22 01:02 blacknon

That is better solved by watchexec.

Also a Rust-based app. What do you think about collaboration, @blacknon? It seems timer- and filesystem-based watching is complementary, often needed together and shares some UI design. A single, more powerful tool with more maintainership capacity would be ideal, no?

sanmai-NL avatar Nov 11 '22 10:11 sanmai-NL

There's a way to set this up as follows:

  1. Have watchexec run a command and output the result to a file.
    • This requires an additional trick (see the mv command below) so that hwatch won't read a half-written file.
  2. Have hwatch read that file.

An outline:

$ # In one terminal or tmux tab
$ watchexec 'git log --graph --all --pretty=oneline > /tmp/git_log2 && 
                                mv /tmp/git_log2 /tmp/git_log' --print-events

$ # In another terminal or tmux tab
$ hwatch -n 0.2 cat /tmp/git_log

You could also make a script to do this while running watchexec in the background without the --print-events.

Unfortunately, I find that setting up watchexec to work when I want it to is so fiddly that it is not worth the trouble for me. In particular, when I run watchexec and cargo watch at the same time, they seem to steal events from each other, making both unreliable.

I might also try entr as a substitute for watchexec.

Edit: I found entr to be more reliable and efficient, even though the command to run it is uglier. I use rg to get a list of files to watch while obeying .gitignore; you could also use fd or something else.

while sleep 0.1; do
    rg --files | entr -d -s  'git log --graph --all --pretty=oneline > /tmp/git_log2 && 
                              mv /tmp/git_log2 /tmp/git_log && date' && break
done

ilyagr avatar Jan 12 '23 05:01 ilyagr