Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import sublime
import sublime_plugin
import subprocess
import re
import sys
# import os
class InputCommand(sublime_plugin.TextCommand):
def run(self, edit):
file_name = self.view.file_name()
print(file_name)
filetype = file_name[file_name.rfind('.') + 1:]
file_name_only = file_name[:file_name.rfind('.')]
line = self.view.substr(sublime.Region(0, self.view.size()))
if filetype == 'py':
try:
user_input = re.match("'''input\s*\n.*?\n'''", line, flags=re.S).group(0)[9:-4]
output = self.view.window().create_output_panel('op')
output.run_command('erase_view')
try:
result = subprocess.check_output('echo "' + user_input + '" | python ' + file_name,
stderr=subprocess.STDOUT,
shell=True)
output.run_command('append', {'characters': result.decode(sys.getfilesystemencoding())})
except subprocess.CalledProcessError as err:
print(err.returncode, err.output.decode(sys.getfilesystemencoding()))
output.run_command('append', {'characters': err.output.decode(sys.getfilesystemencoding())})
self.view.window().run_command("show_panel", {"panel": "output.op"})
except AttributeError as err:
print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment