Sublime Forum

(?) Run Command Via Addon and Show Results In Console

#1

Is it possible to run an console style application via an addon and display that output into the console output box?

I can run the command all day long via the addon with no issue, the problem I am having is capturing the output of the command and slapping it into the console output window.

I was going to run the command “cmd.exe /c xfgbuild > log.txt” and just print the contents of the log.txt on the screen. However if you go to dos and type “xfgbuild > log.txt” it actually does not redirect all output into the file. So that is not an option here.

I could do it as a build system, but unless you can make an addon run a build system that doesn’t make sense in my case.

TIA

0 Likes

#2

This is how I accomplished it at this time:


      cwd = os.getcwd()
      os.chdir(path)
      e = subprocess.call("cmd.exe /c dir > c:\\cmd.log 2>&1")
      os.chdir(cwd) 
      
      fil = open("c:\\cmd.log", 'r')
      f = fil.read()
      fil.close()
      print(f)
0 Likes

#3

The subprocess module has a variety of methods to help with capturing the output. For example:

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Using cmd.exe here may not be necessary based on your needs.
        output = subprocess.check_output('cmd.exe /c dir', universal_newlines=True)
        window = self.view.window()
        output_view = window.create_output_panel('my_output')
        window.run_command('show_panel', {'panel': 'output.my_output'})
        output_view.run_command('insert', {'characters': output})

See the subprocess docs for more detail: docs.python.org/3/library/subprocess.html

You can change the settings on the output_view like any normal view.

0 Likes

#4

[quote=“sapphirehamster”]The subprocess module has a variety of methods to help with capturing the output. For example:

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Using cmd.exe here may not be necessary based on your needs.
        output = subprocess.check_output('cmd.exe /c dir', universal_newlines=True)
        window = self.view.window()
        output_view = window.create_output_panel('my_output')
        window.run_command('show_panel', {'panel': 'output.my_output'})
        output_view.run_command('insert', {'characters': output})

See the subprocess docs for more detail: docs.python.org/3/library/subprocess.html

You can change the settings on the output_view like any normal view.[/quote]

Interesting, thank you.

0 Likes