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

Feature Request: recover nvim-ghost buffer #47

Open
mikaraunio opened this issue Jan 15, 2023 · 10 comments
Open

Feature Request: recover nvim-ghost buffer #47

mikaraunio opened this issue Jan 15, 2023 · 10 comments
Labels
enhancement New feature or request stalled Progress on this has stalled. Will resume when time and resources are available.
Milestone

Comments

@mikaraunio
Copy link

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.

@subnut subnut added enhancement New feature or request needs design We haven't thought yet how we'd go about fixing this issue labels Mar 11, 2023
@subnut
Copy link
Owner

subnut commented Mar 21, 2023

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 subnut added in progress Is being fixed currently and removed needs design We haven't thought yet how we'd go about fixing this issue labels Mar 21, 2023
@mikaraunio
Copy link
Author

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.

@subnut
Copy link
Owner

subnut commented Mar 21, 2023

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
Copy link
Owner

subnut commented Mar 29, 2023

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

@subnut subnut added awaiting feedback Needs more feedback before being finalized and removed in progress Is being fixed currently labels Apr 2, 2023
@subnut
Copy link
Owner

subnut commented Apr 10, 2023

@mikaraunio Have you tried the keep-buffers branch?

@mikaraunio
Copy link
Author

@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.

@subnut
Copy link
Owner

subnut commented Apr 19, 2023

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
Copy link
Owner

subnut commented Apr 19, 2023

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?

@mikaraunio
Copy link
Author

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

@subnut subnut added bug Something isn't working in progress Is being fixed currently and removed awaiting feedback Needs more feedback before being finalized labels Nov 5, 2023
@Andrew15-5
Copy link

Andrew15-5 commented Mar 15, 2024

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.)

@subnut subnut added stalled Progress on this has stalled. Will resume when time and resources are available. and removed bug Something isn't working in progress Is being fixed currently labels Jun 5, 2024
@subnut subnut added this to the v0.7 milestone Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stalled Progress on this has stalled. Will resume when time and resources are available.
Projects
None yet
Development

No branches or pull requests

3 participants