Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/ck_com_shell.py
Created November 26, 2013 00:58
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/7651707 to your computer and use it in GitHub Desktop.
Save zeffii/7651707 to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import subprocess
import threading
import os
"""
// --- these don't require the console prefix %>
[-] chuck --shell : will start ChucK in --shell mode (server) using a new thread.
[-] kill : will end chuck (should end all chucks alive).
// --- these do, because it indicates that you expect an open console.
[ ] %> -k : will kill ChucK server thread. (no space between -k)
[ ] %> + this: add shred (current open file), "this" will be reserved
[ ] %> =/- this: replace/remove shred (current open file)
[ ] %> = x.ck: replace shred named x
[ ] %> - x.ck : remove shred named x
[ ] %> = number(s) : replace shred(s) by id, separated by spaces
[ ] %> - number(s) : remove shred(s) by id, separated by spaces
[ ] %> -all : remove all shreds. (no space between -all)
[ ] %> status: will print the status to the python console.
A key combo : will add selection as shred (sends everything enclosed in { }
as an 'on the fly' shred)
"""
def kill_chuck():
p = subprocess.call(["chuck", "--kill"])
print(p)
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", "unchuck"):
return "kill"
if found_content == "chuck --shell":
return "shell"
sides = found_content.split("%>")
right_side = sides[1].strip()
# print(right_side)
# 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
class Ck_com_shell(sublime_plugin.TextCommand):
chuck_process = None
chuck_queue = None
chuck_thread = None
def run(self, edit):
view = self.view
file_path = view.file_name()
file_name = os.path.basename(file_path)
selections = view.sel()
if selections[0].a == selections[0].b:
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":
kill_chuck()
print("ending a chuck process!")
return
# start shell as subprocess in a thread.
if commands == "shell":
print("starting shell")
chuck_thread = Ck_Shell(["chuck", "--shell"])
chuck_thread.start()
return
if commands:
print(commands)
try:
if chuck_thread.isAlive():
Ck_Shell.shell_process.stdin.write(commands + "\x0c")
except:
print("server seems lost, try: (re)starting it.")
return
else:
print("see com shell documentation")
class Ck_Shell(threading.Thread):
def __init__(self, commands):
self. commands = commands
threading.Thread.__init__(self)
def run(self):
Ck_Shell.shell_process = subprocess.Popen(self.commands,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True).communicate()
if Ck_Shell.shell_process:
try:
print(shell_process[0].decode())
except:
print("nothing to print")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment