Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3/ST3 support #66

Merged
merged 3 commits into from Oct 17, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 46 additions & 21 deletions guard.py
@@ -1,6 +1,11 @@
import sublime
import sublime_plugin
import thread
try:
import thread
st_version = 2
except ImportError:
from threading import Thread
st_version = 3
import subprocess
import os
import stat
Expand All @@ -19,7 +24,10 @@ def __init__(self):

def set_listener(self, listener):
self.listener = listener
self.output_view = self.listener.window.get_output_panel('guard')
if st_version == 2:
self.output_view = self.listener.window.get_output_panel('guard')
else:
self.output_view = self.listener.window.create_output_panel('guard')
self.enable_word_wrap()
self.set_color_scheme()
self.load_config()
Expand All @@ -37,7 +45,7 @@ def path_has_guardfile(self, path):
def find_project_root_path(self):
project_root_path = None
for path in self.open_folder_paths():
print "Checking ... " + path
print ("Checking ... " + path)
if (self.path_has_guardfile(path)):
project_root_path = path
break
Expand Down Expand Up @@ -72,9 +80,15 @@ def start_guard(self):
self.running = True
self.show_guard_view_and_enable_autoshow()
if self.proc.stdout:
thread.start_new_thread(self.read_stdout, ())
if st_version == 2:
thread.start_new_thread(self.read_stdout, ())
else:
Thread(target=self.read_stdout, args=()).start()
if self.proc.stderr:
thread.start_new_thread(self.read_stderr, ())
if st_version == 2:
thread.start_new_thread(self.read_stderr, ())
else:
Thread(target=self.read_stderr, args=()).start()

def read_stdout(self):
while True:
Expand Down Expand Up @@ -104,20 +118,24 @@ def append_data(self, data):
clean_data = self.remove_terminal_color_codes(clean_data)

# actually append the data
self.output_view.set_read_only(False)
edit = self.output_view.begin_edit()
if st_version == 2:
self.output_view.set_read_only(False)
edit = self.output_view.begin_edit()

# clear the output window when a predefined text is found.
if (self.clear_when_find_this_text and self.clear_when_find_this_text.search(clean_data)):
self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
# clear the output window when a predefined text is found.
if (self.clear_when_find_this_text and self.clear_when_find_this_text.search(clean_data)):
self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))

self.output_view.insert(edit, self.output_view.size(), clean_data)
self.output_view.insert(edit, self.output_view.size(), clean_data)

# scroll to the end of the new insert
self.scroll_to_end_of_guard_view()
# scroll to the end of the new insert
self.scroll_to_end_of_guard_view()

self.output_view.end_edit(edit)
self.output_view.set_read_only(True)
self.output_view.end_edit(edit)
self.output_view.set_read_only(True)
else:
self.output_view.run_command('guard_message', {'string': clean_data})
self.scroll_to_end_of_guard_view()

def normalize_line_endings(self, data):
return data.replace('\r\n', '\n').replace('\r', '\n')
Expand All @@ -142,31 +160,31 @@ def hide_guard_view(self):
self.listener.window.run_command('hide_panel', {'panel': 'output.guard'})

def stop_guard(self):
self.proc.stdin.write('e\n')
self.proc.stdin.write(b'e\n')
self.proc.stdin.flush()
self.running = False

def is_guard_running(self):
return self.running

def reload_guard(self):
self.proc.stdin.write('r\n')
self.proc.stdin.write(b'r\n')
self.proc.stdin.flush()

def run_all_tests(self):
self.proc.stdin.write('\n')
self.proc.stdin.write(b'\n')
self.proc.stdin.flush()

def output_help(self):
self.proc.stdin.write('h\n')
self.proc.stdin.write(b'h\n')
self.proc.stdin.flush()

def toggle_notifications(self):
self.proc.stdin.write('n\n')
self.proc.stdin.write(b'n\n')
self.proc.stdin.flush()

def pause(self):
self.proc.stdin.write('p\n')
self.proc.stdin.write(b'p\n')
self.proc.stdin.flush()

def load_config(self):
Expand Down Expand Up @@ -276,3 +294,10 @@ def run(self):

def is_enabled(self):
return GuardControllerSingleton().is_guard_running()

class GuardMessageCommand(sublime_plugin.TextCommand):
"""
A command to write a message to the Guard messaging buffer
"""
def run(self, edit, string=''):
self.view.insert(edit, self.view.size(), string)