Skip to content

Instantly share code, notes, and snippets.

@mavidser
Created January 4, 2015 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavidser/83d50803622ae70895ce to your computer and use it in GitHub Desktop.
Save mavidser/83d50803622ae70895ce to your computer and use it in GitHub Desktop.
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