dsanson / termpdf.py

A graphical pdf and epub reader that works inside the kitty terminal
MIT License
496 stars 30 forks source link

Auto-refresh on file change #12

Open slarwise opened 4 years ago

slarwise commented 4 years ago

Hi!

Would it be possible to have termpdf refresh the current pdf when the file is updated? #10 solves this using the workflow described there but having an option for auto-refresh on file change would make it simpler to use in any workflow.

dsanson commented 4 years ago

Okay. This feature is now implemented. I don't have any proper asynchronic control in the script, so I can't make it refresh completely automatically: most of the time, it is waiting for a keypress, and I don't know how to interrupt that when a file changes.

For now, it checks to see if the file has changed after each key press. If the file has changed, the keypress is swallowed and the document is refreshed. Does that seem right to you?

slarwise commented 4 years ago

Thanks, that was fast! It seems to be working well.

I was looking for refreshing without even having to touch termpdf. I usually write latex in neovim, have latexmk continuously compiling in the background and view the pdf in Skim which refreshes the document when it's been updated. So I never have to go into the pdf viewer unless I need to change page. This implementation definitely helps though.

So since the main loop is waiting for keypresses, you would need another thread that watches the file to make this work?

dsanson commented 4 years ago

I think that's right. I have no experience with multiple threads in Python. Features I'd like to have that require it:

At some point, I need to sit down and read through some tutorials on threading in python, and get this implemented!

dsanson commented 4 years ago

Okay. I reimplemented autorefresh using threading. This is still probably not the right thing to do. I just have a loop that pools os.path.getmtime() every .5 seconds. The correct solution is to use the watchdog module, but I couldn't figure out how to get that to work.

slarwise commented 4 years ago

Those features sound awesome, especially navigation from vim.

Tried the new version with the threading, it doesn't seem to work for me. I open the pdf with termpdf, make a change in the tex-code and compile it with latexmk. After that termpdf closes with no error message. Let me know if you want more details.