Created
January 4, 2015 12:44
-
-
Save mavidser/83d50803622ae70895ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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