Home Download Buy Blog Forum Support

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

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

Postby TXHank on Tue Aug 05, 2014 3:30 pm

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
TXHank
 
Posts: 3
Joined: Wed Jul 30, 2014 8:06 pm

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

Postby TXHank on Tue Aug 05, 2014 7:29 pm

This is how I accomplished it at this time:

Code: Select all

      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)

TXHank
 
Posts: 3
Joined: Wed Jul 30, 2014 8:06 pm

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

Postby sapphirehamster on Thu Aug 07, 2014 12:00 am

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

Code: Select all
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: https://docs.python.org/3/library/subprocess.html

You can change the settings on the output_view like any normal view.
sapphirehamster
 
Posts: 91
Joined: Sun Jul 01, 2012 11:19 pm

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

Postby TXHank on Fri Aug 08, 2014 8:42 pm

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

Code: Select all
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: https://docs.python.org/3/library/subprocess.html

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


Interesting, thank you.
TXHank
 
Posts: 3
Joined: Wed Jul 30, 2014 8:06 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests