hwatch
hwatch copied to clipboard
[FR] Watch for file changes instead of refresh every n seconds
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
.
That's a good idea! I think interval is an exclusive option, but it looks good!
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?
There's a way to set this up as follows:
- Have
watchexec
run a command and output the result to a file.- This requires an additional trick (see the
mv
command below) so thathwatch
won't read a half-written file.
- This requires an additional trick (see the
- 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