Home Download Buy Blog Forum Support

Write to Output Panel from Python Plugin?

Write to Output Panel from Python Plugin?

Postby mikeb on Sun Sep 20, 2009 7:23 pm

I am working on a plugin and would like to write messages to the output panel, rather than the console with the "print" command. Searching the forums I found out that this has been asked before and was resolved by using the build commands, but in my case I don't think that will work. Is there any way to write to the output panel from a python plugin? Thanks!
mikeb
 
Posts: 31
Joined: Mon Sep 07, 2009 12:33 am

Re: Write to Output Panel from Python Plugin?

Postby jps on Tue Sep 22, 2009 10:58 am

There isn't a way to do this yet, but 20090922 does introduce a new feature of scratch buffers - if you create a new buffer, and call view.isScratch(True), then it'll be a more appropriate place to send plugin output: the user won't get prompted to save it.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Write to Output Panel from Python Plugin?

Postby mikeb on Tue Sep 22, 2009 5:18 pm

Thanks for the response and the scratch buffer is cool, but not quite what I had in mind for output. Really I am just looking for a way to display results just as it is in the console or output panel. Console is actually fine, it's just that there is quite a bit of other things being printed there and it would be nice to have a clean panel to send plugin output to.

I tried the scratch panel setting(with beta 20090923) but it didn't seem to work. Here is the python
Code: Select all
window = view.window()               
results = window.newFile()
results.isScratch(True)

and here is the error in the console
Code: Select all
Traceback (most recent call last):
  File ".\sublimeplugin.py", line 118, in execTextCommand
  File ".\Repl.py", line 35, in run
Boost.Python.ArgumentError: Python argument types in
    View.isScratch(View, bool)
did not match C++ signature:
    isScratch(class slate::SP<class TextBufferView>)
mikeb
 
Posts: 31
Joined: Mon Sep 07, 2009 12:33 am

Re: Write to Output Panel from Python Plugin?

Postby EJ12N on Tue Sep 22, 2009 6:24 pm

mikeb wrote:Thanks for the response and the scratch buffer is cool, but not quite what I had in mind for output. Really I am just looking for a way to display results just as it is in the console or output panel. Console is actually fine, it's just that there is quite a bit of other things being printed there and it would be nice to have a clean panel to send plugin output to.

I tried the scratch panel setting(with beta 20090923) but it didn't seem to work. Here is the python
Code: Select all
window = view.window()               
results = window.newFile()
results.isScratch(True)

and here is the error in the console
Code: Select all
Traceback (most recent call last):
  File ".\sublimeplugin.py", line 118, in execTextCommand
  File ".\Repl.py", line 35, in run
Boost.Python.ArgumentError: Python argument types in
    View.isScratch(View, bool)
did not match C++ signature:
    isScratch(class slate::SP<class TextBufferView>)


Try
Code: Select all
results.setScratch(results.bufferId())

:)

But I agree we need a way to write to output panel!
something like sublime.outputGet() /sublime.outputWrite() :D
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: Write to Output Panel from Python Plugin?

Postby EJ12N on Wed Sep 23, 2009 3:38 pm

But I agree we need a way to write to output panel!
something like sublime.outputGet() /sublime.outputWrite() :D


I'll take that back, with setScratch and setReadOnly APIs we can have as many output panels as we want :D
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: Write to Output Panel from Python Plugin?

Postby mikeb on Wed Sep 23, 2009 7:28 pm

So I was able to get output printed to a scratch buffer and I can now see some real benefits to using them over the output panel or console and perhaps we don't even need a way to print to output console. One idea I am thinking about would be to create a syntax definition for the scratch buffer to make it more readable. My initial preference was just to be able to have an area at the bottom of the screen to display output and history... perhaps a new layout that had the a pane on bottom that spanned all vertical panes above might be a valuable layout to add and would serve the same purpose but also be tabbed, etc...

** A note for others that might be trying to do the same thing as me. results.setScratch(results.bufferId()) worked perfectly. In order to find the same panel on a second run of the plugin I had to set the name of the scratch buffer and then loop through the all the views and check the name of each one to find the same buffer on a second run of the plugin. I tried results = window.openFile(name) but it just said that it couldn't find the open file with that name, which might be a bug.
mikeb
 
Posts: 31
Joined: Mon Sep 07, 2009 12:33 am

Re: Write to Output Panel from Python Plugin?

Postby gpfsmurf on Wed Sep 23, 2009 10:50 pm

I think you just need to use:
Code: Select all
results.setScratch(True)

results.bufferId() evaluates to True, that's why it works.


In order to find the same panel on a second run of the plugin I had to set the name of the scratch buffer and then loop through the all the views and check the name of each one to find the same buffer on a second run of the plugin.

You can keep the scratch buffer in a class variable:

EDIT: there's a problem with the code below: if the user closes the "My output" buffer, the plugin will not be aware of it and "self.Results in view.window().views()" always returns false

Code: Select all
class MyCommand(sublimeplugin.TextCommand):
   Results = ''
   def run(self, view, args):
      output = "blablabla"
      
      if not self.Results:# or not self.Results in view.window().views():
         # Need to create Results scratch buffer
         self.Results = view.window().newFile()
         self.Results.setScratch(True)
         self.Results.setName("My output")
         
      self.Results.insert(-1, output + "\n")
      view.window().focusView(self.Results)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm


Return to General Discussion

Who is online

Users browsing this forum: Yahoo [Bot] and 16 guests