Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gonzaarcr opened this issue Feb 15, 2022 · 3 comments
Open

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

gonzaarcr opened this issue Feb 15, 2022 · 3 comments
Assignees

Comments

@gonzaarcr
Copy link

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 gonzaarcr changed the title Watch for file changes instead of refresh every n seconds [FR] Watch for file changes instead of refresh every n seconds Feb 15, 2022
@blacknon
Copy link
Owner

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

@blacknon blacknon self-assigned this Feb 16, 2022
@sanmai-NL
Copy link

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?

@ilyagr
Copy link
Contributor

ilyagr commented Jan 12, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants