Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/ChucK.py
Created December 6, 2013 21:34
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 zeffii/7832489 to your computer and use it in GitHub Desktop.
Save zeffii/7832489 to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import sys
import subprocess
import threading
import webbrowser
import os
from queue import Queue, Empty
ON_POSIX = 'posix' in sys.builtin_module_names
def command_dispatcher(commands):
print("doing command:", commands)
Ck_loop_vmCommand.chuck_process.stdin.write(bytes(commands))
Ck_loop_vmCommand.chuck_process.stdin.write(bytes("\x0c"))
Ck_loop_vmCommand.chuck_process.stdin.flush()
def kill_chuck():
subprocess.call(["chuck", "--kill"])
print("ending a chuck process!")
return
def com_parser(line_under_cursor, file_name):
right_side = None
try:
found_content = line_under_cursor.rsplit("//", 1)[1]
found_content = found_content.strip()
if found_content in ("kill", "%> -k"):
return "kill"
# if found_content == "chuck --shell":
# return "shell"
sides = found_content.split("%>")
right_side = sides[1].strip()
# this will refer to the name of the current file.ck
if "this" in right_side:
right_side = right_side.replace("this", file_name)
except:
print("unparsable")
return
return right_side
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
# command to start ChucK interpreter chuck
class Ck_loop_vmCommand(sublime_plugin.TextCommand):
chuck_process = None
chuck_queue = None
chuck_thread = None
output_view = None
panel_name = None
def run(self,edit):
# start ChucK
if Ck_loop_vmCommand.chuck_thread is None or not Ck_loop_vmCommand.chuck_thread.isAlive():
settings = sublime.load_settings("ChucK.sublime-settings")
file_path = self.view.file_name()
file_name = os.path.basename(file_path)
cwd = os.path.dirname(file_path)
print("Starting ChucK : ", cwd)
chuck_init = ["chuck", '--shell']
Ck_loop_vmCommand.chuck_process = subprocess.Popen(chuck_init,
cwd=cwd, bufsize=1,
close_fds=ON_POSIX,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True, shell=True)
Ck_loop_vmCommand.chuck_queue = Queue()
threading_args = (
Ck_loop_vmCommand.chuck_process.stdout,
Ck_loop_vmCommand.chuck_queue)
Ck_loop_vmCommand.chuck_thread = threading.Thread(
target=enqueue_output,
args=threading_args)
# thread dies with the program
Ck_loop_vmCommand.chuck_thread.daemon = True
Ck_loop_vmCommand.chuck_thread.start()
print("ChucK has started")
sublime.set_timeout(self.poll, 800)
def poll(self):
# continue while chuck is running
if Ck_loop_vmCommand.chuck_thread is not None and Ck_loop_vmCommand.chuck_thread.isAlive():
ckReturnedSomething = True
somethingHappened = False
# while ckReturnedSomething:
# try:
# line = Ck_loop_vmCommand.chuck_queue.get_nowait()
# except Empty:
# ckReturnedSomething = False
# else:
# somethingHappened = True
try:
line = Ck_loop_vmCommand.chuck_queue.get_nowait()
except Empty:
ckReturnedSomething = False
else:
somethingHappened = True
sublime.set_timeout(self.poll, 1000)
# command to stop ChucK interpreter chuck
class Ck_kill_vmCommand(sublime_plugin.WindowCommand):
def run(self):
print("Stopping ChucK")
if Ck_loop_vmCommand.chuck_thread is not None and Ck_loop_vmCommand.chuck_thread.isAlive():
print('enters here')
Ck_loop_vmCommand.chuck_process.stdin.write("--kill")
Ck_loop_vmCommand.chuck_process.stdin.write("\x0c")
Ck_loop_vmCommand.chuck_process.stdin.flush()
# subprocess.call(["chuck", "--kill"])
# command to send the current line to chuck
class Ck_add_shredCommand(sublime_plugin.TextCommand):
def run(self, edit):
if Ck_loop_vmCommand.chuck_thread is not None and Ck_loop_vmCommand.chuck_thread.isAlive():
view = self.view
print("still allive")
file_path = view.file_name()
file_name = os.path.basename(file_path)
selections = view.sel()
if not selections[0].a == selections[0].b:
print("see com shell documentation")
return
sel = view.line(selections[0])
line_under_cursor = view.substr(sel)
commands = com_parser(line_under_cursor, file_name)
if not commands:
return
if commands == "kill":
commands = "--kill"
command_dispatcher(commands)
@zeffii
Copy link
Author

zeffii commented Dec 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment