Hey there,
I’m pretty sure someone has encountered this problem already, but unfortunately I can’t seem to find any related topics. Here’s my problem:
I’m writing a plugin that executes an external command. If executed from the terminal, this command yields several lines one at a time. I’d like to be able to launch my plugin so that these results are displayed in my “tests” panel on-line, but all I can get is all lines after the execution is completed along with other info lines I append to the console somewhere. What is worse, Sublime hangs while the process is in progress, which sucks big time. So the questions are:
- How do I execute the process in the background so that Sublime doesn’t hang?
- How do I output results line by line as soon as they are produced?
Thanks in advance!
Here’s my code structure in case you need it.
class MyCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        self.show_tests_panel()
        if len(self.view.file_name()) > 0:
            # some magic ... 
            # my guess is I need threading here, 
            # but I have no experience with that
            self.some_function()
    def some_function():
        # blah blah
        # this part below should probably be rewritten
        p = subprocess.Popen(command, stdout=subprocess.PIPE)
        out, _ = p.communicate()
        self.append_data(out)
    def append_data(self, data):
        self.output_view.set_read_only(False)
        edit = self.output_view.begin_edit()
        self.output_view.insert(edit, self.output_view.size(), data)
        self.output_view.end_edit(edit)
        self.output_view.set_read_only(True)
    def show_tests_panel(self):
        if not hasattr(self, 'output_view'):
            self.output_view = self.view.window().
                                   get_output_panel("tests")
        self.clear_test_view()
        self.view.window().run_command("show_panel", 
                                       {"panel": "output.tests"})
    def clear_test_view(self):
        self.output_view.set_read_only(False)
        edit = self.output_view.begin_edit()
        self.output_view.erase(edit, 
                               sublime.Region(0, self.output_view.size()))
        self.output_view.end_edit(edit)
        self.output_view.set_read_only(True)  
