acquisto2
acquisto2

Reputation: 31

SublimeREPL: How can I automatically close (or restart) the previous console when rebuilding?

I recently switched to using Sublime Text as my Python IDE, and have since installed REPL. In order to re-use the same tab when running my code, I made a few edits repl_python_run command in my SublimeREPL\config\Python\Main.sublime-menu directory. (instructions here: Re-use tab ... SublimeREPL

This works well, and when I re-build a script (cmd + B) the new output stays in the same tab; however, I would like to automatically close/restart the previous console after doing so. The reason is that I keep accumulating open python environments that must be closed manually.

Here is a picture of my taskbar after running the same code three times

Here is a picture of the tab where REPL is running.

As shown in the second picture, I can type in exit() before rebuilding, but it would be nice if this could be done automatically. My thinking is that I could somehow set the previous console to close after rebuilding, or I could "re-run" the code in the same console via one simple command.

Upvotes: 3

Views: 1456

Answers (2)

user7778287
user7778287

Reputation: 367

Inspired by @Not_a_programmer's post, I did a slightly different modification.

In the sublimerepl.py (Windows 10) file C:\Users\User\AppData\Roaming\Sublime Text 3\Packages\SublimeREPL

Search for and change the code under to this:

if view.name() == view_id:
    found = view
    old_rv = self.repl_view(found)
        if old_rv:
            old_rv.on_close()
    #window.focus_view(found)    
    break

I comment this normally default feature which focuses sublimes view to the newly opened repl. Next for some reason its necessary to change the 'on_close' function like so:

def on_close(self):
    self.repl.kill()
    for fun in self.call_on_close:
        fun(self)

This does the trick for the most part, but I ran into issues in the repl output where the "REPL KILLED" text when running a new repl would mangle the output sometimes, so in the same file, I commented out this line:

# self.write("\n***Repl Killed***\n""" if self.repl._killed else "\n***Repl Closed***\n""")

This effectively ends the repl process before running a new one, and is convenient for keeping Python from overrunning my task manager at least.

Upvotes: 0

Not_a_programmer
Not_a_programmer

Reputation: 171

I did a dirty workaround which somehow works. I edited sublimerepl.py like:

if view.name() == view_id:
    found = view
    old_rv = self.repl_view(found)
    if old_rv:
        old_rv.on_close()
    window.focus_view(found)
    break

Also I changed the on_close function from

 self.repl.close()

to

 self.repl.kill()

Now, the amount of Python instances stays the same.

There are still some inconsistencies. When I use this code to talk to an Arduino, I'll get a PermissionError every other time. Adding time.sleep(1) didn't change this. I'm open to suggestions.


EDIT: After using my "solution" for a while, I got annoyed by ***Repl Killed*** appearing everytime I executed code. Therefore, I decided to close the current REPL view and just start a new one. This way, I don't get an unnecessary amount of tabs, the background Python shell is closed and I start a fresh view every time I execute my code. The new code for sublimerepl.py is:

for view in window.views():
    if view.name() == view_id:
        view.close()
        break
view = window.new_file()

close() still has to be replaced by kill() for some reason.

Upvotes: 2

Related Questions