nvim-ghost.nvim icon indicating copy to clipboard operation
nvim-ghost.nvim copied to clipboard

Feature Request: recover nvim-ghost buffer

Open mikaraunio opened this issue 2 years ago • 10 comments

It seems that currently, nvim-ghost buffers are permanently deleted when the browser disconnects.

I'd love to have a way to recover their content, in case of a server error on form submit, browser crash etc.

mikaraunio avatar Jan 15 '23 18:01 mikaraunio

Okay, two ways come to my mind -

  • Add a :GhostTextKeep command. When you run this command in a buffer created by nvim-ghost, the buffer won't get deleted when the browser disconnects.
  • Add a configuration variable g:nvim_ghost_keep_buffers which, when set to 1, will cause nvim-ghost to not delete buffers when the browser disconnects.

BTW, sorry for the late reply. These last few months were pretty hectic for me.

subnut avatar Mar 21 '23 06:03 subnut

Thanks, happy to see this move forward. Think I'd prefer way number 2 so wouldn't have to worry about remembering to run :GhostTextKeep.

mikaraunio avatar Mar 21 '23 12:03 mikaraunio

Note to self (Implementation details)

  • Browser calls close
  • We check if g:nvim_ghost_keep_buffers == 1
  • If no, current code is followed
  • If yes
    • Remove buftype (because we set it so that the user doesn't mistakenly save the temporary buffer, but now that the buffer is no longer going to be changed or deleted, the user may want to save it as a file)
    • (anything else...?)

TIPS

  • Rename nvim_ghost#helper#...() to nvim_ghost#server#...() eg.
    • nvim_ghost#helper#kill_server() to nvim_ghost#server#kill()
    • nvim_ghost#helper#session_closed() to nvim_ghost#server#close_session()
  • From binary, call nvim_ghost#helper#buf_del() or something like that, which contains all the vim code that's currently in the binary itself.

subnut avatar Mar 21 '23 20:03 subnut

Try the keep-buffers branch, and report issues (if any)

subnut avatar Mar 29 '23 10:03 subnut

@mikaraunio Have you tried the keep-buffers branch?

subnut avatar Apr 10 '23 18:04 subnut

@subnut Sorry for the delay. I Trying it now and seeing the following:

  • Have set vim.g.nvim_ghost_keep_buffers = 1
  • Connected to this comment box and entered text
  • Reloaded the page, and the buffer disappears from nvim like before.

mikaraunio avatar Apr 17 '23 23:04 mikaraunio

Personal note (sample log) -

[17:09:52]: Websocket ::1:48760:0:0 connected to /run/user/1000/nvim.27576.0
[17:09:52]: From 48760 got {"title":"Feature Request: recover nvim-ghost buffer · Issue #47 · subnut/nvim-ghost.nvim","url":"github.com","syntax":"","text":"Hi","selections":[{"start":2,"end":2}]}
[17:09:52]: nvim_event  handle=True  ('nvim_buf_changedtick_event', [<Buffer(handle=3)>, 1])
[17:09:52]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 2, 0, 1, ['Hi'], False])
127.0.0.1 - - [19/Apr/2023 17:09:52] "GET /focus?focus=/run/user/1000/nvim.27576.0 HTTP/1.1" 200 -
127.0.0.1 - - [19/Apr/2023 17:09:53] "GET /focus?focus=/run/user/1000/nvim.27576.0 HTTP/1.1" 200 -
[17:09:55]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 3, 0, 1, ['Hi '], False])
[17:09:55]: To 48760 sent {"text": "Hi ", "selections": [{"start": 3, "end": 3}]}
[17:09:55]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 4, 0, 1, ['Hi t'], False])
[17:09:55]: To 48760 sent {"text": "Hi t", "selections": [{"start": 4, "end": 4}]}
[17:09:55]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 5, 0, 1, ['Hi th'], False])
[17:09:55]: To 48760 sent {"text": "Hi th", "selections": [{"start": 5, "end": 5}]}
[17:09:56]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 6, 0, 1, ['Hi the'], False])
[17:09:56]: To 48760 sent {"text": "Hi the", "selections": [{"start": 6, "end": 6}]}
[17:09:56]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 7, 0, 1, ['Hi ther'], False])
[17:09:56]: To 48760 sent {"text": "Hi ther", "selections": [{"start": 7, "end": 7}]}
[17:09:56]: nvim_event  handle=True  ('nvim_buf_lines_event', [<Buffer(handle=3)>, 8, 0, 1, ['Hi there'], False])
[17:09:56]: To 48760 sent {"text": "Hi there", "selections": [{"start": 8, "end": 8}]}
127.0.0.1 - - [19/Apr/2023 17:09:58] "GET /focus?focus=/run/user/1000/nvim.27576.0 HTTP/1.1" 200 -
[17:10:00]: Websocket ::1:48760:0:0 closed by browser
[17:10:00]: nvim_event  handle=True  ('nvim_buf_detach_event', [<Buffer(handle=3)>])
[17:10:00]: Closing websocket ::1:48760:0:0
[17:10:00]: Websocket ::1:48760:0:0 closed by us

Buffer stays (ie. isn't deleted) but the text is gone.

subnut avatar Apr 19 '23 11:04 subnut

Further investigation needed.

[17:10:00]: Websocket ::1:48760:0:0 closed by browser
[17:10:00]: nvim_event  handle=True  ('nvim_buf_detach_event', [<Buffer(handle=3)>])
[17:10:00]: Closing websocket ::1:48760:0:0
[17:10:00]: Websocket ::1:48760:0:0 closed by us

Probable race condition or what?

subnut avatar Apr 19 '23 11:04 subnut

Correction to my comment above, confirm I'm seeing the same thing as you: the buffer stays, but the text is gone.

mikaraunio avatar Apr 19 '23 14:04 mikaraunio

Hello, fellow Neovimers. I found out about this project from another person and so far this project looks promising. Even though some things are a hustle to do, but in the end the advantages are so worth it (for some use cases).

Anyway, I wanted to share my patch that I successfully made and it works great. This probably should go into the code base, but since I've made it more Unix-like (I'm using Linux btw), it just not at the cross-platform level yet to be a proper PR. Here is the patch:

patch
diff --git a/binary.py b/binary.py
index 6d12314..0e8e34b 100644
--- a/binary.py
+++ b/binary.py
@@ -1,3 +1,4 @@
+import datetime
 import json
 import os
 import random
@@ -341,9 +342,10 @@ class GhostWebSocket(WebSocket):
         global FOCUSED_NVIM_ADDRESS
         self.neovim_address = FOCUSED_NVIM_ADDRESS
         self.neovim_handle = pynvim.attach("socket", path=self.neovim_address)
-        self.buffer_handle = self.neovim_handle.api.create_buf(False, True)
+        basename = datetime.datetime.now().isoformat(timespec='seconds')
+        self.neovim_handle.command(f"tabe /tmp/GhostText_message_{basename}.md")
+        self.buffer_handle = self.neovim_handle.api.get_current_buf()
         self.neovim_handle.api.buf_set_option(self.buffer_handle, "bufhidden", "wipe")
-        self.neovim_handle.command(f"tabe | {self.buffer_handle.number}buffer")
         self.handle_neovim_notifications = True
         self._start_neovim_listener()

So it basically changes how it previously created a buffer and what you can do with it. Initially it would've created 2 buffers one of which isn't used, and the other is the used one, but it is unnamed, and therefore can't be used with LSP servers like ltex-ls for grammar checking (LanguageTool wrapper). And also it's harder to save the message in case you need to save it and/or go back to it. So this patch also saves all messages "just in case" (and creates only 1 new buffer).

I thought that it's better to use ~/.cache/ for that, but it is still platform-dependent, so it doesn't make much of a difference at this point in time anyway.

The only nifty features that I would like to add is a URL-based file name. It's like screenshots that you can find in Android that are named with app's name in the file name, because it was taken while that app was opened on the screen. So it will be just like that, but instead there will be just URLs. It can be only domain-based or a full URL, so that you can go back and see where exactly did you write that message (combined with a timestamp it would be a perfect combo).

Since you can add au with a URL pattern, I assume you can get the URL somewhere. But I don't know if it is limited to the Neovim itself or the nvim-ghost-binary as well.

Oh, and also the command from the workflow didn't work locally (with an active venv dir), so instead I had to add --paths:

pyinstaller --onefile --distpath . --paths .venv/lib/python3.10/site-packages binary.py --name nvim-ghost-binary

And the binary went from 20.4 MiB to 12.43 MiB compared to the release binary.

(Of course this comment was written with GhostText and the patched version.)

Andrew15-5 avatar Mar 15 '24 02:03 Andrew15-5