randy3k / Terminus

Bring a real terminal to Sublime Text
https://packagecontrol.io/packages/Terminus
MIT License
1.38k stars 84 forks source link

`'NoneType' object has no attribute 'run_command'` when running `terminus_open` and the panel was previously hidden rather than closed #322

Closed timfjord closed 2 years ago

timfjord commented 2 years ago

Background

I am working on a sublime plugin where terminus acts as a runner. When I used terminus_exec everything was ok, but when I switched to terminus_open for more advanced integration I started to get the following when run a command with a custom panel name on the second time after the previous panel was hidden(Esc) rather than closed(⌘ + W)

Traceback (most recent call last):
  File "/Users/timfjord/Library/Application Support/Sublime Text 3/Installed Packages/Terminus.sublime-package/terminus/commands.py", line 96, in <lambda>
  File "/Users/timfjord/Library/Application Support/Sublime Text 3/Installed Packages/Terminus.sublime-package/terminus/commands.py", line 266, in run_async
AttributeError: 'NoneType' object has no attribute 'run_command'

When the panel isn't hidden and still in the view or is closed the command runs without problems

Solution

I was able to fix it by adding a workaround

panel = self.window().find_output_panel(self.PANEL_NAME)
# check if the panel is hidden
if panel is not None and not bool(panel.window()):
  panel.run_command('terminus_close')

So basically when the panel is hidden I simply closed it and run the command again

I believe something like this is already present in the terminus_exec handler(I just couldn't find it) because there are no issues when doing the same(obviously without the panel_name option) with terminus_exec

It looks like it has to do with that fact that window() returns None when the panel is not in the view that's why my workaround is working


Just in case here is the implementation https://github.com/timfjord/AnyTest/blob/main/plugin/outputs/terminus.py

randy3k commented 2 years ago

It seems to be an issue of a tagged panel. I have pushed a fix 97f1208. Removing the tag arg should also fix your issue.

If you launch the command from a different window, the tagged panel of the first window will be reused. Depends on what you want to do, it may not be necessary to tag a panel, or you'll need window specific tags.

randy3k commented 2 years ago

v0.3.23 is out

timfjord commented 2 years ago

Awesome, thanks @randy3k for the super quick fix 👍🏻 tag is important because I want some custom shortcuts so I am glad that I can remove my workaround

randy3k commented 2 years ago

Please note that tags are shared globally. So you will need to have window specific tags to allow users to create panels on multiple windows. But doing so may defect that reason of using tags for the purpose of key bindings.

timfjord commented 2 years ago

Thanks for letting me know, it is quite important for my use-case

So just to confirm, if I run terminus in one window with a tag, and then run another terminus command in another window using the same tag, the latter command will jump to the previous window and be executed there?

timfjord commented 2 years ago

Now I understand what you meant that the tags are shared globally. Basically when I tried to create a panel with a specific tag and it was already visible in a different window then my command was run in that window rather than in my current window.

So I am wondering is it on purpose? I mean are there technical limitations to making that window scoped? I understand this is a breaking change I am more wondered whether it is possible at all(given the way terminus is implemented)

I think tags should be window specific, I mean why would we want tags to be shared globally? I don't see any use case unless there are some technical limitations.

@randy3k Could you please shed some light on this?

randy3k commented 2 years ago

It seems more sensible to make tags window specific. It just happened that we have only one dictionary to store the Terminal instances. Let me think what I could do to addres this.

randy3k commented 2 years ago

I have just pushed 6f300b9 and v0.3.24 is out.

randy3k commented 2 years ago

Ai, the patch doesn't work for panel. Wait for another update.

randy3k commented 2 years ago

v0.3.25 is out.

timfjord commented 2 years ago

Thanks, @randy3k

I can confirm tags are now window scoped! :tada:

randy3k commented 2 years ago

~~Just notice that there is another bug preventing this from working correctly. :(. Creating a new terminal might kill the process of an existing terminal with the same tag. WIll fix it soon.~~

Edit: I was wrong again. It is fine.