glow
glow copied to clipboard
Add option to edit current document
Implements charmbracelet/glow#182
Allows the user to edit the current document by typing 'e'. If $EDITOR is set, a temporary file is created with the contents of the current document, and the specified editor is used to open it. If no changes are made when the editor exits, nothing happens. If changes are made, then:
if the document is a local file, the edits are written to that file if the document is stashed, a new stash is created with the edits, and the old stash is deleted.
It would be ideal to simply update the contents of a stash, but the charm API doesn't currently allow this. Deleting the selected stash has the side effect that, when the user returns to the list of files and tries to re-open the same document, the stash won't load. Restarting glow solves this and shows the newly created, updated stash.
https://github.com/charmbracelet/charm/pull/13 creates a SetMarkdownBody() method to resolve this minor issue.
So I'm realizing now which parts of this should be (non-)blocking. Launching the editor needs to be blocking. But once the edits are made, saving the local file / stashing the changes should be done asynchronously. Working on that now
Alright, as far as I can tell, this is in a pretty good state. Found no bugs while testing. Code is much more organized. Stuff that should block blocks and stuff that shouldn't doesn't.
Thank you for the PR!
Just a heads up that we're working on an update to Glow that will most likely conflict with this, though we're happy to look at those conflicts on our end. The holidays are also upon us so it may be a little bit before we give this PR a proper review.
Baha, no worries. I'm happy to look over it, too. If there's a fork or branch where the update is being worked on, I could look over adjusting my code to work with it
Cool, we'll push something up early next week. We may contribute to your branch as well.
So I've been testing this out with various editors. It behaves oddly with GUI editors.
- The file remains displayed while the editor is launched.
- This might not be a bad thing! If/when charmbracelet/glow#148 is implemented, it might be nice to have a live preview of the document while you're editing it. However, this likely won't work out of the box; we'd probably have to re-work some of the current code to have a live preview while a GUI editor is in use.
- The control isn't necessarily immediately returned to
glowafter you're finished editing. For example, if I useEDITOR=subl glowwhen I'm already using Sublime Text to edit other files... when I'm finished editing the glow document, I don't exit Sublime since I'm still using it for other stuff. Sinceglowis waiting for the editor to exit, I then have to either quit Sublime and interrupt my other work or killglowand lose my edits.
A couple possible solutions:
- Use
fsnotifyto detect when the temporary file being used for edits is written to. Copy those changes to the local file or upload them to the stashed file as appropriate for every write event. - If we can detect whether the editor is GUI based or console based, make GUI file editing non-blocking. Instead, display a message like "File opened in external editor. Press 'enter' to continue when finished.".
I'm already using fsnotify to implement charmbracelet/glow#148, so that shouldn't be too bad.
However, I haven't the slightest idea how to detect if the editor is GUI or console based, but intuition says... while nothing is technically "impossible" when programming, this probably leans towards more complex than it's worth. If that's the case, I currently don't have a good idea for returning control back to glow when finished editing.
Having a direct editing feature would be awesome!
@shitchell curious if you tried using EDITOR=subl --wait in order for the editor to return once the file is saved/closed. Sorry for the necrobump.
Hey @shitchell, thank you so much for this PR!
Open in editor was added to Glow recently in:
- https://github.com/charmbracelet/glow/pull/361
You can now press e on any local document to edit it.