Sublime Forum

Write to Output Panel from Python Plugin?

#1

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!

0 Likes

#2

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.

0 Likes

#3

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

window = view.window()                
results = window.newFile()
results.isScratch(True)

and here is the error in the console

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>)
0 Likes

#4

[quote=ā€œmikebā€]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

window = view.window()                
results = window.newFile()
results.isScratch(True)

and here is the error in the console

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>) [/quote]

Try results.setScratch(results.bufferId())
:smile:

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

0 Likes

#5

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

Iā€™ll take that back, with setScratch and setReadOnly APIs we can have as many output panels as we want :smiley:

0 Likes

#6

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.

0 Likes

#7

I think you just need to use: results.setScratch(True)
results.bufferId() evaluates to True, thatā€™s why it works.

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

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)
0 Likes