Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/ck_com_shell.py
Created November 25, 2013 20:21
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/7648175 to your computer and use it in GitHub Desktop.
Save zeffii/7648175 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_actions(self, commands):
# if it reaches here, there are more subtle things going on.
Ck_com_shell.sh.stdin.write(commands)
p = Ck_com_shell.sh.stdin.write("\x0c")
print(p)
# Ck_com_shell.stdin.stdin.flush()
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)
print(right_side)
except:
print("unparsable")
return
return right_side
class Ck_com_shell(sublime_plugin.TextCommand):
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("ended a chuck process!")
return
if commands == "shell":
# start shell as subprocess in a thread.
print("starting shell")
th = Ck_Shell_Start(["chuck", "--shell"])
th.start()
return
# if we reach here, the user is likely entering console commands.
if Ck_Shell_Start.sh.isAlive():
com_actions(commands)
else:
print("server seems lost, try: (re)starting it.")
else:
print("see com shell documentation")
class Ck_Shell_Start(threading.Thread):
sh = None
def __init__(self, commands):
self. commands = commands
threading.Thread.__init__(self)
def run(self):
Ck_Shell_Start.sh = subprocess.Popen(self.commands,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True).communicate()
if Ck_Shell_Start.sh:
try:
print(sh[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